0

i write an two scripts for sqlite3 operations .In my first script i create the database and the table name .and in my second script i create the table records.While executing the second script i want to get the already created database and choose the table name..please any one help me..!

script1.sh

#!/bin/bash

echo " --- Enter the Database name ---" #name of the database
read databasename

echo " --- enter the table name --- " #name of the table
read table_name

sqlite3 $databasename.db "DROP TABLE IF EXISTS $table_name;"

sqlite3 $databasename.db  "CREATE TABLE IF NOT EXISTS $table_name(cus_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,cus_name TEXT NOT NULL UNIQUE ,cus_domain TEXT UNIQUE, cus_status TEXT NOT NULL,Port INTEGER NOT NULL);" #table creation

Script2.sh

echo "choose the database"
#Here i want to choose the already create database.

echo "choose the table"
#Here i want to choose the table in the alreadt created database.

echo " --- Enter the total number of customer records do you want ---"
read cus_count # number of rows value


echo "--- Enter the following details one by one---"

RED='\033[0;31m'
NC='\033[0m'
port_num=8080
declare -a customer

for((i=1;i<=cus_count;i++))
do


echo "enter the $i customer details"

echo "---Enter the customer name---"
read c_name

d_name=${c_name,,}
#echo $d_name

customer=$(sqlite3 $databasename.db "select cus_domain from $table_name where cus_domain like '$d_name';")

for cus in "${customer[@]}"
do

if [[ $d_name != $customer ]];

then
    echo "---Enter the Status(Active/Inactive)---"
    read c_status

if [[ "$port_num" == "$port_num" ]]; then
       port_num=$(($port_num + 1))

c_domain="$c_name"


sqlite3 $databasename.db "INSERT OR IGNORE INTO $table_name (cus_name,cus_domain,cus_status, Port) VALUES(\"$c_name\",\"${c_domain,,}\",\"${c_status,,}\",\"$port_num\") ;" 
fi


else
    echo -e "${RED}!!!OOPS for you entered customer name already  domain name assigned!!!${NC}"
    echo -e "${RED}Please enter new customer name${NC}"

i=$(($i - 1))

fi
done
done

echo " --- Records from the $table_name ---"

sqlite3 $databasename.db "select * from $table_name;"

please any one help me..Thanks in advance...

  • How about writing the tablename to a file in the first script `echo -n $table_name > table_name.txt` and picking it up in the second `table_name=$(cat table_name.txt)` – Mark Setchell Aug 17 '17 at 06:32
  • for table selection ok.but how can i select the database...? – Mahendranatarajan Aug 17 '17 at 06:42
  • Write the database name to a second file. Or write database name to file first followed by appending table name in same file maybe? – Mark Setchell Aug 17 '17 at 06:46
  • To select from a list, consider using something like [dialog](https://linux.die.net/man/1/dialog). – CL. Aug 17 '17 at 08:14

1 Answers1

0

You can write the database and table name to a file.

echo $databasname >> /tmp/new_database
echo $table >> /tmp/new_database

and configure the second script to read this file

Or you can call the script two from script one passing databasename and table name as args

script1
#!/bin/bash

...
/bin/bash script2.sh $databasename $tablename


script2.sh
#!/bin/bash
databasename = $1
tablename = $2
Joao Vitorino
  • 2,976
  • 3
  • 26
  • 55