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