我有一个产生以下输出的脚本:
[RESPONSE] code = 211 description = Domain name not available queuetime = 0 runtime = 0.009
我只需要将code = 211
部分分配给变量。我该怎么做?
答案 0 :(得分:2)
如果您希望将code = 211
存储到变量中,请说var
执行此操作:
$ var="$(./your_script | sed -E 's/.*(code = [[:digit:]]+).*/\1/')"
$ echo "$var"
code = 211
相反,如果您希望在给定脚本结果的情况下将211分配给code
:
$ eval "$(./your_script | sed -E 's/.*(code = [[:digit:]]+).*/\1/;s/ //g')"
$ echo "$code"
211
备注强>
s/.*(code = [[:digit:]]+).*/
保留结果的code = number
部分s/ //g
删除=
周围的空格,以便将code=number
传递给eval
答案 1 :(得分:1)
#Assuming there is space between [response] and code.Seems there is from your example.
var=$(./yourScript.sh |awk '{print $2,$3,$4}')
echo $var
code = 211