terminating script with CTRL+D

Hi,

I'm trying to make a script that reads the console input and terminates with CTRL+D. It's absolutely basic but I don't know how to "read" the CTRL+D. I've tried a bunch of things like

EOT=^D
while [ "$input" != "$EOF" ] //with & without quotations
do
read input
echo $input
done
while [ "$input" != $'\x04' ] 
while [[ $input != $'\004' ]]

and quite a few others but I just can't make it work.
If anyone could tell me how to do this, I would appreciate the help.

The name for the ctrl-D signal is "eof". If you want to do anything special if/when the user sends a ctrl-D, you'll need to trap it. The syntax in ksh is

trap cmd sig1 sig2 ...
trap 'print \'You hit control-D!\'' eof

The read command returns status code 1 at EOF.

while read input
do
   echo $input
done

Jean-Pierre.

@Glenn Arndt
I am using bash and I looked through the trap sigspecs and couldn't find which signal corresponds to CTRL+D.

@aigles
I've tried the loop you suggested but the problem is that it does not output the value of $input after termination and I would like that to be seen.

Thank you for suggestions. I hope that soon I'll be done with this thing.

Ctrl-D isn't a signal. It closes stdin.

OK. Excuse me for my messed-up terminology, I'm still a beginner.
Would you mind telling me than how can I make a script that reads from stdin closes stdin when CTRL+D is entered and outputs what has been read to stdout?
I understand that I can simply do

read input
echo $input

but I have to press CTRL+D twice to close stdin in this case.

I would appreciate your help and I am sorry if my incompetence is making you nervous.

After termination (CTRL-D) the variable $input is empty.

while read input
do
   echo "Input value: $input"
done
echo "We are at EOF"

Jean-Pierre.

Jean-Pierre, I really appreciate your help. What you've suggested is very close to what I am trying to achiveve with one difference only: the input is displayed only after return hit and I want it to be displayed after CTRL+D also,
i.e. asdfasdfasdfCTRL+D and I want the stdout to be asdfasdfasdf and to return to the console.
Can you tell me how to accomplish this?

blahblahblahblahblah

I've noticed that, on the BASH shell, 'read' may return input even when it's returning error when stdin closes. Try one last echo to see if you actually got it.

You can do something like this :

while read input
do
   echo "Input value: $input"
   last_input="$input"
done
echo "We are at EOF"
echo "The last input was: $last_input"

Jean-Pierre.

The Bash read built-in has the -d option so you can specify a delimiter other than newline...

read -d '^D' input

...where '^D' is 'Ctrl-V Ctrl-D'.

Thanks for you suggestions everyone. I ended up using your last one Jean-Pierre and though it is not exactly what I was looking for it helped me do what I want.