简体   繁体   English

命令替换,bash和管道出错

[英]Error with command substitution, bash and pipes

Using CentOS 6.4 使用CentOS 6.4

Executing: 执行:

var1=$(softnas-cmd checkupdate | jq .success)

give me an error: 给我一个错误:

"parse error: Invalid numeric literal at line 1, column 9"

if I execute var1=$(softnas-cmd checkupdate) followed by echo $var1 I get: 如果我执行var1=$(softnas-cmd checkupdate)然后执行echo $var1我得到:

{"success" : true, "session_id" : 1935, "result" : {"success":true,"msg":"You are running the latest version","records":{"version":"2.1.3.el6.x86_64","newversion":"2.1.3.el6.x86_64","updateavailable":false,"msg":"You are running the latest version"},"total":4}}

if I then execute var2=$(echo $var1 | jq .success) this works and I get the expected: true 如果然后执行var2=$(echo $var1 | jq .success)则可以正常工作,并且得到了预期的结果:true

ultimately I want to do this: 最终我想这样做:

if [ $(softnas-cmd checkupdate | jq .succcess) = "true" ]; then
   echo "update required"
   ...
fi;

can anyone tell me how to format the command substitution so I can execute in one shot? 谁能告诉我如何设置命令替换的格式,以便一次执行?

Thanks 谢谢

Are you trying to parse the true/false after the string "updatevailable"? 您是否要在字符串“ updatevailable”之后解析true / false? If so, try this: 如果是这样,请尝试以下操作:

res=`softnas-cmd checkupdate | sed -n "s/.*updateavailable\":\([tf][a-z]*\),.*/\1/p"`; 
if [ "$res" == "true" ]; then 
    echo "update required" ... 
fi;

if you are, however, trying to parse the true/false after the first "success" string, then: 但是,如果您尝试在第一个“成功”字符串之后解析true / false,则:

res=`softnas-cmd checkupdate | sed -n "s/.*\"success\" : \([tf][a-z]*\), .*/\1/p"`; 
if [ "$res" == "true" ]; then 
    echo "update required" ... 
fi;

and parsing the second "success" string would be: 并解析第二个“成功”字符串为:

res=`softnas-cmd checkupdate | sed -n "s/.*\"success\":\([tf][a-z]*\), .*/\1/p"`; 
if [ "$res" == "true" ]; then 
    echo "update required" ... 
fi;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM