LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-24-2006, 05:07 AM   #1
panchosansa
LQ Newbie
 
Registered: Oct 2006
Posts: 18

Rep: Reputation: 0
bash script to read CTRL+D


Hello,

I am trying to make a small script that exits the program when it encouters CTRL+D. I've tried things like

Code:
while [[ $in != "^D" ]]
read in
echo $in
Code:
while [[ $in != EOF ]]
Code:
while [[ $in != $EOF ]] //this one termitated the program right away
but with no success. I was also considering making 0x04 a condition in the loop but couldn't find a way to do this. I would be grateful if anyone can tell me how to make this thing work.
 
Old 10-24-2006, 07:56 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594Reputation: 3594
I am trying to make a small script that exits the program when it encounters CTRL+D
That's logout (signal 1 IIRC) but you can't catch that because it's not a means to end the script. To catch a signal use the Bash "trap" builtin as in "trap 'echo trapped exit, logout or interrupt' 0 1 2".
 
Old 10-24-2006, 09:02 AM   #3
panchosansa
LQ Newbie
 
Registered: Oct 2006
Posts: 18

Original Poster
Rep: Reputation: 0
Thanks unSpawn, I wasn't aware that this is the way it should be done. I've tried it and it works but not exactly as I want it to. The segment that I'm using is
Code:
trap "echo trapped" 0 1 2
read input
echo $input
and this works more or less fine. One problem is that I have to enter CTRL+D twice so that the program logouts and the second thing is that it exits also on return and that's something I would like to avoid. Am I doing sth wrong or is it just the way the trap works? Is it possible to have the program logout on CTRL+D and not logout on return.
 
Old 10-24-2006, 10:53 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Code:
cat | while read line; do
  #script actions using $line
done
Something like that? [Ctrl]+D is merely a manual EOF when typing to standard input; you will get the same result when the end of a file is reached or a pipe is closed. When not piping input to the script, the standard input file is the console, and [Ctrl]+D closes the input side of that file.
ta0kira

Last edited by ta0kira; 10-24-2006 at 10:56 AM.
 
Old 10-25-2006, 03:49 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: NetBSD, Void, Debian, Mint, Ubuntu, Puppy, Raspbian
Posts: 3,487

Rep: Reputation: 233Reputation: 233Reputation: 233
set -o ignoreeof

in your profile
 
Old 10-26-2006, 03:25 AM   #6
panchosansa
LQ Newbie
 
Registered: Oct 2006
Posts: 18

Original Poster
Rep: Reputation: 0
Thanks for your help everyone. I ended up using unSpawn suggestions and it suits me fine though it is not exactly waht I was looking for.

Regards,
V
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
read write in bash script yhus Programming 6 09-18-2006 12:23 PM
bash script that can read lines of text palceksmuk Programming 1 12-25-2005 03:49 AM
bash: read lines from a configuration script ldp Programming 2 09-23-2005 11:58 AM
Help with BASH script PLEASE READ!!! hroman Programming 7 10-08-2004 07:39 PM
problems with bash script read wedgeworth Programming 3 02-16-2004 08:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration