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"
}

testunix/Linux OS の一部であるすべての unix シェルで使用できるコマンドです。関数に単純なテストの名前を付けないでください。事故に備えることになります。;-)

これが役立つことを願っています。

于 2012-01-30T18:59:46.267 に答える