2017-05-09 75 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