LoginSignup
53
44

More than 5 years have passed since last update.

シェルスクリプトでtry-catch-finally

Posted at

引数のパスに対してのcdの成否でCATCHが表示されたりされなかったり。
FINALLYは常に表示される。

#!/bin/sh

set -eu

trap catch ERR
trap finally EXIT

function catch {
    echo CATCH
}
function finally {
    echo FINALLY
}

echo pre
cd $1
echo post

exit 0

成功例

$ ./test.sh /
pre
post
FINALLY

失敗例

$ ./test.sh /root
pre
./test.sh: line 16: cd: /root: 許可がありません
CATCH
FINALLY
53
44
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
53
44