Live data from Hacker News

“Exit traps” can make your Bash scripts more robust and reliable (2013)

redsymbol.net

111–120 of 159 posts

Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)

#112
I have given up the unequal struggle to learn Bash.

For me it is "read only".

It is too arcane, even for me.

I use Perl now. I tried to reform last year as I was building a system from lots of executable pieces, the perfect job for Bash

After much pain and suffering I re-wrote it in Perl. What a (relative) breeze.

Just. Don't. Do. Bash.

Works for me!

Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)

#114

Just as an alternative suggestion, consider using a "down file" instead if you can and letting the code gracefully end. Plus you get to write "touch down" and do an end zone dance.

I think you need to be a little more specific about what the "down file" is for, especially since you can't for google it.

Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)

#115
post #39

Earlier quoted context omitted.

> Because trap doesn't pass the signal information to the handler You can examine $? on entry to the trap function. On signals, it will be 128 + signal. i.e. on TERM (15) it will be 143. On INT (2) it will be 130. #!/bin/bash skip_exit= on_exit() { code=$? if test $code == 130; then skip_exit=1 fi if test -n "$skip_exit"; then return fi echo "Exiting with: $code" return $code } trap on_exit INT EXIT sleep 2 false Wit…

For robust code you should also use set -e. This changes things again. Often a process started by the shell will get the signal, too (depends of course on how it is sent) and exit with a non-zero return value (depends on the process of course). I believe (not at the computer right now) the handler for EXIT is called in that case. Was it so that bash can also trap ERR, but dash cannot? It's not perfectly easy to handl…

> For robust code you should also use set -e.

Highly debatable.

Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)

#116
post #114

Just as an alternative suggestion, consider using a "down file" instead if you can and letting the code gracefully end. Plus you get to write "touch down" and do an end zone dance.

I think you need to be a little more specific about what the "down file" is for, especially since you can't for google it.

Apologies. It's for ending execution prematurely. Here's an example:

while true; do

  echo hello

  test -f down && exit
done

Now to stop early you execute "touch down".

Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)

#117
post #69

Just use Python or any other proper high-level language that has proper control structures.

Sometimes you can “just use python”, like openwrt script or similar.

OpenWrt has both CPython and MicroPython packaged.

Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)

#118
post #65

Earlier quoted context omitted.

It's secret enough to be well documented in the man page. The real question is, why do people look to random web pages prior to having digested everything in the manual? People used to say "rtfm" all the time, this would be regarded as shockingly rude in today's tech culture but it was a valuable public service to have it repeated, like being reminded to eat your vegetables.

Because ain't nobody got time for that. ;) More seriously, I think that we have been trained to rely on just in time searches (or ChatGPT sessions) when we encounter the next thing we need to learn. RTFM is just so time consuming and I personally don't recall everything I have read, leading me to rely on search/AI to re-learn the next thing just in time anyways. In some ways this is a vast improvement, which is why i…

[dead]

Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)

#119

Don't use traps unless you have to. They are subtly complex and require a great deal more code to deal with edge cases. There is almost always a simpler way to accomplish what you want. If Bash has taught me anything, it's that many advanced features should seldom be used. Always resist the temptation to be fancy.

If Bash has taught me anything it's that Bash should seldom be used. Always resist the temptation to be lazy.

Re: “Exit traps” can make your Bash scripts more robust and reliable (2013)

#120
post #25
post #7

An annoying thing about bash is that EXIT will also run on SIGINT (^C), which most other shells won't (in my reading it's also not POSIX compliant, although the document is a bit vague). Some might argue this is a feature, but IMHO it's a bug – sometimes you really don't want cleanup to happen so people can inspect the contents of temporary files for debugging. Because trap doesn't pass the signal information to the…

Some temporary file remover. Lol indeed

All files are temporary if your timespan is long enough.
Post reply on HN