当用户想要关闭它时,从bash脚本退出STDIN

时间:2017-05-09 12:23:40

标签: linux bash terminal stdin

我正在使用bash脚本自动创建文件。我生成了一个文件rc_notes.txt,它有来自两个标签的提交消息,并希望在新文件中将其重新写为rc_device.txt

我希望用户编写客户发行说明并退出我在终端中提示的BASH STDIN

我的脚本中的问题是我无法捕获文件的结尾。

想知道怎么做。我不想捕捉近距离信号。我想输入魔术字符串示例: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手动退出。我不想那样做。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

在&#34; Done&#34;已输入

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
相关问题