考虑以下C shell脚本:
set MYVAR = "a='str.*' -p -10"
set output = `echo "$MYVAR" | sed 's/-p -[0-9]\+/-p -100/g'`
echo "$output"
当从shell获取时,我收到错误echo: no match
。我可以对脚本进行哪些更改以避免错误?我尝试添加“:q”并使用Perl one liner但尚未找到优雅的解决方案。
答案 0 :(得分:0)
这很棘手,因为你有两个“。*”{这将为文件的全球化}和MYVAR中的单引号{这意味着你不能使用'来解释字面意思} 我能想到的最好的方法是用单引号替换MYVAR中的双引号,在这种情况下,一切都应该有效。你的sed行不起作用。但那不是问题。我取代了sed来模仿我认为你想要做的事情。
所以:
- set MYVAR = 'a="str.*" -p -10'
- echo $MYVAR
echo: No match. #as expected because you need to put it in double quotes
- echo "$MYVAR"
a="str.*" -p -10
- set output = `echo "$MYVAR" | sed 's/-p -10/-p -100/g'`
- echo $output
echo: No match. #Reason same as before
- echo "$output"
a="str.*" -p -100