我想将数据库表中的特定列值与Shell脚本中的相应输入字符串进行比较。我怎样才能做到这一点?
在下面的脚本中,我想读取所有客户值,但只读取第一个值。我能做什么?

customer=$(sqlite3 $databasename.db "select cus_name from $table_name"); if [[ $c_name != $customer ]];

#!/bin/bash

echo " --- Enter the Database name ---"
read databasename

echo " --- enter the table name --- "
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);"


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


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

port_num=8080

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


echo "enter the $i customer details"

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

customer=$(sqlite3 $databasename.db "select cus_name from $table_name");


if [[ $c_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,,}.in\",\"$c_status\",\"$port_num\") ;"
fi


else
    echo "!!!OOPS you entered customer name already available!!!"
    echo "---Please enter new customer name---"

i=$(($i - 1))

fi
done
done

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

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

最佳答案

使用bash数组

declare -a customer_list
customer_list=( "$(sqlite3 "$databasename".db "select cus_name from $table_name")" )


然后做

for customer  in "${cus_array[@]}"
do
 if [[ "$c_name" != "$customer" ]]
 then
 ..
 fi
done

09-11 20:29