Please use
[code][/code] tags around your code, to preserve formatting and to improve readability.
And please take some time to format your script a bit more. Add some blank spaces between logical sections. But at least you're commenting.
Code:
# Execute command
exec $1 &
# Check for errors
if [ $? -gt 0 ]; then
You have to wait until a command finishes before you can test its exit code (naturally). But when you execute a command in the background you are telling the shell to fork off a new process and continue on to the next command immediately. The subsequent test is then run before the result of the previous command is known.
Instead of using
exec and
&, try simply running the command in a subshell or grouped command block.
Code:
# Execute command
{ $1 ;} # or ( $1 )
# Check for errors
if (( $? > 0 )); then
Bash's ((..))
arithmetic evaluation is recommended for numeric tests like this, BTW.
However, I don't think it's a good idea to transfer a whole command in a single parameter. Storing code in variables is a tricky thing and not generally recommended. It may be safer if you just use
$@ instead to pass it the entire argument list as the command to launch, and forget about quoting it.
http://mywiki.wooledge.org/BashFAQ/050
Finally,
$(..) is highly recommended over `..`