1

Im having issues where the EXIT Trap command is not seeing my exit code. Ive tried just setting a $var from 0 to 1 and right now I'm trying to override the exit with a 1 and base on that having the trap command run certain code.

#!/bin/bash

if [[ 0 -ge 1 ]]; then
    echo "run code"
else
    echo "oops.. dont like what I see"
    exit 1
fi

finish() {
    sleep 5
    term=$?
    if [[ $term -eq 0 ]]; then
        echo pass
    else
        echo fail
    fi
}
trap 'finish' EXIT

When I troubleshoot the code. term is being assign to 0 when exit is triggered.

Tags
  • 172
  • 3
  • 16
  • 1
    The trap is never installed with the code given in the question, as `exit` runs before it's ever configured. – Charles Duffy Sep 14 '18 at 19:58
  • Compare to https://ideone.com/LLDJZR, which doesn't exhibit the issue (because it defines the `finish` function and installs it as a trap at the **top** of the script). – Charles Duffy Sep 14 '18 at 20:00
  • Ok, I thought it didnt matter for trap commands since and EXIT always runs at the end of the script no matter what – Tags Sep 14 '18 at 20:00
  • The EXIT trap may always run, but you still need to actually install something in that hook for it to be triggered. If you never run the line of code that installs it (because you exit before getting to that line)... – Charles Duffy Sep 14 '18 at 20:01
  • Think of it as if by default, you have `trap '' EXIT`. So when you run `exit` on line 1, that empty-string exit trap happens. Whereas if you run the command `trap 'foo' EXIT`, then *only after that command has been run* you defined `foo` as a new exit trap. And if you run `trap 'bar' EXIT`, it'll replace `foo`; the `trap` command itself modifies the shell's state when it executes, so it has to actually be executed to do anything. – Charles Duffy Sep 14 '18 at 20:03
  • ...so, it sounds like this question could probably be edited to better represent the misunderstanding at the core of the question -- are you inclined to do so? As-is, I think it's prone to "close as typo or not reproducible", but if you edit it to explain why you think the original code *should* work, then an explanation of why it doesn't is on-topic. – Charles Duffy Sep 14 '18 at 20:04
  • "EXIT only runs at the end of the script" doesn't mean everything up to the end of the script gets evaluated before an exit trap is invoked; instead, it means that if you reach the end of the script and terminate for that reason, an exit trap still runs -- but if the script terminates for another reason, then other content *isn't* necessarily invoked). – Charles Duffy Sep 14 '18 at 20:05
  • I see, that makes sense. Another lame mistake.. but I'm learning. Not too bad I think for two weeks of learning. Thank you Charles – Tags Sep 14 '18 at 20:06
  • 1
    Thank you for putting in the work to build a reproducer; that's why the issue was as easy to isolate as it was. Indeed, not bad at all for two weeks. :) – Charles Duffy Sep 14 '18 at 20:07
  • 2
    Hmm. I'm curious how you're troubleshooting that's coming up with `term=0`. See https://ideone.com/JsGtwX -- only lines 3, 6 and 7 get run at all, so there's no `term=` assignment whatsoever. – Charles Duffy Sep 14 '18 at 20:10
  • (...which is to say -- could you edit the question further, to show precisely how you're reaching that conclusion?) – Charles Duffy Sep 14 '18 at 20:25
  • 1
    I know why, I had `sleep 5` right above the assignment of `term=$?`. Which I would assume would overwrite `$?` to `0` since it was a successful execution. – Tags Sep 15 '18 at 03:28
  • Yup, that would do it. Always important to test that an issue recurs with the exact code in the question. :) – Charles Duffy Sep 15 '18 at 15:04

1 Answers1

1

Two issues here:

  • Because the trap 'finish' EXIT line is at the bottom of the script, any exit command which is invoked before execution reaches that point will not honor the trap.

    To address this, move the finish function's declaration, and the trap command activating it, above the first point in the script where an exit might occur

  • Because the sleep 5 is immediately above the term=$?, it overrides the value of $? which might have otherwise been set. Be sure to capture $? before running any commands which might change it.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441