在命令行中将参数传递给 ksh 脚本时转义反引号 - grave(`)
escape backticks - grave( ` ) when passing a argument to a ksh script in command line
我写了一个方便的 ksh shell 脚本,它读取输出 firstcommand
,从 grep 执行一些过滤,并将结果值提供给 othercommand
handy.ksh
#!/usr/bin/ksh
othercommand `firstcommand | grep 'keyword' | cut -d ' ' -f 1`
效果很好。
但是,我想通过命令行传递我的“关键字”。通常我可以使用 $1
>handy.ksh TEST
在脚本中 $1 将被测试
然而,我无法逃脱坟墓,$1 是按字面解释的。
如何转义 ` ?
您可以使用 $()
代替反引号:
#!/usr/bin/ksh
# test.ksh
echo $(echo "foo bar" | grep "" | cut -d ' ' -f 1)
现在您可以这样称呼它:
ksh test.ksh foo
我写了一个方便的 ksh shell 脚本,它读取输出 firstcommand
,从 grep 执行一些过滤,并将结果值提供给 othercommand
handy.ksh
#!/usr/bin/ksh
othercommand `firstcommand | grep 'keyword' | cut -d ' ' -f 1`
效果很好。 但是,我想通过命令行传递我的“关键字”。通常我可以使用 $1
>handy.ksh TEST
在脚本中 $1 将被测试 然而,我无法逃脱坟墓,$1 是按字面解释的。
如何转义 ` ?
您可以使用 $()
代替反引号:
#!/usr/bin/ksh
# test.ksh
echo $(echo "foo bar" | grep "" | cut -d ' ' -f 1)
现在您可以这样称呼它:
ksh test.ksh foo