簡體   English   中英

外殼腳本,子外殼,子腳本,管道-WEIRD(?)CTRL + C信號傳播

[英]shell script, subshell, sub script, pipe - WEIRD (?) CTRL+C SIGNAL PROPAGATION

因此,我發現我的bash腳本的行為很奇怪。

這是一個測試腳本:

echo "start of script"

(

echo "start of subshell"

cat > /tmp/$$ << EOF
trap 'exit 99' SIGINT

echo "sleep 10, hit ctrl+c now"
sleep 10
EOF

chmod +x /tmp/$$
/tmp/$$

echo "end of subshell"

#)
) | tee -a /tmp/$$.log

echo "end of script"

因此,正如您所看到的,我使用創建子shell的括號輕松地將輸出用於記錄目的。

在此子外殼中,我運行一個腳本,該腳本捕獲ctrl + c信號並僅退出該腳本。

因此,根據我是否通過管道傳輸subshel​​l的輸出,在下標(睡眠10)期間按ctrl + c時的行為會有所不同。

| tee -a /tmp/$$.log ,輸出顯示:

[/tmp] ./test.sh
start of script
start of subshell
sleep 10, hit ctrl+c now
end of script

如果沒有管道和三通,輸出將顯示:

[/tmp] ./test.sh
start of script
start of subshell
sleep 10, hit ctrl+c now
**end of subshell**
end of script

有人可以解釋一下這種行為嗎? 有沒有辦法確保下標的結尾不會像管道和T形那樣被跳過?

謝謝

嘗試使用tee選項“ -i”。

(
#script
) | tee -i -a /tmp/$$.log

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM