[英]How to do sqlplus in bash if else?
I am trying to write a bash function which should delete some entries from table1 if the user enters 1, otherwise remove different entries. 我正在尝试编写一个bash函数,如果用户输入1,该函数应从table1中删除一些条目,否则删除其他条目。 My function looks like this: 我的函数如下所示:
function reset_db
{
if [ $usr_input == 1 ]
then
sqlplus -s $USR/$pwd@$SID << EOF
delete from table1 where component = 'ABC';
delete from table2 where component = 'ABC';
exit
EOF
else if [ $usr_input == 2 ]
delete from table1 where component = 'XYZ';
delete from table2 where component = 'XYZ';
exit
EOF
fi
}
I am getting error: syntax error near unexpected token `fi' 我收到错误消息: 意外令牌'fi'附近的语法错误
I am sure that it is happening because I am using if-else incorrectly somewhere but not able to figure out a way to fix it. 我确信它正在发生,因为我在某个地方错误地使用了if-else,但是却找不到解决它的方法。
Also please let me know how can I post code under the same thread if I have any more follow up questions. 另外,如果还有其他后续问题,请告诉我如何在同一线程下发布代码。
您的“ else if”是错误的,正确的语法是“ elif”。
You need to repeat the command in each clause of the if
statement: 您需要在if
语句的每个子句中重复该命令:
function reset_db
{
if [ $usr_input == 1 ]
then
sqlplus -s $USR/$pwd@$SID << EOF
delete from table1 where component = 'ABC';
delete from table2 where component = 'ABC';
exit
EOF
elif [ $usr_input == 2 ]; then
sqlplus -s $USR/$pwd@$SID << EOF
delete from table1 where component = 'XYZ';
delete from table2 where component = 'XYZ';
exit
EOF
fi
}
As a simplification, you should refactor this: 为简化起见,您应该重构此代码:
reset_db () {
if [[ $usr_input = 1 ]]; then
to_delete='ABC'
elif [[ $usr_input = 2 ]]; then
to_delete='XYZ'
else
return
fi
sqlplus -s "$USR/$pwd@$SID" <<EOF
delete from table1 where component = '$to_delete'
delete from table2 where component = '$to_delete'
exit
EOF
fi
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.