How to trap set -o nounset in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to trap set -o nounset in bash
# 1  
Old 06-28-2011
How to trap set -o nounset in bash

I have a bash script using "set -o nounset" to prevent unset variables.

However I have created a trap to run some cleanup options upon exit of the script which works fine for CTRL-C, etc. but if it hits and unset variable the trap does not run and the script bails out without having tidied up after itself.

I have been trying various signals but none seem to cause the trap to execute when hitting and unset variable.

What signal does "set -o nounset" use?

Below is an example (I have tried various other signals)

Code:
#!/bin/bash

set -u

trap "echo trap_has_run > /tmp/foo.txt" 1 2 3 15 ERR

echo $unsetvar

Code:
> /tmp/traptest.sh
/tmp/traptest.sh: line 7: unsetvar: unbound variable
> cat  /tmp/foo.txt
cat: /tmp/foo.txt: No such file or directory

# 2  
Old 06-28-2011
It is just a bit in VM, where is the signal?

Why not avoid touching unset variables in the trap or whatever ?

Code:
$ truss bash -c 'set -u ; echo mark ; echo $haha'
execve("/ndmtest/clmstux/nbkodln/myroot/usr/local/bin/bash", 0x7b0f54b8, 0x7b0f54c8) = 0 [32-bit]
 .
 .
 .
sigsetstatemask(0x17, NULL, 1073851480)                    = 0
getpeername(0, 0x7b0f59a0, 0x7b0f599c)                     ERR#216 ENOTSOCK
sigsetstatemask(0x17, NULL, 1073851480)                    = 0
sigsetstatemask(0x17, NULL, 1073851480)                    = 0
mark
write(1, "m a r k \n", 5)                                  = 5
bash: haha: unbound variable
write(2, "b a s h :   h a h a :   u n b o ".., 29)         = 29
sigsetstatemask(0x17, 0x4001ac58, 0)                       = 0
sigsetstatemask(0x17, 0x4001ac58, 0)                       = 0
exit(127)


Last edited by DGPickett; 06-28-2011 at 03:07 PM..
# 3  
Old 06-28-2011
So that means it will run the trap on normal exit of the script, assuming EXIT is used... Looks like I will have to rethink how my cleanup operates as I had planned on only running the trap on script failure and had hoped it would capture "set -u" exit's.

Could you elaborate on you second sentence?
# 4  
Old 06-29-2011
It is possible to test before you touch. Personally, I turn it off and write scripts where unset and "" are equivalent. It makes for more code and more ways to fail, more costly than can be offset by any speed it adds to debugging!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash set dummy ?

Could somebody please provide me with verbatim description / purpose of the following ? set dummy $ac_prog; ac_word=$2 set dummy $ac_tool_prefix; ac_word=$2 could it be written set dummy $ac_prog; ac_word=$2 set dummy $ac_tool_prefix; ac_word=$2 (9 Replies)
Discussion started by: anne
9 Replies

2. Shell Programming and Scripting

How do i check if all parameters are set in bash?

Hi, I have 4 parameters passed to my shell and i validate if all four are entered using the following snippet: if then echo "Not entered" else echo "entered" fi I get the following output as 'Not entered even when i enter the values for all prompts. Please advise. Thanks. (5 Replies)
Discussion started by: Jesshelle David
5 Replies

3. Shell Programming and Scripting

Bash - trap error file not found

Hello. In bash, is there a way to trap error "file not found" when a script call another script which is not found; then abort. Example ( part of script running with -x option set) : + return 0 + RETURN_CODE=0 + ] + /root/bin/200_yast_install/00_reset_yast_install bash:... (5 Replies)
Discussion started by: jcdole
5 Replies

4. Shell Programming and Scripting

Shell script to set trap for finding cron job failures

Unix box: solaris 5.8 Server: IP Need to to set trap for cron job failures by writing a shell script (5 Replies)
Discussion started by: ChandruBala73
5 Replies

5. Homework & Coursework Questions

VM trap may work differently than a pure install trap.

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: That is the last reply I received from my instructor, and I'm looking for some alternatives. When using... (2 Replies)
Discussion started by: newuser45
2 Replies

6. Shell Programming and Scripting

Record the Signal Type or Number in Bash Trap function

In my Bash script I have an exit/cleanup function in a trap statement like: trap exitCleanup 1 2 3 6 15 25 Is there anyway to capture which signal # has occurred to record in a log file. Please note I am trying to avoid something like: trap 'mySignal=1; exitCleanup' 1 trap... (1 Reply)
Discussion started by: ckmehta
1 Replies

7. Shell Programming and Scripting

Bash Trap Problem?

Hey all, I'm a fairly new shell scripter (been writing some very basic stuff for a couple of years once in a blue moon). I have the need to start 2 or 3 processes from within a bash script when it's run then have the script kind of hang out and wait for the user to ctrl+c and when that happens... (3 Replies)
Discussion started by: jsabino
3 Replies

8. Shell Programming and Scripting

Cntl+z Trap is not detecting ??? Help required to add a trap detection ???

Hi folks, I have tried to add some trap detection in the below script....this script is used to monitor database activities...in a rather awkward way :rolleyes:.... The idea behind adding trap is that....this script creates lots of temporary files in the running folder to store the count... (1 Reply)
Discussion started by: frozensmilz
1 Replies

9. Shell Programming and Scripting

test-and-set in bash

I'm not talking about the assembly instruction TAS, a better name could be check-and-set :) Anyway, is there a way to simplify the following if ; then VAR="something"; fi I have ~20 variables that should be test-and-set like this, and it really looks lame. (2 Replies)
Discussion started by: rayne
2 Replies

10. Shell Programming and Scripting

how to trap unix signal if the process killed/interupt occured in bash...

hey champs, I have a process running.......i have to catch/trap the signal when the process is being interupted/killed (kill -9 pid) option...... how can i achieve the same thru my process........ let my process is a.sh and it supposed to take 13 mins to complete, but due to some problem ,... (15 Replies)
Discussion started by: manas_ranjan
15 Replies
Login or Register to Ask a Question