Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use ctrl-D in a shell script

Tags:

bash

shell

unix

I have a bash shell script that requires ctrl-D to break out of the terminal.can anyone tell me how to use it in the shell script

example

ssh host last --->displays the list of users who were logged on to that host

I have different hosts the output is appended to one final and when I'm executing this particular shell script along with other series of commands, i'm using ctrl-D to see the output

suppose my shell script is myscript.sh to execute myscript.sh

./myscript.sh

ctl-D

connection to host closed

output is displayed

Instead, I want to use ctrl-D in my script file

like image 454
Shruti Avatar asked Oct 16 '10 11:10

Shruti


2 Answers

There is no way to do this directly. Use a heredoc to feed stdin instead.

./someprog.sh << EOF
something here
EOF
like image 84
Ignacio Vazquez-Abrams Avatar answered Oct 03 '22 09:10

Ignacio Vazquez-Abrams


You could try exec <&-
&- is used to close a file descriptor (ps:everything in linux is a kind of file...)
<&- is closing file descriptor 0 = stdin - can also be written as 0<&-

If you open a normal terminal in your linux machine and type exec <&- you will see your terminal to close/dissapear like if you press ^D.

PS1: Similarly, exec >&- closes stdout

PS2: If you close stdin with exec <&- you can re-open to continue your script with something like exec </dev/tty

like image 31
George Vasiliou Avatar answered Oct 03 '22 09:10

George Vasiliou