TRAP Command --use based on exit criteria


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting TRAP Command --use based on exit criteria
# 1  
Old 12-05-2008
TRAP Command --use based on exit criteria

Hi all,
I am using the trap command in my script, and I want it to trap the signal based on the exit code the script returns.
can anybody tell me how can I use "if loop" for "trap" command.
I want to print
"terminated by user" if signal is SIGINT or 2
"failure" if signal is not 2 and not 0
"SUCCESS...!!!" if signal is 0

Thanks in advance
# 2  
Old 12-05-2008
If there are only specific functions you know you want to fail on, you can code a function for each and eval whatever you trap within the function. Seems like too much work, because what if the error is something you never anticipated and would tip you off to the real issue (assuming you run into one)? You can't account for kill -9 -- it's going to stop your script and you're not going to get an exit code. Here's a quick way to account for singals 1-8 (you can work the rest), and by default your script is going to return 0 is it executes ok, so you can just trap 0 and 2 and I'd think the OS should be left to handle the rest...:

#!/usr/bin/ksh
# First set a trap for all signals
for x in `kill -l`
do trap "echo Failed - Signal $x" $x
#echo "Signal set to $x for $x"
done

# Change the trap for signals 0 and 2
trap 'echo "terminated by user"' 2
trap 'echo "SUCCESS"' 0

# Use a loop so it's easy to see what's happening for demo purposes
counter=0
while [ $counter -lt 8 ]
do echo "Script running"
counter=$((counter+1))
sleep 1
kill -$counter $$
done

When run, this outputs the following:
Script running
Failed - Signal HUP
Script running
terminated by user
Script running
Failed - Signal QUIT
Script running
Failed - Signal ILL
Script running
Failed - Signal TRAP
Script running
Failed - Signal ABRT
Script running
Failed - Signal EMT
Script running
Failed - Signal FPE
SUCCESS
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl: trap signal 'exit': why I am not able to have it work??

First time trying to work with signals in Perl. Reviewing example I try it, but not able to get it work for 'exit'. I hope, I am correct, assuming, that the ending any code by exit $return_code; the $SIG{EXIT} should be de-referenced and processed?! So, I have such code, that, I assume,... (5 Replies)
Discussion started by: alex_5161
5 Replies

2. Shell Programming and Scripting

Need a Linux command for find/replace column based on specific criteria.

I'm new to shell programming, I have a huge text file in the following format, where columns are separated by single space: ACA MEX 4O_ $98.00 $127.40 $166.60 0:00 0:00 0 ; ACA YUL TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ; ACA YYZ TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ; ADZ YUL TS_ $300.00... (3 Replies)
Discussion started by: transat
3 Replies

3. Linux

Merge two files based on matching criteria

Hi, I am trying to merge two csv files based on matching criteria: File description is as below : Key_File : 000|ÇÞ|Key_HF|ÇÞ|Key_FName 001|ÇÞ|Key_11|ÇÞ|Sort_Key22|ÇÞ|Key_31 002|ÇÞ|Key_12|ÇÞ|Sort_Key23|ÇÞ|Key_32 003|ÇÞ|Key_13|ÇÞ|Sort_Key24|ÇÞ|Key_33 050|ÇÞ|Key_15|ÇÞ|Sort_Key25|ÇÞ|Key_34... (3 Replies)
Discussion started by: PK29
3 Replies

4. Shell Programming and Scripting

Match based on criteria to file

Trying to match $1 of target.txt to $5 of file.txt. If there is a match then in an output.txt file $1,$1 (row underneath),$6,$4,$7 from file.txt are printed on the same line as $1 of target.txt. The input is from excel and the output should be tab-deliminated. Thank you :). target.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

5. UNIX for Dummies Questions & Answers

How to fetch files right below based on some matching criteria?

I have a requirement where in i need to select records right below the search criteria qwertykeyboard white 10 20 30 30 40 50 60 70 80 qwertykeyboard black 40 50 60 70 90 100 qwertykeyboard and white are headers separated by a tab. when i execute my script..i would be searching... (4 Replies)
Discussion started by: vinnu10
4 Replies

6. UNIX for Dummies Questions & Answers

How to select files based on a criteria?

I have a file..... xxx 2345 455 abc 345 555 cdf 456 777 fff 555 888 Now my requirement is, Say if, i want to select only those records prior to the record fff 555 888... how do i go about doing this in unix.... The fff would be hardcoded as it wud be fixed and everytime when i... (7 Replies)
Discussion started by: saggiboy10
7 Replies

7. Shell Programming and Scripting

Merging Lines based on criteria

Hello, Need help with following scenario. A file contains following text: {beginning of file} New: This is a new record and it is not on same line. Since I have lost touch with script take this challenge and bring all this in one line. New: Hello losttouch. You seem to be struggling... (4 Replies)
Discussion started by: losttouch
4 Replies

8. UNIX for Dummies Questions & Answers

remove duplicates based on a field and criteria

Hi, I have a file with fields like below: A;XYZ;102345;222 B;XYZ;123243;333 C;ABC;234234;444 D;MNO;103345;222 E;DEF;124243;333 desired output: C;ABC;234234;444 D;MNO;103345;222 E;DEF;124243;333 ie, if the 4rth field is a duplicate.. i need only those records where... (5 Replies)
Discussion started by: wanderingmind16
5 Replies

9. Shell Programming and Scripting

substract column based on some criteria

Please guide if you know how to solve this. I have a tab delimited INPUT FILE where each record is separated by ----- ----- ABC 4935402 4936680 Pattern=Cheers07080.1 ABC 4932216 4932368 Pattern=Cheers07080.1 ABC 4931932 4932122 ... (8 Replies)
Discussion started by: sam_2921
8 Replies

10. Shell Programming and Scripting

Getting exit status of child in trap handler

Hi, I have a trap problem when calling a child script in the background. I know there are a lot of threads here on the issue of traps and signals, I think I have read all the relevant ones, but still haven't found an answer to my problem. I'm working on Linux or HP, the script as you can see... (4 Replies)
Discussion started by: rimon
4 Replies
Login or Register to Ask a Question