Find the answer to your Linux question:
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 ...
  1. #1
    Just 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:

    Code:
    read -p "Enter Name: " -e namein
    
    if [ "$namein" == "^D" ]
    then
         echo "ctrl-d detected, exiting"
         exit
    fi
    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.

    Any ideas? Thanks!

  2. #2
    tpl
    tpl is offline
    Linux 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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...