Skip to content

Instantly share code, notes, and snippets.

@bash0C7
Last active December 20, 2015 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bash0C7/6061782 to your computer and use it in GitHub Desktop.
Save bash0C7/6061782 to your computer and use it in GitHub Desktop.
bash set -e option

parent.sh

呼び出し元。set -exののち、スタートメッセージを表示 -> 引数で渡されたシェルスクリプトをsource -> エンドメッセージ表示

child*.sh

呼び出される子供。

child_set_e.sh

set -eのち、スタートメッセージを表示 -> falseを実行(つまりここでコマンド失敗) -> エンドメッセージ表示

child_no_set_e.sh

スタートメッセージを表示 -> falseを実行(つまりここでコマンド失敗) -> エンドメッセージ表示 ここではset -eとかやらない。

echo start ko
false
echo end ko
function func() {
echo $1
return 1
}
echo start ko
func 'do func'
echo end ko
set -e
echo start ko
false
echo end ko
set -ex
echo oya start
source $@
echo oya end
set -ex
trap 'echo !trapped!' EXIT
echo oya start
source $1 || echo error
echo oya end
@bash0C7
Copy link
Author

bash0C7 commented Jul 23, 2013

mac

$ bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12)
Copyright (C) 2007 Free Software Foundation, Inc.
$ sh --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12)
Copyright (C) 2007 Free Software Foundation, Inc.
$ bash parent.sh child_no_set_e.sh
+ echo oya start
oya start
+ source child_no_set_e.sh
++ echo start ko
start ko
++ false
 bash parent.sh child_set_e.sh   
+ echo oya start
oya start
+ source child_set_e.sh
++ set -e
++ echo start ko
start ko
++ false

@bash0C7
Copy link
Author

bash0C7 commented Jul 23, 2013

sl

$ bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ sh --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ bash parent.sh child_no_set_e.sh
+ echo oya start
oya start
+ source child_no_set_e.sh
++ echo start ko
start ko
++ false
$ bash parent.sh child_set_e.sh
+ echo oya start
oya start
+ source child_set_e.sh
++ set -e
++ echo start ko
start ko
++ false

@bash0C7
Copy link
Author

bash0C7 commented Jul 23, 2013

sl root

# bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
# sh --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
# bash parent.sh child_set_e.sh
+ echo oya start
oya start
+ source child_set_e.sh
++ set -e
++ echo start ko
start ko
++ false
# bash parent.sh child_no_set_e.sh
+ echo oya start
oya start
+ source child_no_set_e.sh
++ echo start ko
start ko
++ false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment