Bash 脚本 - 带引号的命令中的 If 语句
在 bash 脚本中,我通过“psql -c”运行 sql 查询。根据 bash 脚本的争论,select 命令的 where 子句将有所不同。所以基本上我需要知道是否可以做这样的事情:
psql -c "select statement here until we get to the where clause at which point we break out of statement and do"
if (arg1 was given)
concatenate "where arg1" to the end of the above select statement
if (arg2 was given)
concatenate "where arg2" to the end of the above select statement
等等对于尽可能多的参数。我知道如果我只传递参数,我可以在 sql 函数中更轻松地完成此操作,但这确实不是一个选项。谢谢!
编辑:发布此内容后 5 秒,我意识到我可以在调用 psql 命令之前创建一个字符串,然后对其调用 psql 命令。哎哟!
Within a bash script I am running a sql query via 'psql -c '. Based off of the arguements given to the bash script, the where claus of the select command will be different. So basically I need to know if its possible to do something like this:
psql -c "select statement here until we get to the where clause at which point we break out of statement and do"
if (arg1 was given)
concatenate "where arg1" to the end of the above select statement
if (arg2 was given)
concatenate "where arg2" to the end of the above select statement
and so on for as many arguments. I know I could do this much easier in a sql function if I just passed the arguments but that really isnt an option. Thanks!
Edit: 5 seconds after posting this I realize I could just create a string before calling the psql command and then call the psql command on that. Doh!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这使用“使用替代值”替换 -
${VAR:+alternate}
- 如果设置了$VAR
且未设置,则替换alternate
空的。如果$VAR
为空,则不会替换任何内容。This uses the "use alternate value" substitution -
${VAR:+alternate}
- wherealternate
is substituted if$VAR
is set and not empty. If$VAR
is empty, nothing will be substituted.保存脚本,例如query.sh:
这样调用
Save ascript, e.g. query.sh:
Call it like