Mahi
Mahi

Reputation: 21

Usage of trap command in shell

I am trying to trap the exit code "0" here. Whenever the exit code 0 is being trapped, check function is being called within trap, where I am trying to restart HW using command hascli -rn /, but this command execution is being skipped every time.

#!/bin/bash
source $TCE_COMMON
source commonFunctions.sh
trap "
     check
     " 0
checkRGservices
function check
{
 hascli -rn /
 %SLEEP 300
 %NODE none
 #!/bin/bash
 source $TCE_COMMON
 source commonFunctions.sh
 checkForCoreFiles
}

Can any one help me to get out of this.

Thanks in advance, Mahi.

Upvotes: 2

Views: 1122

Answers (1)

ktf
ktf

Reputation: 7365

As William already assumed:

In contrast to languages as C or Java where a compiler scans the whole program before it starts, the shell is interpreted in a top-down manner. So to be able to use your function check() you have to define it prior to that. The error message check: command not found is a strong indicator for this kind of problem. Solution: Move the function definition of check() to above the trap command.

One note to the trap command in general: As bobah stated, use keywords (here: EXIT) instead of numbers (here: 0) within the trap command. It makes your code more readable and in addition more portable, since not all *NIX Variants use the same numbers (although '0' is a special case here).

Upvotes: 2

Related Questions