#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Posts
    1
    Rep Power
    0

    Simple Text Database:


    Im having troubles with the grep in this script. This is what I need to do:

    The script should create a new file <database_name>.db, which will store entries in the
    database. In the above example, the file catalogue.db would be created. Note: After
    running the nd.sh command, the file will be empty.
    The name of the new database and the schema used should be appended to the file
    databases.txt. Each line in the file should thus contain one database_name and one
    schema_name.

    This is my script:

    #!/bin/sh
    #script name is nd.sh


    if [ $# -ne 2]
    then
    echo "Wrong amount of arguments."
    elif [ ! -f "schemas.txt" ]
    then
    echo "The file the schemas.txt does not exist."
    elif [ ! -r "schemas.txt" ]
    then
    echo "The file schemas.txt is not readable."
    elif [ ! -f "databases.txt" ]
    then
    echo "The file databases.txt does not exist."
    elif [ ! -r "databases.txt" ]
    then
    echo "The file databases.txt is not readable."
    elif [ ! -w "databases.txt" ]
    then
    echo "The file databases.txt is not writable."
    else
    touch $1.db
    echo $1 $2 databases.txt
    for grep $2 schemas.txt
    do
    echo $2 " Is already in schemas.txt."
    done
    for grep $1 databases.txt
    do
    echo $1 " Is already in databases.txt."
    done
    fi

    exit 0

    And this is what I need to check for:

    Your script should check for the following error conditions
    a) The user entered the correct number of command line arguments
    b) The file schemas.txt exists and is readable
    c) The file databases.txt exists and is both readable and writeable
    d) The schema_name given by the user exists in the schemas.txt file.
    e) The database_name given by the user does not exist in the databases.txt file
    f) The <database_name>.db file does not already exist

    My problem is it says $2 in line 26 is an irregular use of token, if you can check the script for any other errors i would appreciate it.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Location
    Dublin, Ireland
    Posts
    174
    Rep Power
    48
    This isn't valid syntax

    Code:
    for grep $2 schemas.txt
    I'm not quite sure what you're trying to achieve, your code doesn't make much sense... perhaps you're trying to do something like this?

    Code:
    if [[ `grep $2 schemas.txt` ]]; then
        echo "$2 is already in schemas.txt";
        if [[ `grep $1 databases.txt` ]]; then
             echo "$1 is already in databases.txt";
        fi
    fi
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    867
    Rep Power
    391

    Cool



    -- Or --

    Code:
    #!/bin/sh
    #script name is nd.sh
    
    [ $# -ne 2 ] && `echo "Wrong number of arguments."; exit 1;`
    if [ ! -f schemas.txt ]; then
      echo "The file schemas.txt does not exist."; exit 1;
    elif [ ! -r schemas.txt ]; then
      echo "The file schemas.txt is not readable."; exit 1;
    elif [ ! -f databases.txt ]; then
      echo "The file databases.txt does not exist."; exit 1;
    elif [ ! -r databases.txt ]; then
      echo "The file databases.txt is not readable."; exit 1;
    elif [ ! -w databases.txt ]; then
      echo "The file databases.txt is not writable."; exit 1;
    fi
    echo "Processing: Database $1, Schema $2"
    flg=0
    if [ ! `grep $2 schemas.txt` ]; then 
      echo "$2 Is NOT in schemas.txt."; flg=1;
    fi
    if [ `grep $1 databases.txt` ]; then
      echo "$1 Is already in databases.txt."; flg=1;
    fi
    if [ -f $1.db ];then 
      echo "$1.db already exists."; flg=1;
    fi
    [ ! "$flg" == '0' ] && echo "Script aborted!"; exit 2;

    Last edited by LKBrwn_DBA; April 2nd, 2007 at 02:24 PM.

IMN logo majestic logo threadwatch logo seochat tools logo