1

我遇到了一个我不知道原因的问题。我想最简单的解释方法是通过代码示例:

test ()
{
    echo "This will be printed to the file"

    #But the output of rsync will not
    rsync -av /path/A /path/B

    echo "This will be printed to the file"
}

function_calling_test ()
{
    test | tee -a "file_B.txt"
}

function_calling_test | tee -a "file_A.txt"

在上面的示例中,file_A.txt将包含echo输出和函数"test"的rsync输出,但file_B.txt将仅包含echo输出。为什么是这样?

4

1 回答 1

1

您需要将 stderr 输出添加到流中

mytest ()
{
    echo "This will be printed to the file"

    #But the output of rsync will not
    rsync -av /path/A /path/B 2>&1
    # -----------------------^^^^^^

    echo "This will be printed to the file"
}

test是一个可用于作为 unix/Linux OS 一部分的所有 unix shell 的命令。不要将您的功能命名为简单的测试,您正在为事故做好准备!;-)

我希望这有帮助。

于 2012-01-30T18:59:46.267 回答