第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

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

3 回答

?
慕萊塢森

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

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


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


希望這對(duì)沒(méi)有bash的其他人有用。


此外,對(duì)于使用命名管道的任何人來(lái)說(shuō),它是安全的rm $PIPE,因?yàn)樗∠斯艿琅cVFS的鏈接,但使用它的進(jìn)程仍然保持引用計(jì)數(shù),直到它們完成。


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


#!/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


查看完整回答
反對(duì) 回復(fù) 2019-08-23
  • 3 回答
  • 0 關(guān)注
  • 659 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)