Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Instructions: Your assignment is to write three shell scripts that are used to s

ID: 3750026 • Letter: I

Question

Instructions: Your assignment is to write three shell scripts that are used to setup and initialize a simple text database. Create additional shell scripts as needed to abstract out common operations. While you are allowed to invoke other shell scripts that you have written and the Unix utilities specified above, you are not allowed to invoke other executables i.e. you cannot write your solution in a conventional programming language like C+ or Java) If you need to store information into a temporary file, then you can use the file imp.tx. But be sure to delete it before you exit your script. ns.sh The first shell script should be named ns.sh. Its purpose is to create a new schema. It accepts as arguments a schema name and a list of up to cight field names. For instance, below is an example invocation of the ns.sh script and response ns.sh person name age weight Schema "person" has been created. The information regarding schemas should be stored in a file called schemas.txt. Each line of that file contains the schema name followed by the field names. Below is an example schemas.txt file: person name age weight grades name examl exam2 exam3 company employee SSN age Your script should check if the schema name already exists and if so it should print an appropriate error message and exit. If it doesn't already exist, then the script should append the information to the end of the file and print a message that the schema has been created nd.sh The second shell script should be named nd.sh and its purpose is to create a new database. It accepts two arguments, where the first is a schema name and the second is a database name. For instance, below is an example invocation of the ns.sh script and response % nd.sh person medical Database "medical" using schema "person" 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: medical person cop3330 grades ntel company cop4342 grades If either the schema name does not exist or the database name already exists, then you should print 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 .db, and print a message that the database has been created. -1 ir.sh The third shell script should be named irsh and its purpose is to insert a record into a database. It accepts one argument, which is the database name. The irsh script should then prompt the user for the values of the fields in that database. After that the script should read in the field values and append these values in a new line to the end of the does not exist, then you should print an appropriate error message and exit.

Explanation / Answer

Below is first shell "ns.sh" script to add schema in file "schemas.txt"


#!/bin/bash
if [ "$#" -gt 9 ]; then
echo "Feilds should not more than 8 "
fi
schema = $1
feild1 = $2
feild2 = $3
feild3 = $4
feild4 = $5
feild5 = $6
feild6 = $7
feild7 = $8
feild8 = $9
file= 'schemas.txt'
exits=$(grep -c $schema $file)
if [[ $exists -gt 0 ]]; then
echo "Schemas is already exist.."
else
echo -e "$schema $ $feild1 $feild2 $feild3 $feild4 $feild5 $feild6 $feild7 $feild8" >>$file

echo "Schema " $schema " has been created "
fi

Description :

First of all we check the number of parameter that we have pass to script i.9 parameter . In that 1 for shema name and 8 fore field name then take this all parameter in variable.After we need to check scema is already present or not so it is checked by grep count with -c which will give count 1 is schema is already present in scchema.txt file and it will return 0 if schema not present in file.

if it is not present in file then we will writte schema and feilld name in file. we are using >> for write file in apend mode. if we use > the it will not write file in append mode.

Below is seconf script " nd.sh" to add new database to file database.txt


#!/bin/bash
if [ "$#" -eq 2 ]; then
echo "Parameter must be 2 i.e one schema and second for database name "
fi
schema = $1
database = $2

dbfile = 'database.txt'
schemafile = 'schemas.txt'
shema_exits =$(grep -c $schema $schemafile)
db_exits =$(grep -c $database $dbfile)
if [[ $shema_exits -lt 0 ]]; then
echo "Schemas does not exist..."
exit 1;
fi
if [[ $db_exits -gt 1 ]]; then
echo "database is already exist..."
fi
if [[ ( $db_exits -lt 0 ) || ( $shema_exits -eq 1 )]]; then
echo -e "$schema $database" >>$dbfile

touch $database.db
echo "Database " $database " using schema "$schema " has been created."
fi

Description :

First of all we check parameter that need to pass to script , this parameter must be 2.if it is not then it will print error message "Parameter must be 2 i.e one schema and second for database name" this parameter check by using [ "$#" -eq 2 ] condition.

Then we check schema and database exit or not in schema and database file using below command :

shema_exits =$(grep -c $schema $schemafile)
db_exits =$(grep -c $database $dbfile)

above grep command return number i.e 1 if exit and 0 if it is not exit.

so we just give condition of variable value in if condition.

if condition schema exit and database does not exit (this condition check by [[ ( $db_exits -lt 0 ) || ( $shema_exits -eq 1 )]] ) then we create database by inserting entry in database and also create file database_name.db using touch command.

Below is final script that will add record to database :

#!/bin/bash
if [ "$#" -gt 1 ]; then
echo "Parameter should be less than 1 "
fi
db_name = $1


dbfile = 'database.txt'
schemafile = 'schemas.txt'
db_exits =$(grep -c $database $dbfile)
if [[ $db_exits -lt 1 ]]; then
echo "database does not exist..."
exit 1
fi
grep $db_name $schemafile | awk -F ":" '{print "Enter value for" $1" " $2" " $3" " $4" " $5" " $6" " $7" " $8" "}'
read valueField
echo $valueField >> $db_name.db
echo "Record added to "$db_name " database"

Description :

Most of the command and logic are explain in above scripts only main thing is how to read the feild name from schema file by searching schema and read particular line of the schema file.

this logic are implemented by awk kit of unix took. this coomand is given below :

grep $db_name $schemafile | awk -F ":" '{print "Enter value for" $1" " $2" " $3" " $4" " $5" " $6" " $7" " $8" "}'

this comand search shema and print feild .

Another different concept is to read value from user , this is done by simple command " read variable_name"

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote