2017-05-09 72 views
1

我從bash腳本中自動創建文件。我生成了一個文件rc_notes.txt,它具有來自兩個標記的提交消息,並且想要在新文件中將其重寫爲rc_device.txt當用戶想要關閉它時,從bash腳本退出STDIN

我希望用戶編寫客戶發行說明並退出BASHSTDIN,我在終端中提示。

我的腳本中的問題是我無法捕捉到文件的關閉。

想知道該怎麼做。我不想陷入關閉信號。我想輸入魔術字符串的例子:Done或者一些觸發STDIN關閉的字符串,這些字符串會優雅地從腳本中退出。


我的腳本:

#/bin/bash 

set -e 


echo "Creating the release candiate text" 
rc_file=rc_updater_notes.txt 
echo "=========Reading the released commit message file==========" 
cat $rc_file 
echo "=========End of the commit message file==========" 

echo "Now write the release notes" 

#exec < /dev/tty 
while read line 
do 
    echo "$line" 
done < "${1:-/dev/stdin}" > rc_file.txt 

它確實創建該文件,但我需要通過輸入ctrl+Dctrl+z手動退出。我不想這樣做。有什麼建議麼?

+0

用戶如何關閉標準輸入Ctrl + C或CTRL + d? –

+0

我希望用戶輸入一個字符串爲「完成」。我想抓住這個字符串想關閉STDIN或退出。 – LethalProgrammer

回答

1

爲了打破循環的時候 「完成」 進入

while read line 
do 
    if [[ $line = Done ]]; then 
     break; 
    fi 
    echo "$line" 
done < "${1:-/dev/stdin}" > rc_file.txt 

while read line && [[ $line != Done ]] 
do 
    echo "$line" 
done < "${1:-/dev/stdin}" > rc_file.txt 
+0

這就是我要找的。我不想陷入信號。謝謝。 – LethalProgrammer