我想检查所有具有相应给定值的客户名称值(c_name) [英] i want to check the all customer name values with the respective given value(c_name)

查看:45
本文介绍了我想检查所有具有相应给定值的客户名称值(c_name)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的脚本中,客户名称在for循环中未正确比较.请解决我的问题.{#!/bin/bash

In my script, the Customer name is not compared correctly in the for loop.please fix my issue. { #!/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
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


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



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

c_domain="$c_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))

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

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;"}

输出1:

mahendranatarajan @ amachu-Inspiron-3558:〜$ ./new2.sh

mahendranatarajan@amachu-Inspiron-3558:~$ ./new2.sh

-输入数据库名称---

--- Enter the Database name ---

演示

-输入表名---

demo_table

demo_table

-输入您想要的客户记录总数---

--- Enter the total number of customer records do you want ---

3

---逐一输入以下详细信息---

--- Enter the following details one by one---

输入1个客户详细信息

---输入客户名称---

---Enter the customer name---

ABC

---输入状态(有效/无效)---

---Enter the Status(Active/Inactive)---

有效

输入2个客户详细信息

---输入客户名称---

---Enter the customer name---

ABC

!!!糟糕,您输入的客户名称已经可用!!!

!!!OOPS you entered customer name already available!!!

---请输入新的客户名称---

---Please enter new customer name---

输入2个客户详细信息

---输入客户名称---

---Enter the customer name---

BCD

---输入状态(有效/无效)---

---Enter the Status(Active/Inactive)---

有效

输入3个客户详细信息

---输入客户名称---

---Enter the customer name---

ABC

---输入状态(有效/无效)---

---Enter the Status(Active/Inactive)---

有效

---来自demo_table的记录---

--- Records from the demo_table ---

1 | ABC | abc.in |活动| 8081

1|ABC|abc.in|Active|8081

2 | BCD | bcd.in | Active | 8082

2|BCD|bcd.in|Active|8082

mahendranatarajan @ amachu-Inspiron-3558:〜$

mahendranatarajan@amachu-Inspiron-3558:~$

在这里,我将第一个值设置为"ABC",然后将第二个值设置为"ABC",然后正确显示警告消息.但是以下输出中,我将第一个值指定为"ABC",然后将第二个值指定为"BCD",然后将第三个值指定为"ABC",但我无法正确获取警告消息.对于该问题,我该怎么办..?

here I give first value as "ABC" then second value as "ABC" then it shows the warning message correctly. but the following output I give first value as "ABC" then second value as "BCD" then third value as "ABC" but I cant get the warning message properly.what can I do for that issue..?

输出2:

mahendranatarajan @ amachu-Inspiron-3558:〜$ ./new2.sh

mahendranatarajan@amachu-Inspiron-3558:~$ ./new2.sh

-输入数据库名称---

--- Enter the Database name ---

演示

-输入表名---

demo_table

demo_table

-输入您想要的客户记录总数---

--- Enter the total number of customer records do you want ---

3

---逐一输入以下详细信息---

--- Enter the following details one by one---

输入1个客户详细信息

---输入客户名称---

---Enter the customer name---

ABC

---输入状态(有效/无效)---

---Enter the Status(Active/Inactive)---

有效

输入2个客户详细信息

---输入客户名称---

---Enter the customer name---

BCD

---输入状态(有效/无效)---

---Enter the Status(Active/Inactive)---

有效

输入3个客户详细信息

---输入客户名称---

---Enter the customer name---

ABC

---输入状态(有效/无效)---

---Enter the Status(Active/Inactive)---

有效

---来自demo_table的记录---

--- Records from the demo_table ---

1 | ABC | abc.in |活动| 8081

1|ABC|abc.in|Active|8081

2 | BCD | bcd.in | Active | 8082

2|BCD|bcd.in|Active|8082

mahendranatarajan @ amachu-Inspiron-3558:〜$

mahendranatarajan@amachu-Inspiron-3558:~$

推荐答案

循环的问题是查询从$ table_name;中选择cus_name;" .这是选择表中的所有cus_name并填充数组.在上面的示例中,当您输入第二个客户之后再次提供第一个客户名称时,您的查询正在获取以下结果

The problem with your loop is the query "select cus_name from $table_name;". This is selecting all the cus_name in the table and populating the array. In your above example when you are providing the first customer name again after entering the second customer, your query is fetching the following result

++'从demo_table中选择cus_name'+客户='ABCBCD'这就是您的if条件失败的原因

++ 'select cus_name from demo_table' + customer='ABC BCD' This is why your if condition fails

+ [[ABC!= \ A \ B \ C \\ B \ C \ D]]

要解决问题,请将查询更改为

To fix your problem change your query to below

从$ table_name中选择cus_name,其中cus_name类似于'$ c_name';")

这将检查数据库中是否存在该客户,以及该客户是否存在并返回客户名称.如果存在ABC,则它将返回ABC,并且您的if条件将正常运行.

This will check if the customer exists in the DB and if the customer exists and will return the customer name. if ABC exists then it will return ABC and your if condition will work correctly.

这篇关于我想检查所有具有相应给定值的客户名称值(c_name)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