【发布时间】:2018-11-04 16:27:14
【问题描述】:
我正在尝试以参数方式控制命令的错误输出,但管道命令被作为另一个参数处理。下面的测试脚本;
...$ cat printOutput.sh
#!/bin/sh
if [ $# -gt 0 ]; then echo "Wrong parameter $1"; exit; fi
echo "stdout"
echo "errout" >&2
...$ cat test.sh
#!/bin/sh
cmdBase="./printOutput.sh"
if [ -z $1 ]; then
#Do not pipe
cmd="$cmdBase"
else
#Pipe err
cmd="$cmdBase 2>/dev/null"
fi
echo "`$cmd`"
只有在选择了 --verbose 选项时才会打印错误输出,但无论如何都会打印。测试脚本显示 2>/dev/null 管道被作为参数处理。
...$ ./test.sh --verbose
Wrong parameter 2>/dev/null
...$ sh -x ./test.sh --verbose
+ cmdBase=./printOutput.sh
+ [ -z --verbose ]
+ cmd=./printOutput.sh 2>/dev/null
+ ./printOutput.sh 2>/dev/null
+ echo Wrong parameter 2>/dev/null
Wrong parameter 2>/dev/null
为什么/如何在这里将管道作为参数处理?
【问题讨论】: