| Bash pipes and exit problem |
|
|
|
|
I have one simple bash helper function assert bored from http://tldp.org/LDP/abs/html/debugging.html and slightly modified: ################################################################# All look fine and I start use it. But there also not without submarine riffs. It suddently stop working and hopefully I occasionally found error messages in logs. Assert stop working in strange way - error printed, but no script termination happened. I start dig and found very interesting behaviour: echo 'Before function' I have spent many time to figure out what happened. In short - pipe run in subshell! It may be not so obviously, but it stated in bash man (you could see explanation also there http://steve-parker.org/sh/functions.shtml ). There no standard way to terminate parent (main script) in this case regardless of used "return" or "exit" statements in function:http://stackoverflow.com/questions/4419952/difference-between-return-and-exit-in-bash-functions, http://stackoverflow.com/questions/1816824/exit-entire-program-from-a-function-call-in-shell. Exception from that I found solution kill himself like "kill $$" - http://www.unix.com/shell-programming-scripting/138182-functions-exit-kill-bash.html. It may work in this particular solution, but also have some downsides like unavailable pass status code, may kill not intended script when it sourced and so on. Another way to do that job is explicitly check $PIPESTATUS special variable (More examples and details:http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2005-06/1079.html). So, to provide also exit code from assert I wrote this small helper function: # $1 - pipestatus code Example of usage: assert '1 -gt 2' | tee LOG |
| Written by Павел Алексеев aka Pahan-Hubbitus |
| Sunday, 12 December 2010 21:19 |
| Last Updated on Sunday, 09 January 2011 02:03 |
Последнее из блога
Последнее из блога






