how can i do some action when 'ctrl+d' is pressed

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how can i do some action when 'ctrl+d' is pressed
# 1  
Old 09-10-2009
how can i do some action when 'ctrl+d' is pressed

hi Gurus, please why is this happening:
when i run this:
declare -a name
declare -a ph
declare -a eid
echo "         name    phone    email_id"    
while :
 #if [ $i>$n ]; then
   echo -n "field $i:"; read name[$r$c] ph[$r$c] eid[$r$c];
   let "i++";
   let "c++";
#echo "the values of fields are :"

 # echo "field$i:${fld[$r$i]}    ${typ[$r$i]}    ${con[$r$i]}" 

when i keep on entering the values, the while loop runs uptil c=7 for c=8 it gives error like:
prayush@prayush-desktop ~/Desktop $ grun.bash ./ex2
         name    phone    email_id
field 1:abc    4327543 jhasdf@fdsj
field 2:sdf 23553    asf@sgfdfg
field 3:adsf    578543    asdfjdsaf
field 4:ahdfjhjk    4795636    gfhdhsdh
field 5:hretiuewndf    9805687954      fdsfjkfkj
field 6:asdfadsf
field 7:
field 8:
./ex2: line 15: 08: value too great for base (error token is "08")

when in Ist code ,if i make r=1, then it becomes an infinite loop. secondly when i uncomment if-else & for code in the script it skips the code with in 'else' and put null in the array variables .
still the problem of "value too great for base (error token is "08")" remains the same for r=0 & n>=8,for r=1 & n>=8 the 'while loop' breaks as per the condition.

heres the output:
 prayush@prayush-desktop ~/Desktop $  ./ex2
         name    phone    email_id
the values of fields are :
./ex2: line 24: 08: value too great for base (error token is "08")

what i actually wanted to do was, to read the values of variable until ctrl+d or ctrl+c or may be with any combination with 'ctrl' or 'Alt' if u can suggest. And when this shortkey is pressed i want to perform some custom actions.
# 2  
Old 09-10-2009
You use 01 02 03 04 .. 07 as array indices, which is unusual. I suspect these number get interpreted as octal numbers instead of decimal numbers, hence 08 would be to big for the base and it should be 10 (equals decimal 8).

Last edited by Scrutinizer; 09-10-2009 at 05:12 PM..
# 3  
Old 09-11-2009
thanks scruitizer!! that could be the case!!!!!!
But what about the problem of skipping the code when i uncomment 'if-else & for' code. the script is behaving differently!!
# 4  
Old 09-11-2009
Hi tprayush,

You are doing a lexicographical comparison (">") instead of a numerical comparison ("-gt"). So instead of
if [ $i>$n ]; then

you should use
if [ $i -gt $n ]; then

or preferably:
if [[ $i -gt $n ]]; then

or use an arithmetic evaluation:
if (( i > n )); then

Last edited by Scrutinizer; 09-11-2009 at 08:39 AM..
# 5  
Old 09-11-2009
thanks Scrutinizer for your help..
but i don't get the meaning of 'lexicographical comparison`!!!
# 6  
Old 09-11-2009
Alphabetical or "dictionary" order, typically it means using ASCII ordering.
so e.g. 100<2<23<39<4, whereas the numerical order is 2<4<23<39<100.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automation of keyboard 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

Get Rid ^C when pressed Ctrl-C

How do i get rid of the ^C when i pressed Ctrl-C? (7 Replies)
Discussion started by: vietrice
7 Replies

4. 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

5. Shell Programming and Scripting

Ctrl-C or Ctrl-Z causing exit the session

H! I have written script where it need to invoke the perl script in background, then write the pid in temp file then bring back the job to foreground. whenever the Ctrl-C or Ctrl-Z is pressed in the script has to exit and prompt should be dispalyed. but this script causing exit from shell session... (2 Replies)
Discussion started by: jramesh1
2 Replies

6. Shell Programming and Scripting

detect F5 is pressed

Hello friends, I want to write a shell script in bash shell . Working for the script is to detect any key pressed and disply on screen as "you have pressed: " For example if user pressed F5 then a messaged has to be displayed as "you have pressed F5. Thank you. (4 Replies)
Discussion started by: pradeepreddy
4 Replies

7. Shell Programming and Scripting

Catching ctrl-C or ctrl-D

Hi there, I'm using HP-UX 11 machine. I am running a script, thats gonna take a long time to execute. When I press ctrl-c to come out of my script, I have to catch that signal(ctrl-c) and display that ctrl-c had been pressed. How can I do it. Thanks in advance (2 Replies)
Discussion started by: sendhilmani123
2 Replies

8. 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

9. Programming

know what key is pressed

hi im making a program, and i would like to know how can i know what key was pressed. i'm using Sun5.7 and C. is there a keypress/keypressed function in C? how can i know recognize the keys (enter, tab, shift, etc.)? can i recognize two keys ? (shift+A, ctrl+C, etc) any idea.. thanks (4 Replies)
Discussion started by: DebianJ
4 Replies
Login or Register to Ask a Question