【发布时间】:2017-12-25 05:32:51
【问题描述】:
我正在尝试编写一个脚本:
- 提示您输入姓名
- 在sqlplus数据库中做一个
select count看是否存在 - 最终用户查看查询结果并提示询问用户是否要运行 statement2
- 如果您说是,则运行语句 2。如果没有脚本被杀死
我遇到的问题是在第一个查询运行以检查它是否存在之后,我不确定如何添加提示,询问您是否要插入数据库
到目前为止,这是我的代码:
echo "Please enter last name: 'input;"
read input
statement1="select count(*) as count from users
where fname = '"$input"'"
statement2="INSERT INTO users VALUES ('"$input"');
$ORACLE_HOME********************************
$statement1
/
quit;
Eossql
我无法弄清楚的部分是在执行语句 1 之后我希望系统询问是否应该执行语句 2。有什么建议吗?
谢谢!
编辑: 感谢 CDahn 的建议,这是我编辑的代码。该解决方案有效,但我想知道是否有更清洁的方法来执行此操作,而不是连接到 sqlplus 两次。
echo "Please enter last name: 'input;"
read input
Statement2="INSERT INTO users VALUES ('"$input"');"
output1=$($sqlplus -s User/Pass@connection <<EOF
set head off
select count (*) from users
where fname = '$input';
EXIT;
EOF
)
read -p "User $input appears $output1 times. Create user? [Y/n]" answer
if [ -z "$answer" -o "$answer" == "y" -o "$answer" == "Y" ]
then
$sqlplus -s User/Pass@connection << Eossql
set autocommit on
set head off
$Statement2
quit;
Eossql
else
echo "Not creating user"
fi
【问题讨论】:
-
您需要提出问题,然后使用 if 语句处理响应。
标签: bash shell perl sqlplus ksh