gpt4 book ai didi

linux - 在命令行中将参数传递给 ksh 脚本时转义反引号 - grave(`)

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:04:18 27 4
gpt4 key购买 nike

我写了一个方便的 ksh shell 脚本,它读取 firstcommand 的输出,从 grep 执行一些过滤,并将结果值提供给 othercommand

handy.ksh
#!/usr/bin/ksh
othercommand `firstcommand | grep 'keyword' | cut -d ' ' -f 1`

它工作得很好。但是,我想通过命令行传递我的“关键字”。通常我可以使用 $1

>handy.ksh TEST

在脚本中 $1 将是 TEST然而,我无法逃脱坟墓,$1 是按字面解释的。

如何转义 ` ?

最佳答案

您可以使用 $() 代替反引号:

#!/usr/bin/ksh
# test.ksh
echo $(echo "foo bar" | grep "$1" | cut -d ' ' -f 1)

现在你可以这样调用它:

ksh test.ksh foo

关于linux - 在命令行中将参数传递给 ksh 脚本时转义反引号 - grave(`),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50625932/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com