Skip to main content
Tweeted twitter.com/StackUnix/status/871916509626552321
Ask for the idiomatic way, not the way I actually know
Source Link
Ian
  • 273
  • 3
  • 7

I need to know whether a command has succeeded or failed, and unconditionally run some cleanup afterward.

Neither of the normal options for executing sequential commands seem to be applicable here:

$ mycmd.sh && rm -rf temp_files/    # correct exit status, cleanup fails if mycmd fails
$ mycmd.sh ;  rm -rf temp_files/  # incorrect exit status, always cleans up
$ mycmd.sh || rm -rf temp_files/    # correct exit status, cleanup fails if mycmd succeeds

If I was going to do it in a shell script, I'd do something like this:

#!/usr/bin/env bash
mycmd.sh
RET=$?
rm -rf temp_files
exit $RET

Is there a more idiomatic way to accomplish that on the command line than semicolon-chaining all those commands together?

I need to know whether a command has succeeded or failed, and unconditionally run some cleanup afterward.

Neither of the normal options for executing sequential commands seem to be applicable here:

$ mycmd.sh && rm -rf temp_files/    # correct exit status, cleanup fails if mycmd fails
$ mycmd.sh ;  rm -rf temp_files/  # incorrect exit status, always cleans up
$ mycmd.sh || rm -rf temp_files/    # correct exit status, cleanup fails if mycmd succeeds

If I was going to do it in a shell script, I'd do something like this:

#!/usr/bin/env bash
mycmd.sh
RET=$?
rm -rf temp_files
exit $RET

Is there a way to accomplish that on the command line?

I need to know whether a command has succeeded or failed, and unconditionally run some cleanup afterward.

Neither of the normal options for executing sequential commands seem to be applicable here:

$ mycmd.sh && rm -rf temp_files/    # correct exit status, cleanup fails if mycmd fails
$ mycmd.sh ;  rm -rf temp_files/  # incorrect exit status, always cleans up
$ mycmd.sh || rm -rf temp_files/    # correct exit status, cleanup fails if mycmd succeeds

If I was going to do it in a shell script, I'd do something like this:

#!/usr/bin/env bash
mycmd.sh
RET=$?
rm -rf temp_files
exit $RET

Is there a more idiomatic way to accomplish that on the command line than semicolon-chaining all those commands together?

Source Link
Ian
  • 273
  • 3
  • 7

Getting "ensure" / "finally" functionality in a shell command (not script)?

I need to know whether a command has succeeded or failed, and unconditionally run some cleanup afterward.

Neither of the normal options for executing sequential commands seem to be applicable here:

$ mycmd.sh && rm -rf temp_files/    # correct exit status, cleanup fails if mycmd fails
$ mycmd.sh ;  rm -rf temp_files/  # incorrect exit status, always cleans up
$ mycmd.sh || rm -rf temp_files/    # correct exit status, cleanup fails if mycmd succeeds

If I was going to do it in a shell script, I'd do something like this:

#!/usr/bin/env bash
mycmd.sh
RET=$?
rm -rf temp_files
exit $RET

Is there a way to accomplish that on the command line?