terminating script with CTRL+D


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
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

Code:
EOT=^D
while [ "$input" != "$EOF" ] //with & without quotations
do
read input
echo $input
done

Code:
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.
# 2  
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
Code:
trap cmd sig1 sig2 ...

Code:
trap 'print \'You hit control-D!\'' eof

# 3  
The read command returns status code 1 at EOF.
Code:
while read input
do
   echo $input
done

Jean-Pierre.
# 4  
@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.
# 5  
Quote:
Originally Posted by sanchopansa
@Glenn Arndt
I am using bash and I looked through the trap sigspecs and couldn't find which signal corresponds to CTRL+D.
Ctrl-D isn't a signal. It closes stdin.
# 6  
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.
# 7  
Quote:
Originally Posted by sanchopansa
@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.
After termination (CTRL-D) the variable $input is empty.
Code:
while read input
do
   echo "Input value: $input"
done
echo "We are at EOF"


Jean-Pierre.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #476
Difficulty: Medium
A Zettabyte (ZB) is 1,180,591,620,717,411,303,424 bytes.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automation of keyboard inputs..like Ctrl+d and Ctrl+a

Hi..! I'm stuck with my automation of starting a process and keeping it running even after the current ssh session has exited.. So i'm trying to use command 'screen'. which is doing exactly what i wanted, But the problem is automation of the same. i will have to press Ctrl+a and Ctrl+d for... (2 Replies)
Discussion started by: chandana hs
2 Replies

2. UNIX for Dummies Questions & Answers

Ctrl-V + Ctrl-J for newline character does not work inside vi editor

Hi friends, I am trying to add a newline char ('\n') between the query and the commit statement in the following shell script. #! /bin/sh echo "select * from tab; commit;" > data.sql I have tried typing in "Ctrl-V + Ctrl-J" combination which has inserted ^@ (NUL) character but the commit... (1 Reply)
Discussion started by: royalibrahim
1 Replies

3. Shell Programming and Scripting

How to handle CTRL+Z or CTRL+C in shells script?

Hi, while executing shell script, in the middle of the process, if we kill the shell script( ctrl+z or ctrl+c), script will be killed and the files which using for the script will be in the folder. How to handle those scenarios. Is there any possibilities, if user breaks the script, I need to... (3 Replies)
Discussion started by: ckchelladurai
3 Replies

4. UNIX for Dummies Questions & Answers

tcsh: how to prevent a foreach from terminating a script when the result is null?

Sorry if this has been answered. I did search both Google and this site and did find this post: unix.com/unix-dummies-questions-answers/152992-how-ignore-errors-script.html However, it wasn't answered. I have the same question - how do you prevent a tcsh script from terminating when the... (4 Replies)
Discussion started by: deepstructure
4 Replies

5. UNIX for Advanced & Expert Users

why the script is not terminating with proper results

Hi everyone, I am new to the linux.I wrote a small script and assigning two values to fname and lname and I want if the fname or lname are not given proper name like Toys or Gun the script should terminate and if they are given proper name it should execute.please help thanks:wall: #!/bin/bash... (4 Replies)
Discussion started by: starter2011
4 Replies

6. Shell Programming and Scripting

How can i terminating expect script without terminating SSH connection.

Hi all , i know i ask a lot of question but these are really hard to solve and important question. I send two scripts: expect.sh: #!/usr/local/bin/expect spawn ssh root@172.30.64.163 expect "login:" send "root\n" expect "password:" send "root\n^M" interact and son.sh: ... (2 Replies)
Discussion started by: fozay
2 Replies

7. Shell Programming and Scripting

how to terminate ssh connection without terminating script

Hi all, I connect with SSH connection to remote machine in the script and ı want to logout at half of the script then continue to script. If ı write exit in the script it terminates script not SSH connection. How can i do that please help me (1 Reply)
Discussion started by: fozay
1 Replies

8. Shell Programming and Scripting

script unexpectedly terminating

I now that this isnt the greatest code around. Im a network guy by trade not a programer .. but needed something to compare config files ... Anyway ... intermittently, the program terminates. Ive been looking at the code for a week trying to figure it out and Im stumped. Can anyone provide... (0 Replies)
Discussion started by: popeye
0 Replies

9. UNIX for Dummies Questions & Answers

Terminating child script with terminating the parent script

Hi I was working on a shell script with randomly shows a page of text from a randomly selected topic .As soon as the page is displayed it callers a timer script which keeps on running indefinitely until the timer script is killed by the user. This is where I have the problem,if I press... (2 Replies)
Discussion started by: mervin2006
2 Replies

10. AIX

Disable ctrl-c,ctrl-d,ctrl-d in ksh script

I wrote a ksh script for Helpdesk. I need to know how to disable ctrl-c,ctrl-z,ctrl-d..... so that helpdesk would not be able to get to system prompt :confused: (6 Replies)
Discussion started by: wtofu
6 Replies

Featured Tech Videos