执行shell脚本中内置的命令字符串

发布于 2025-01-10 22:22:31 字数 3299 浏览 0 评论 0原文

我正在尝试在 for 循环中构建命令字符串并执行它。 然而这并没有按计划进行。
事实上,运行注释中的最后两行似乎确实有效。 我该如何解决这个问题?如何以正确的方式构建命令字符串?

非常感谢任何帮助!

#!/bin/sh
JSONQUERYFILE="ReportingQueries.json"
JQCOMMAND="/C/gitBashCmd/jq-win64.exe"

for i in $($JQCOMMAND -c -r '.FilterQueries[].Id' $JSONQUERYFILE) 
    do
        #remove weird chars from $i...      
        forId=$(echo $i | tr -d '\r')
        jqFilterString=".FilterQueries[] | if .Id == \"$forId\" then \"YES\" else \"NOPE\" end"
        echo $jqFilterString 
        commandString="$JQCOMMAND -c -r '"$jqFilterString"' $JSONQUERYFILE"

        echo $commandString
        $($commandString)
#       echo /C/gitBashCmd/jq-win64.exe -c -r '.FilterQueries[] | if .Id == "274005" then "YES" else "NOPE" end' ReportingQueries.json
#       /C/gitBashCmd/jq-win64.exe -c -r '.FilterQueries[] | if .Id == "274005" then "YES" else "NOPE" end' ReportingQueries.json
done

注释中最后两行的输出似乎工作正常:(jq-win64 命令自行迭代)

.FilterQueries[] | if .Id == "274301" then "YES" else "NOPE" end
/C/gitBashCmd/jq-win64.exe -c -r .FilterQueries[] | if .Id == "274005" then "YES" else "NOPE" end ReportingQueries.json
NOPE
YES
NOPE
NOPE
.FilterQueries[] | if .Id == "274005" then "YES" else "NOPE" end
/C/gitBashCmd/jq-win64.exe -c -r .FilterQueries[] | if .Id == "274005" then "YES" else "NOPE" end ReportingQueries.json
NOPE
YES
NOPE
NOPE
.FilterQueries[] | if .Id == "274004" then "YES" else "NOPE" end
/C/gitBashCmd/jq-win64.exe -c -r .FilterQueries[] | if .Id == "274005" then "YES" else "NOPE" end ReportingQueries.json
NOPE
YES
NOPE
NOPE
.FilterQueries[] | if .Id == "274000" then "YES" else "NOPE" end
/C/gitBashCmd/jq-win64.exe -c -r .FilterQueries[] | if .Id == "274005" then "YES" else "NOPE" end ReportingQueries.json
NOPE
YES
NOPE
NOPE

如果我们使用构建的命令字符串运行脚本,则输出如下:

.FilterQueries[] | if .Id == "274301" then "YES" else "NOPE" end
/C/gitBashCmd/jq-win64.exe -c -r '.FilterQueries[] | if .Id == "274301" then "YES" else "NOPE" end' ReportingQueries.json
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Windows cmd shell quoting issues?) at <top-level>, line 1:
'.FilterQueries[]
jq: 1 compile error
.FilterQueries[] | if .Id == "274005" then "YES" else "NOPE" end
/C/gitBashCmd/jq-win64.exe -c -r '.FilterQueries[] | if .Id == "274005" then "YES" else "NOPE" end' ReportingQueries.json
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Windows cmd shell quoting issues?) at <top-level>, line 1:
'.FilterQueries[]
jq: 1 compile error
.FilterQueries[] | if .Id == "274004" then "YES" else "NOPE" end
/C/gitBashCmd/jq-win64.exe -c -r '.FilterQueries[] | if .Id == "274004" then "YES" else "NOPE" end' ReportingQueries.json
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Windows cmd shell quoting issues?) at <top-level>, line 1:
'.FilterQueries[]
jq: 1 compile error
.FilterQueries[] | if .Id == "274000" then "YES" else "NOPE" end
/C/gitBashCmd/jq-win64.exe -c -r '.FilterQueries[] | if .Id == "274000" then "YES" else "NOPE" end' ReportingQueries.json
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Windows cmd shell quoting issues?) at <top-level>, line 1:
'.FilterQueries[]
jq: 1 compile error

