exit from STDIN from bash script when the user want to close it

有些话、适合烂在心里 提交于 2021-01-28 07:47:55

问题


I'm automating the file creation from a bash script. I generated a file rc_notes.txt which has commit messages from two tags and want to re-write that in a new file as rc_device.txt.

I want the user to write the customer release notes and exit from the BASH STDIN that I prompt in the terminal.

The problem in my script is I'm not able to trap the close of file.

Wondering how to do. I don't want to trap the close signal. I want to enter magic string example: Done or some string that triggers the closure of STDIN, that exit from the script gracefully.


My script:

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

It does create the file but I need to exit manually by entering ctrl+D or ctrl+z. I don't want to do that. Any suggestions?


回答1:


To break the loop when "Done" is entered

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

or

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


来源:https://stackoverflow.com/questions/43869760/exit-from-stdin-from-bash-script-when-the-user-want-to-close-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!