Skip to content
Advertisement

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

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?

Advertisement

Answer

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
Advertisement