正在执行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

@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

相关内容

  • 没有找到相关文章

最新更新