我从book/book/ex28.html读取了以下代码
echo "Running unit tests:"
for i in tests/*_tests
do
if test -f $i
then
if $VALGRIND ./$i 2>> tests/tests.log
then
echo $i PASS
else
echo "ERROR in test $i: here's tests/tests.log"
echo "------"
tail tests/tests.log
exit 1
fi
fi
done
echo ""操作符"2>>“在shell脚本中做什么?
另外,在我问这个问题之前,我已经在命令行上测试了它,这就是我得到的:
~/Projects/test> if 0 2>> hello.txt; then echo "0"; else echo "?"; fi
?
~/Projects/test> if 1 2>> hello.txt; then echo "0"; else echo "?"; fi
?
~/Projects/test> if 108230284 2>> hello.txt; then echo "0"; else echo "?"; fi
?
~/Projects/test> if 0 2>> hello.txt; then echo "0"; else echo "?"; fi
?
~/Projects/test> cat hello.txt
-bash: 0: command not found
-bash: 1: command not found
-bash: 108230284: command not found
-bash: 0: command not found发布于 2014-02-21 06:11:14
>>操作符通常允许输出重定向到文件并附加到文件中,而不是替换它(>操作符替换)。
任何重定向操作符前面的数字定义要重定向的流号,在本例中是stderr。
因此,some-command 2>> file将stderr-outputs of some-command附加到文件file。
顺便说一句:2>>&1将stderr的内容重定向到与stdout相同的流,这通常用于在一个管道命令中看到两个流:
some-command 2>&1 | less这将将stderr和stdout输送到less,因为2>&1在stderr中混合到stdout流,然后通过管道传输到less。
示例中的if与2>>无关,它将简单地计算valgrind的返回值。
https://stackoverflow.com/questions/21926494
复制相似问题