Last active
June 8, 2016 16:34
-
-
Save m-yamashita/343f68d579358da600d6 to your computer and use it in GitHub Desktop.
初心者向け、「上手い」シェルスクリプトの書き方メモ ref: http://qiita.com/m-yamashita/items/889c116b92dc0bf4ea7d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
: > foo.log # ファイルを 0 バイトで上書き |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# $foo が $bar より大きければ 0 、そうでなければ 1 を $baz に代入する。 | |
[ $foo -ge $bar ] && baz=0 || baz=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 一時ディレクトリ/ファイルを作成する。 | |
temp_file=$(mktemp) | |
temp_dir=$(mktemp -d) | |
# 後始末を定義 | |
trap " | |
rm $temp_file | |
rm $temp_dir | |
" 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set -e | |
ls /path/to/file # ls コマンドでファイルが見つからない場合この時点でエラーとなる | |
cat /path/to/file # 上記でエラーとなった場合この処理が実行される前にスクリプトが中断される。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set -e | |
# 〜 中略 〜 | |
[ $hoge -ge $fuga ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set -eu | |
# 引数が3つ渡されていなければスクリプト実行中止 | |
: $1 $2 $3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
./foo.sh: 行 2: $1: 未割り当ての変数です |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
: ここにコメントが書ける、コンソールに出力される。 | |
: "特殊な記号等を利用するときはクォーテーションで括る(| や & 等)" | |
: " | |
こうすれば | |
複数コメントも書ける。 | |
ちゃんと改行もされて出力される。 | |
" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set -eux | |
# 中略 | |
set +u # 一時的に -u オプションを無効化。 | |
foo=$1 # この $1 は未定義かもしれないが、未定義でもエラーとならない。 | |
echo $foo | |
set -u # -u オプションを元通り付与し直す。 | |
# 以降の処理 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# $foo が $bar より大きければ 0 、そうでなければ 1 を $baz に代入する。 | |
if [ $foo -ge $bar ]; then | |
baz=0 | |
else | |
baz=1 | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# $foo に $bar を代入。但し $bar が未定義だった場合は 0 を代入 | |
foo=${bar:-0} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# $bar が未定義だった場合、 $foo にも $bar にも 0 を代入 | |
foo=${bar:=0} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
script_dir_path=$(dirname $(readlink -f $0)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
script_dir_path=$(dirname $(greadlink -f $0)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trap "rm /tmp/temporary-file" 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trap " | |
mv /tmp/swap-file original-file | |
rm /tmp/target-file | |
" 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
temp_file=$(mktemp) # /tmp 配下にランダムな名前のファイルが作成される。 例: /tmp/tmp.C3N9Ng6IaU | |
temp_dir=$(mktemp -d) # -d を付けることでディレクトリ作成となる。 例: /tmp/tmp.wDOVMXVcio |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment