LoginSignup
582
575

More than 5 years have passed since last update.

シェルスクリプトの罠を避ける三つの tips

Last updated at Posted at 2016-12-07

Unix  tips  () 

Shebang  bash 


Bash 使 Bashism Bashism  Bash 


(一)Bash  /bin/sh 使 Bashism  /bin/sh Bashism 

(二) /bin/sh  Bash 1


 Bash  #!/bin/sh  #!/bin/bash 

Bashism 使

Shellcheck 


Shellcheck  lint Haskell 使

set -Ceu 



-C


grep foo bar.log > filtered.log 使-C 

 >  >| 

-e


 0  -e 

 && true 2
# make build が失敗しても make install を実行する。
# しかし make install が失敗したら make clean 以降は実行しない
set -e
make build && true
make install
make clean
...

-u

デフォルトでは、存在しない変数は空文字列に展開されます。これはしばしば悲惨な結果を招きます。例えば下のスクリプトでは変数の名前を打ち間違えているので ~/foo.tmp ではなく ~ が丸ごと削除されてしまいます。

tmpdirname=foo.tmp
rm -fr ~/"$tempdirname"

-u 


 (#!/bin/bash )  set -Ceu 

 bash -Ceu my_script.sh 




Bash ?Shellcheck ?if -e ?




ACCESS Advent Calendar 2016 ACCESS  @bols_blue 


  1.  Debian  Ubuntu  



    && true  || true ?-e || true 使 $?  
582
575
2

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
582
575