【发布时间】:2013-09-13 16:29:42
【问题描述】:
如果您在 KornShell 脚本中传入一个参数,并且它也是一个类似的命令:
ksh argument.ksh "wc -l"
您将如何在脚本中执行此命令?您是否将其存储在变量中,然后执行它?另外,有没有办法通过在脚本中执行命令来检索标准输出/标准错误?
【问题讨论】:
标签: bash shell unix terminal ksh
如果您在 KornShell 脚本中传入一个参数,并且它也是一个类似的命令:
ksh argument.ksh "wc -l"
您将如何在脚本中执行此命令?您是否将其存储在变量中,然后执行它?另外,有没有办法通过在脚本中执行命令来检索标准输出/标准错误?
【问题讨论】:
标签: bash shell unix terminal ksh
把它放在你的 argument.ksh 脚本中:
echo "Running command $1." ## optional message
eval "$1" ## evaluate "$1" as a whole new command
实际上更好或更安全的方法是使用"$@":
echo "Running command $*." ## optional message
"$@"
然后像这样传递你的论点:
ksh argument.ksh wc -l
【讨论】:
VAR=$(command),例如VAR=$("$@")