Bash 脚本 - 带引号的命令中的 If 语句

发布于 2024-11-07 16:57:48 字数 557 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

很糊涂小朋友 2024-11-14 16:57:48
psql -c "SELECT columns FROM table ${1:+WHERE $1} ${2:+WHERE $2}"

这使用“使用替代值”替换 - ${VAR:+alternate} - 如果设置了 $VAR 且未设置,则替换 alternate空的。如果 $VAR 为空,则不会替换任何内容。

psql -c "SELECT columns FROM table ${1:+WHERE $1} ${2:+WHERE $2}"

This uses the "use alternate value" substitution - ${VAR:+alternate} - where alternate is substituted if $VAR is set and not empty. If $VAR is empty, nothing will be substituted.

水波映月 2024-11-14 16:57:48

保存脚本,例如query.sh:

#!/bin/bash

query="select statement here until we get to the where clause at which point we break out of statement and do"

if [ $# -gt 0 ]
then
    query+=" where $1"
    shift
fi

while [ $# -gt 0 ]
then
    query+=" and $1"
    shift
fi

psql -c "$query"

这样调用

chmod +x ./query.sh
./query.sh "id in (1,2,3)" "modified_by='myname'"

Save ascript, e.g. query.sh:

#!/bin/bash

query="select statement here until we get to the where clause at which point we break out of statement and do"

if [ $# -gt 0 ]
then
    query+=" where $1"
    shift
fi

while [ $# -gt 0 ]
then
    query+=" and $1"
    shift
fi

psql -c "$query"

Call it like

chmod +x ./query.sh
./query.sh "id in (1,2,3)" "modified_by='myname'"
SQL="select x,y,z FROM foobar"
if [ "$1" != "" ]
then
   SQL="$SQL where $1"
fi
psql "$SQL"
SQL="select x,y,z FROM foobar"
if [ "$1" != "" ]
then
   SQL="$SQL where $1"
fi
psql "$SQL"
内心旳酸楚 2024-11-14 16:57:48
stmt="select statement here until we get to the where clause at which point we break out of statement and do"

if (( $# > 0 ))
then
    stmt="$stmt where $1"
fi
if (( $# > 1 ))
then
    stmt="$stmt where $2"
fi

psql -c "$stmt"
stmt="select statement here until we get to the where clause at which point we break out of statement and do"

if (( $# > 0 ))
then
    stmt="$stmt where $1"
fi
if (( $# > 1 ))
then
    stmt="$stmt where $2"
fi

psql -c "$stmt"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文