Results 1 to 2 of 2
Hey I have a question/problem I am encountering. I need to write a bash script which will exit when a EOF is read in using the read command.
Here is ...
- 06-22-2007 #1Just Joined!
- Join Date
- Jun 2007
- Posts
- 4
Question about EOF and newline
Hey I have a question/problem I am encountering. I need to write a bash script which will exit when a EOF is read in using the read command.
Here is what I have so far:
I don't know how I can catch the ctrl-d because it is not a ascii character, and is not signal that can be trapped.Code:read -p "Enter Name: " -e namein if [ "$namein" == "^D" ] then echo "ctrl-d detected, exiting" exit fi
Any ideas? Thanks!
- 06-26-2007 #2Linux User
- Join Date
- Jan 2007
- Location
- cleveland
- Posts
- 452
welcome to the forum
if read has no input, it returns 1 ("Learning the Bash Shell," Newham &
Rosenblatt, p.172) so you could try something like this:
if read -p "Enter Name: " namein
then echo $namein
else echo -e "\nctrl-d detected, exiting"
fi
entering "^d" returns 1 (false) so the else path is taken. The "-e"
option to echo turns on interpretation of the "\n" purely for neatness.
thanks for the interesting question
the sun is new every day (heraclitus)


Reply With Quote