bash-trap

Get the exitcode of the shell script in a “trap EXIT”

不羁岁月 提交于 2019-12-07 04:20:37
问题 I want to have a cleanup action in my Bash scripts, like this: #! /bin/bash set -eu trap 'echo "E: failed with exitcode $?" 1>&2' ERR true false Using $? came to mind as a natural choice, but this isn't the case. It always contains 0 . Is there any way that I can "spy" on the exitcode in the ERR trap? [Update:] I have no idea what I had tested before. This code works like a charm, so I'm leaving it here as a small and good example. 回答1: Your ( probably simplified ) example doesn't exhibit the

Where does the exit status go after trap/return?

扶醉桌前 提交于 2019-12-07 04:17:08
问题 I was playing around with using trap inside a function because of this question, and came up with this secondary question. Given the following code: d() { trap 'return' ERR false echo hi } If I run d , the trap causes the shell to return from the function without printing 'hi'. So far so good. But if I run it a second time, I get a message from the shell: -bash: return: can only `return' from a function or sourced script At first, I assumed this meant the ERR sig was happening twice: Once

Does trap work as expected while piping?

给你一囗甜甜゛ 提交于 2019-12-05 04:32:22
Here is minimal code for issue demonstration: http://pastebin.com/5TXDpSh5 #!/bin/bash set -e set -o pipefail function echoTraps() { echo "= on start:" trap -p trap -- 'echo func-EXIT' EXIT echo "= after set new:" trap -p # we can ensure after script done - file '/tmp/tmp.txt' was not created trap -- 'echo SIG 1>/tmp/tmp.txt' SIGPIPE SIGHUP SIGINT SIGQUIT SIGTERM } trap -- 'echo main-EXIT1' EXIT echo "===== subshell trap" ( echoTraps; ) echo "===== pipe trap" echoTraps | cat echo "===== done everything" output ===== subshell trap = on start: = after set new: trap -- 'echo func-EXIT' EXIT func

Exit code of traps in Bash

血红的双手。 提交于 2019-12-04 19:34:35
问题 This is myscript.sh : #!/bin/bash function mytrap { echo "Trapped!" } trap mytrap EXIT exit 3 And when I run it: > ./myscript.sh echo $? 3 Why is the exit code of the script the exit code with the trap the same as without it? Usually, a function returns implicitly the exit code of the last command executed. In this case: echo returns 0 I would expect mytrap to return 0 Since mytrap is the last function executed, the script should return 0 Why is this not the case? Where is my thinking wrong?

Recover after bash trap

大城市里の小女人 提交于 2019-12-03 16:29:41
I've a bash script with some file manipulations and I would like to process a loop until the end of the block after pressing CTRL + C . I've made an example: #!/bin/bash # Register signal handler ABORT=0; trap ABORT=1 SIGINT; # Create temp dir TEMPDIR=$(mktemp -d -t $0); # Helper functions function do_other_stuff { true; } # Process files for ((COUNTER = 0; COUNTER < 3 && ABORT == 0; COUNTER++)); do FILE=/some/directory/$COUNTER.txt; BASE=$(basename $FILE); cp $FILE $TEMPDIR; > $FILE; do_other_stuff; cp $TEMPDIR/$BASE $FILE; rm $TEMPDIR/$BASE; done; rm -rf $TEMPDIR; This seems to work quite

Trap signal in child background process

♀尐吖头ヾ 提交于 2019-12-03 05:46:57
I am unable to trap a signal when running in a child / background process. Here is my simple bash script : #!/bin/bash echo "in child" trap "got_signal" SIGINT function got_signal { echo "trapped" exit 0 } while [ true ]; do sleep 2 done When running this and later do kill -SIGINT (pid) Everything works as expected, it prints 'trapped' and exits. Now, if I start the same script from a parent script like this : #!/bin/bash echo "starting the child" ./child.sh & Then the child does not trap the signal anymore.... ? After changing to use SIGTERM instead of SIGINT, it seems to be working correctly

How to trap exit code in Bash script

爷,独闯天下 提交于 2019-12-03 05:30:37
问题 There're many exit points in my bash code. I need to do some clean up work on exit, so I used trap to add a callback for exit like this: trap "mycleanup" EXIT The problem is there're different exit codes, I need to do corresponding cleanup works. Can I get exit code in mycleanup? 回答1: I think you can use $? to get the exit code. 回答2: The accepted answer is basically correct, I just want to clarify things. The following example works well: #!/bin/bash cleanup() { rv=$? rm -rf "$tmpdir" exit

Save and restore trap state? Easy way to manage multiple handlers for traps?

[亡魂溺海] 提交于 2019-12-03 04:23:34
问题 What is a good way to override bash trap handlers that don't permanently trample existing ones that may or may not already be set? What about dynamically managing arbitrary chains of trap routines? Is there a way to save the current state of the trap handlers so they can be restored later? 回答1: Save and Restore Your Trap Handler State in Bash I would submit the following stack implementation to track and restore trap state. Using this method, I am able to push trap changes and then pop them

How to trap exit code in Bash script

不想你离开。 提交于 2019-12-02 17:53:13
There're many exit points in my bash code. I need to do some clean up work on exit, so I used trap to add a callback for exit like this: trap "mycleanup" EXIT The problem is there're different exit codes, I need to do corresponding cleanup works. Can I get exit code in mycleanup? I think you can use $? to get the exit code. The accepted answer is basically correct, I just want to clarify things. The following example works well: #!/bin/bash cleanup() { rv=$? rm -rf "$tmpdir" exit $rv } tmpdir="$(mktemp)" trap "cleanup" INT TERM EXIT # Do things... But you have to be more careful if doing

Save and restore trap state? Easy way to manage multiple handlers for traps?

假如想象 提交于 2019-12-02 17:38:32
What is a good way to override bash trap handlers that don't permanently trample existing ones that may or may not already be set? What about dynamically managing arbitrary chains of trap routines? Is there a way to save the current state of the trap handlers so they can be restored later? Save and Restore Your Trap Handler State in Bash I would submit the following stack implementation to track and restore trap state. Using this method, I am able to push trap changes and then pop them away when I'm done with them. This could also be used to chain many trap routines together. See the following