Welcome to the Linux Foundation Forum!

Shell Script Question

Hi all

I want u to help answer this question

( A shell script should be named new_database.sh and its purpose is to create a new database. It accepts two arguments, the first is SCHEMA_NAME and the second is a DATABASE_NAME. For instance, below is an example invocation of the new_database.sh script and response.

$ .new_database.sh persons tutors

DATABASE ‘tutors’ USING SCHEMA 'persons' HAS BEEN CREATED.

The name of the database and the schema it uses should be appended to the databases.txt file.

Below is an example databases.txt file:

databases.txt

tutors persons

student persons

nina_12345_grades grades

intel company

If either the SCHEMA_NAME does not exist or the DATABASE_NAME already exists, then you the command prints an appropriate error message and exit. If the SCHEMA_NAME exists and the DATABASE_NAME is not already used, then the script should append the information to the end of the databases.txt file, create a new empty database file of the form DATABASE_NAME.db, and print a message that the database has been created.

)

thank you.

Comments

  • Goineasy9
    Goineasy9 Posts: 1,114
    And the question is?
  • mfillpot
    mfillpot Posts: 2,177
    Are you building your own text file based database or will this script be used with a database system like mysql or postgresql?

    Also can you please post the contents of your script so we can review your progress and recommend modifications?
  • here's a possible script skeleton. Copy the code below and paste into
    a script file. Fill in with your commands (database creation, etc.).
    #!/bin/sh
    
    LOG=databases.txt
    usage()
    {
    cat <<END
    USAGE: $0 schema_name database_name
    provide description here
    END
    return 1
    }
    create_database()
    {
    #enter the commands to create the database
    if [ $? -eq 0 ]; then
           echo "created database $2 with schema $1"
           echo "$2 $1" >> $LOG
            return 0
    else
            echo "your error message here"
            return 1
    fi
    }
    #check number of arguments entered
    if [ $# -ne 2 ]; then
            usage
            exit $?
    fi
    create_database $1 $2
    

    You should probably use absolute path for the log file, eg
    LOG=$HOME/databases/databases.txt
    
    That way you could invoke the script from anywhere.

Categories

Upcoming Training