注意: 在一个脚本中,使用一个后台运行的命令(使用&)可能会使这个脚本挂起,直到敲回车,挂起才会被恢复.看起来只有这个命令的结果需要输出到stdout 的时候才会发
生这种现象.这会是一个很烦人的现象.
1 #!/bin/bash
2 # test.sh
3
4 ls -l &
5 echo "Done."
如果把这个后台运行命令的输出重定向到文件中或者重定向到/dev/null 中,也能解决这个问题.
4 ls -l &>/dev/null
#!/bin/bash
ARGS=2 # Two args to script expected.
E_BADARGS=65
E_UNREADABLE=66
if [ $# -ne "$ARGS" ]
then
echo "Usage: `basename $0` file1 file2"
exit $E_BADARGS
fi
if [[ ! -r "$1" || ! -r "$2" ]]
then
echo "Both files to be compared must exist and be readable."
exit $E_UNREADABLE
fi
cmp $1 $2 &> /dev/null # /dev/null buries the output of the "cmp" command.
# cmp -s $1 $2 has same result ("-s" silent flag to "cmp")
# Thank you Anders Gustavsson for pointing this out.
#
# Also works with 'diff', i.e., diff $1 $2 &> /dev/null
if [ $? -eq 0 ] # Test exit status of "cmp" command.
then
echo "File \"$1\" is identical to file \"$2\"."
else
echo "File \"$1\" differs from file
$? 是对 cmp $1 $2 &> /dev/null 是的返回值