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
@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.
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.
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?
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.
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.