I'm trying to build a command string in a for loop and execute it.
However that doesn't go as planned.
While in fact, running last 2 lines in comment do seem to work.
How do I get this solved? How do you build a command string the right way?

Any help is much appreciated!

#!/bin/sh
JSONQUERYFILE="ReportingQueries.json"
JQCOMMAND="/C/gitBashCmd/jq-win64.exe"

for i in $($JQCOMMAND -c -r '.FilterQueries[].Id' $JSONQUERYFILE) 
    do
        #remove weird chars from $i...      
        forId=$(echo $i | tr -d '\r')
        jqFilterString=".FilterQueries[] | if .Id == \"$forId\" then \"YES\" else \"NOPE\" end"
        echo $jqFilterString 
        commandString="$JQCOMMAND -c -r '"$jqFilterString"' $JSONQUERYFILE"

        echo $commandString
        $($commandString)
#       echo /C/gitBashCmd/jq-win64.exe -c -r '.FilterQueries[] | if .Id == "274005" then "YES" else "NOPE" end' ReportingQueries.json
#       /C/gitBashCmd/jq-win64.exe -c -r '.FilterQueries[] | if .Id == "274005" then "YES" else "NOPE" end' ReportingQueries.json
done

The output of the 2 last lines in comment seems to work fine: (the jq-win64 command iterates on itself)

.FilterQueries[] | if .Id == "274301" then "YES" else "NOPE" end
/C/gitBashCmd/jq-win64.exe -c -r .FilterQueries[] | if .Id == "274005" then "YES" else "NOPE" end ReportingQueries.json
NOPE
YES
NOPE
NOPE
.FilterQueries[] | if .Id == "274005" then "YES" else "NOPE" end
/C/gitBashCmd/jq-win64.exe -c -r .FilterQueries[] | if .Id == "274005" then "YES" else "NOPE" end ReportingQueries.json
NOPE
YES
NOPE
NOPE
.FilterQueries[] | if .Id == "274004" then "YES" else "NOPE" end
/C/gitBashCmd/jq-win64.exe -c -r .FilterQueries[] | if .Id == "274005" then "YES" else "NOPE" end ReportingQueries.json
NOPE
YES
NOPE
NOPE
.FilterQueries[] | if .Id == "274000" then "YES" else "NOPE" end
/C/gitBashCmd/jq-win64.exe -c -r .FilterQueries[] | if .Id == "274005" then "YES" else "NOPE" end ReportingQueries.json
NOPE
YES
NOPE
NOPE

While if we run the script with the built up command string the output is the following:

.FilterQueries[] | if .Id == "274301" then "YES" else "NOPE" end
/C/gitBashCmd/jq-win64.exe -c -r '.FilterQueries[] | if .Id == "274301" then "YES" else "NOPE" end' ReportingQueries.json
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Windows cmd shell quoting issues?) at <top-level>, line 1:
'.FilterQueries[]
jq: 1 compile error
.FilterQueries[] | if .Id == "274005" then "YES" else "NOPE" end
/C/gitBashCmd/jq-win64.exe -c -r '.FilterQueries[] | if .Id == "274005" then "YES" else "NOPE" end' ReportingQueries.json
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Windows cmd shell quoting issues?) at <top-level>, line 1:
'.FilterQueries[]
jq: 1 compile error
.FilterQueries[] | if .Id == "274004" then "YES" else "NOPE" end
/C/gitBashCmd/jq-win64.exe -c -r '.FilterQueries[] | if .Id == "274004" then "YES" else "NOPE" end' ReportingQueries.json
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Windows cmd shell quoting issues?) at <top-level>, line 1:
'.FilterQueries[]
jq: 1 compile error
.FilterQueries[] | if .Id == "274000" then "YES" else "NOPE" end
/C/gitBashCmd/jq-win64.exe -c -r '.FilterQueries[] | if .Id == "274000" then "YES" else "NOPE" end' ReportingQueries.json
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Windows cmd shell quoting issues?) at <top-level>, line 1:
'.FilterQueries[]
jq: 1 compile error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

