@Gillesの答えを拡張するには:
確かに、サブシェルでset -eコマンド||を実行しても、コマンドの後に演算子を使用すると、コマンド内では機能しません。たとえば、これは機能しません:
#!/bin/sh
# prints:
#
# --> outer
# --> inner
# ./so_1.sh: line 16: some_failed_command: command not found
# <-- inner
# <-- outer
set -e
outer() {
  echo '--> outer'
  (inner) || {
    exit_code=$?
    echo '--> cleanup'
    return $exit_code
  }
  echo '<-- outer'
}
inner() {
  set -e
  echo '--> inner'
  some_failed_command
  echo '<-- inner'
}
outer
ただし||、クリーンアップの前に外部関数から戻るのを防ぐには、演算子が必要です。
これを修正するために使用できる小さなトリックがあります。バックグラウンドで内部コマンドを実行し、すぐにそれを待ちます。wait組み込みは、内部コマンドの終了コードを返し、今は使っている||の後にwaitそうではなく、内部機能、set -e後者の内部で適切に動作します:
#!/bin/sh
# prints:
#
# --> outer
# --> inner
# ./so_2.sh: line 27: some_failed_command: command not found
# --> cleanup
set -e
outer() {
  echo '--> outer'
  inner &
  wait $! || {
    exit_code=$?
    echo '--> cleanup'
    return $exit_code
  }
  echo '<-- outer'
}
inner() {
  set -e
  echo '--> inner'
  some_failed_command
  echo '<-- inner'
}
outer
これは、このアイデアに基づいた汎用関数です。localキーワードを削除すると、すべてのPOSIX互換シェルで動作するはずです。つまり、すべてlocal x=yをjustに置き換えますx=y。
# [CLEANUP=cleanup_cmd] run cmd [args...]
#
# `cmd` and `args...` A command to run and its arguments.
#
# `cleanup_cmd` A command that is called after cmd has exited,
# and gets passed the same arguments as cmd. Additionally, the
# following environment variables are available to that command:
#
# - `RUN_CMD` contains the `cmd` that was passed to `run`;
# - `RUN_EXIT_CODE` contains the exit code of the command.
#
# If `cleanup_cmd` is set, `run` will return the exit code of that
# command. Otherwise, it will return the exit code of `cmd`.
#
run() {
  local cmd="$1"; shift
  local exit_code=0
  local e_was_set=1; if ! is_shell_attribute_set e; then
    set -e
    e_was_set=0
  fi
  "$cmd" "$@" &
  wait $! || {
    exit_code=$?
  }
  if [ "$e_was_set" = 0 ] && is_shell_attribute_set e; then
    set +e
  fi
  if [ -n "$CLEANUP" ]; then
    RUN_CMD="$cmd" RUN_EXIT_CODE="$exit_code" "$CLEANUP" "$@"
    return $?
  fi
  return $exit_code
}
is_shell_attribute_set() { # attribute, like "x"
  case "$-" in
    *"$1"*) return 0 ;;
    *)    return 1 ;;
  esac
}
使用例:
#!/bin/sh
set -e
# Source the file with the definition of `run` (previous code snippet).
# Alternatively, you may paste that code directly here and comment the next line.
. ./utils.sh
main() {
  echo "--> main: $@"
  CLEANUP=cleanup run inner "$@"
  echo "<-- main"
}
inner() {
  echo "--> inner: $@"
  sleep 0.5; if [ "$1" = 'fail' ]; then
    oh_my_god_look_at_this
  fi
  echo "<-- inner"
}
cleanup() {
  echo "--> cleanup: $@"
  echo "    RUN_CMD = '$RUN_CMD'"
  echo "    RUN_EXIT_CODE = $RUN_EXIT_CODE"
  sleep 0.3
  echo '<-- cleanup'
  return $RUN_EXIT_CODE
}
main "$@"
例の実行:
$ ./so_3 fail; echo "exit code: $?"
--> main: fail
--> inner: fail
./so_3: line 15: oh_my_god_look_at_this: command not found
--> cleanup: fail
    RUN_CMD = 'inner'
    RUN_EXIT_CODE = 127
<-- cleanup
exit code: 127
$ ./so_3 pass; echo "exit code: $?"
--> main: pass
--> inner: pass
<-- inner
--> cleanup: pass
    RUN_CMD = 'inner'
    RUN_EXIT_CODE = 0
<-- cleanup
<-- main
exit code: 0
このメソッドを使用する際に注意する必要があるのはrun、コマンドがサブシェルで実行されるため、渡すコマンドから行われたシェル変数のすべての変更が呼び出し関数に反映されないことです。
               
              
test -d /path/to/directory(または[[ -d /path/to/directory ]]bashで)指定されたターゲットがディレクトリかどうかを通知し、静かにそれを行います。