I would like to write a small part of a script that saves the error status, executes some other code, and sets the error status to the original error status. In Bash it looks like this:
<< some command >>; _EXIT=$?;( <<other code here>> ;exit $_EXIT)
But I need code that will run no matter if it is being run under bash, zsh, csh or tcsh. I do not know which shell will be used in advance, because it is decided by the user. The user also decides << some command >>.
It is safe to assume that << other code >> will work in all shells, but it is not safe to assume that you can write a file (so putting it into a file will not work).
Background
GNU Parallel executes commands given by the user in the shell decided by the user. When the command is finished, GNU Parallel has some cleanup to do. At the same time GNU Parallel needs to remember the exit value of the command given by the user. The code run before the snippet above is the user given command. << other code >> is the cleanup code.
fish
:-) ... The problem is that variable assignment is different forsh
/csh
/fish
, and it's not easy to make this compatible (usingtest
or any other commands will never work, since that will reset$?
)... I'm not sure what you're trying to do here, why don't you just execute your script with/bin/sh
or whatever? ... The only option is to create separate scrips. – Martin Tournoij Jan 15 '15 at 23:59_EXIT=$?; <<other code here>>; (exit $_EXIT)
, to allow the "other code" to modify the state of the shell (e.g.,cd
, set variables, etc.) – Scott - Слава Україні Jan 16 '15 at 00:02sh
-like shells andcsh
-like shells. Their syntaxes for variable assignment and conditionals are totally incompatible. – Barmar Jan 16 '15 at 20:36bash
(or whatever shell you prefer) to execute the wrapper? – Barmar Jan 16 '15 at 21:45