首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >操作符"2>>“在shell脚本中做什么?

操作符"2>>“在shell脚本中做什么?
EN

Stack Overflow用户
提问于 2014-02-21 06:02:34
回答 1查看 971关注 0票数 2

我从book/book/ex28.html读取了以下代码

代码语言:javascript
复制
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脚本中做什么?

另外,在我问这个问题之前,我已经在命令行上测试了它,这就是我得到的:

代码语言:javascript
复制
~/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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-21 06:11:14

>>操作符通常允许输出重定向到文件并附加到文件中,而不是替换它(>操作符替换)。

任何重定向操作符前面的数字定义要重定向的流号,在本例中是stderr

因此,some-command 2>> filestderr-outputs of some-command附加到文件file

顺便说一句:2>>&1stderr的内容重定向到与stdout相同的流,这通常用于在一个管道命令中看到两个流:

代码语言:javascript
复制
some-command 2>&1 | less

这将将stderrstdout输送到less,因为2>&1stderr中混合到stdout流,然后通过管道传输到less

示例中的if2>>无关,它将简单地计算valgrind的返回值。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21926494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档