"trap"ed !


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting "trap"ed !
# 1  
Old 07-07-2006
Question "trap"ed !

Hi,
I picked up a shell script function to implement a read timeout functionality : Wait for specified number of seconds to read input, else return.

E.g:
read_timeout 5 localvar
This would wait for 5 seconds to get a user input, and will exit if no input is given for 5 seconds. The localvar variable would become null in the timed out case.

The code goes as :

read_timeout() {
trap : USR1
trap 'kill "$pid" 2> /dev/null' EXIT
(sleep "$1" && kill -USR1 "$$") & pid=$!
read "$2"
ret=$?
kill "$pid" 2> /dev/null
trap - EXIT
return "$ret"
}

Inside this function, I want to set the input variable to a default value if the time out happens.

E.g. :
With the statement
read_timeout 5 localvar

localvar should become equal to "nothing" if I do not enter any value for 5 seconds. This is not happening if a set localvar = nothing before invoking the read_timeout function

Is it possible with this piece of code?


Thanks,
Puneet Arora
# 2  
Old 07-07-2006
bash has a read -t seconds builtin. It does exacatly what you want.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Trap triggered by "wrong parenthesizes" ?

I have following traps(s) in my code Line 913 triggers one of the traps with parenthesizes around (case-insensitive). Checked by removing them. Since it is a text I do not mind removing the offending () . But why it triggers both traps ? PS Where does the "bad" in trap... (0 Replies)
Discussion started by: annacreek
0 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Solaris

Trap signal on Window Manager "X" button clicked?

Well, my first post... thanks in advance! Can applications be notified of the X Window close (with "X" button) so the signal handler can run a cleanup process method? About the app: built with GNU C/C++ on Solaris 10, with WxWidgets. It is launched by a shell script as a background task. The... (2 Replies)
Discussion started by: HandsOGold
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

unix "trap" command behaviour

Hi I am using "trap" command in my script to prevent the user from running Ctrl-C during the its execution. My script creates number of children processes which in turn create some children processes as well during the execution. When user / tester tries to run Ctrl-C, the parent process is... (1 Reply)
Discussion started by: aoussenko
1 Replies

7. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

8. Solaris

"Trap 3e" OBP error on Ultra 5.

My Ultra 5 (Solaris 10) is now unbootable, with the OBP failing with error "trap 3e" When it tries to boot from disk. Disk is a Maxtor 80GB IDE, and was working previously. I've booted to cdrom and run "Format" from Single user, but when I try to read the disk, it just core dumps. I have... (2 Replies)
Discussion started by: akbar
2 Replies

9. UNIX for Dummies Questions & Answers

"trap" password expiration message

The majority of the users on our system are "captured" users where they log into script that is launched via their .profile. The password expiration message flashes by very quickly before they get the "menu" portion of the script to continue. Other than a sleep at scripts startup to slow... (2 Replies)
Discussion started by: MizzGail
2 Replies
Login or Register to Ask a Question