勿忘初心 2025-01-17 22:22:31

@dan @ikegami 非常感谢你的提示--arg。
这是更好的版本:

#!/bin/sh
JSONQUERYFILE="ReportingQueries.json"
JQCOMMAND="/C/gitBashCmd/jq-win64.exe"

for i in $($JQCOMMAND -c -r '.FilterQueries[].Id' $JSONQUERYFILE) 
    do  
        forId=$(echo $i | tr -d '\r')         #remove weird chars from $i...   
        echo $JQCOMMAND -c -r --arg idToSearch "$forId" '.FilterQueries[] | if .Id == $idToSearch then "YES" else "NOPE" end' $JSONQUERYFILE 
        $JQCOMMAND -c -r --arg idToSearch "$forId" '.FilterQueries[] | if .Id == $idToSearch then "YES" else "NOPE" end' $JSONQUERYFILE
done

它会产生以下输出:

/C/gitBashCmd/jq-win64.exe -c -r --arg idToSearch 274301 .FilterQueries[] | if .Id == $idToSearch then "YES" else "NOPE" end ReportingQueries.json
YES
NOPE
NOPE
NOPE
/C/gitBashCmd/jq-win64.exe -c -r --arg idToSearch 274005 .FilterQueries[] | if .Id == $idToSearch then "YES" else "NOPE" end ReportingQueries.json
NOPE
YES
NOPE
NOPE
/C/gitBashCmd/jq-win64.exe -c -r --arg idToSearch 274004 .FilterQueries[] | if .Id == $idToSearch then "YES" else "NOPE" end ReportingQueries.json
NOPE
NOPE
YES
NOPE
/C/gitBashCmd/jq-win64.exe -c -r --arg idToSearch 274000 .FilterQueries[] | if .Id == $idToSearch then "YES" else "NOPE" end ReportingQueries.json
NOPE
NOPE
NOPE
YES

@dan @ikegami Thank you SO much for the hint --arg.
This is the better version:

#!/bin/sh
JSONQUERYFILE="ReportingQueries.json"
JQCOMMAND="/C/gitBashCmd/jq-win64.exe"

for i in $($JQCOMMAND -c -r '.FilterQueries[].Id' $JSONQUERYFILE) 
    do  
        forId=$(echo $i | tr -d '\r')         #remove weird chars from $i...   
        echo $JQCOMMAND -c -r --arg idToSearch "$forId" '.FilterQueries[] | if .Id == $idToSearch then "YES" else "NOPE" end' $JSONQUERYFILE 
        $JQCOMMAND -c -r --arg idToSearch "$forId" '.FilterQueries[] | if .Id == $idToSearch then "YES" else "NOPE" end' $JSONQUERYFILE
done

And it produces the following output:

/C/gitBashCmd/jq-win64.exe -c -r --arg idToSearch 274301 .FilterQueries[] | if .Id == $idToSearch then "YES" else "NOPE" end ReportingQueries.json
YES
NOPE
NOPE
NOPE
/C/gitBashCmd/jq-win64.exe -c -r --arg idToSearch 274005 .FilterQueries[] | if .Id == $idToSearch then "YES" else "NOPE" end ReportingQueries.json
NOPE
YES
NOPE
NOPE
/C/gitBashCmd/jq-win64.exe -c -r --arg idToSearch 274004 .FilterQueries[] | if .Id == $idToSearch then "YES" else "NOPE" end ReportingQueries.json
NOPE
NOPE
YES
NOPE
/C/gitBashCmd/jq-win64.exe -c -r --arg idToSearch 274000 .FilterQueries[] | if .Id == $idToSearch then "YES" else "NOPE" end ReportingQueries.json
NOPE
NOPE
NOPE
YES

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