亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將stdout的COPY重定向到bash腳本本身的日志文件

將stdout的COPY重定向到bash腳本本身的日志文件

函數式編程 2019-08-23 16:01:31
將stdout的COPY重定向到bash腳本本身的日志文件我知道如何將stdout重定向到文件:exec > foo.log echo test這會將'test'放入foo.log文件中。現在我想將輸出重定向到日志文件并將其保存在stdout上即它可以從腳本外部輕松完成:script | tee foo.log但我想在腳本本身內聲明它我試過了exec | tee foo.log但它不起作用。
查看完整描述

3 回答

?
慕萊塢森

TA貢獻1810條經驗 獲得超4個贊

busybox,macOS bash和非bash shell的解決方案


接受的答案肯定是bash的最佳選擇。我在沒有訪問bash的Busybox環境中工作,并且它不理解exec > >(tee log.txt)語法。它也無法exec >$PIPE正常工作,嘗試創建一個與命名管道同名的普通文件,該文件失敗并掛起。


希望這對沒有bash的其他人有用。


此外,對于使用命名管道的任何人來說,它是安全的rm $PIPE,因為它取消了管道與VFS的鏈接,但使用它的進程仍然保持引用計數,直到它們完成。


注意$ *的使用不一定安全。


#!/bin/sh


if [ "$SELF_LOGGING" != "1" ]

then

    # The parent process will enter this branch and set up logging


    # Create a named piped for logging the child's output

    PIPE=tmp.fifo

    mkfifo $PIPE


    # Launch the child process with stdout redirected to the named pipe

    SELF_LOGGING=1 sh $0 $* >$PIPE &


    # Save PID of child process

    PID=$!


    # Launch tee in a separate process

    tee logfile <$PIPE &


    # Unlink $PIPE because the parent process no longer needs it

    rm $PIPE    


    # Wait for child process, which is running the rest of this script

    wait $PID


    # Return the error code from the child process

    exit $?

fi


# The rest of the script goes here


查看完整回答
反對 回復 2019-08-23
  • 3 回答
  • 0 關注
  • 664 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號