执行shell脚本中内置的命令字符串
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@dan @ikegami 非常感谢你的提示--arg。
这是更好的版本:
它会产生以下输出:
@dan @ikegami Thank you SO much for the hint --arg.
This is the better version:
And it produces the following output: