- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2019-01-12T03:48:28+00:00","default:haikikyou","haikikyou")
#author("2019-01-30T13:20:51+00:00","default:haikikyou","haikikyou")
#contents
* パラメータ展開 [#mf070ac4]
*** ${parameter} [#lc33dd13]
parameterの値に置換される。
#geshi(bash){{{
#!/usr/bin/env bash
PARAM="parameter"
echo $PARAM #=> "parameter"
}}}
*** ${parameter:-word} [#r6cf08cd]
parameter が設定されていないか空文字列であれば、 wordを展開したものに置換される。そうでなければ、 parameter の値に置換される。
変数のデフォルト値を設定したい場合などによく使う。
#geshi(bash){{{
echo $USER #=> "moritetu"
# ${parameter:-word}
SSH_USER="${USER:-"guest"}"
echo "$SSH_USER" #=> "moritetu"
SSH_OPTS="${OPTIONS:-""}"
echo "$SSH_OPTS" #=> ""
}}}
*** ${parameter:=word} [#h57e3e86]
parameterが設定されていないか空文字列であれば、 wordを展開したものがparameter に代入される。その後、parameter の値への置換が行われる。
#geshi(bash){{{
# ${parameter:=word}
# ex1
${COMMAND:="date"} #=> "2019年 1月30日 水曜日 22時10分22秒 JST"
# ex2
: ${PARAM:="param1"}
echo $PARAM #=> "param1"
}}}
*** ${parameter:?word} [#v638fa90]
- parameterが空文字列または設定されていない場合、word を展開したものが標準エラー出力に出力される。
- wordがなければ パラメータが空文字列または設定されていないことを示すメッセージが標準エラー出力に出力される。
- 対話的シェルでなければ、 シェルは終了する。
- parameterに空文字列以外が設定されていれば、 parameter 値への置換が行われる。
#geshi(bash){{{
# ${parameter:?word}
# ex1
${not_defined_param:?} #=> not_defined_param: パラメータが null または設定されていません
# シェルは終了する
# ex2
${not_defined_param2:?"parameter is not defined"} #=> not_defined_param2: parameter is not defined
# シェルは終了する
# ex3
defined_param2="defined_param2"
echo ${defined_param2:?"parameter is not defined"} #=> "defined_param2"
}}}
*** ${parameter:+word} [#oe3b5ed1]
*** ${parameter:offset} [#ld6dac40]
*** ${parameter:offset:length} [#jd26b524]
*** ${!prefix*} [#rb7f2b5d]
*** ${!prefix@} [#gcbf3e51]
*** ${!name[@]} [#vf3ba1d3]
*** ${!name[*]} [#e583b691]
*** ${#parameter} [#h4f5448b]
*** ${parameter#word} [#w735de22]
*** ${parameter##word} [#h7b52b17]
*** ${parameter%word} [#pfe93282]
${parameter%%word}
${parameter/pattern/string}
${parameter^pattern}
${parameter^^pattern}
${parameter,pattern}
${parameter,,pattern}
* プロセス置換 [#we760055]
- &code(){>(list)};は、ファイルへの出力がlistへの入力となる。
- &code(){<(list)};は、listの出力がファイルへの入力となる。
&label(sample){サンプル}; ''lsの結果で*.shファイルのみを出力''
#geshi(bash,number){{{
a=
while IFS= read -r line
do
a="$line:$a"
done < <(ls . | grep -e "\.sh$")
}}}
&label(sample){サンプル}; ''a.txtとb.txtをdiffの入力ファイルとして比較''
#geshi(bash,number){{{
diff -u <(cat a.txt) <(cat b.txt)
}}}
&label(sample){サンプル};
#geshi(bash,number){{{
exec 3>&1
exec > >(while read line; do echo "$(date): $line";done)
echo "hello"
echo "bar"
exec 1>&3 3>&-
}}}
* execを使った入出力のリダイレクト [#s12f0ef4]
execコマンドでリダイレクトだけを指定すると、シェル自身のファイル記述子とデバイスの対応を変更することができる。
**** &label(sample){例}; execを使った入力のリダイレクト [#z15e5164]
#geshi(bash,number){{{
# copy stdin to descriptor 3
exec 3<&0
# redirect stdin to test.c
exec < test.c
# read a line from test.c
read line
echo $line
# restore stdin and close descriptor 3
exec 0<&3 3<&-
}}}
**** &label(sample){例}; execを使った出力のリダイレクト [#z15e5164]
#geshi(bash,number){{{
# save stdout as descriptor 3
exec 3>&1
exec > test.txt
echo "this message is written into the test.txt"
# restore stdout and close descriptor 3
exec 1>&3 3>&-
}}}
特定の式や文の出力をまとめてリダイレクトしたい場合は、ブレースを使った複合コマンドでも処理できる。
&label(sample){例}; ''複合コマンドの出力をファイルにリダイレクトする''
#geshi(bash,number){{{
#!/usr/bin/env bash
# 複合コマンド
{
# stdout.txtに出力
echo "this message is written into stdout.txt"
# stderr.txtに出力
echo "this message is written into stderr.txt" >&2
} > stdout.txt 2> stderr.txt
}}}
* 参考リンク [#q8d32fb3]
- [[Advanced Bash-Scripting Guid>http://tldp.org/LDP/abs/html/index.html]] - &size(11){&color(gray){on http://tldp.org/LDP/abs/html/index.html};};
- [[Man page of BASH>https://linuxjm.osdn.jp/html/GNU_bash/man1/bash.1.html]] - &size(11){&color(gray){on https://linuxjm.osdn.jp/html/GNU_bash/man1/bash.1.html};};
- テストフレームワーク
-- [[Bats>https://github.com/sstephenson/bats]] - &size(11){&color(gray){on https://github.com/sstephenson/bats};};
-- [[shunit2>https://github.com/kward/shunit2]] - &size(11){&color(gray){on https://github.com/kward/shunit2};};
--- [[Pac Learner shUnit2 2.1.x ドキュメント>https://sites.google.com/site/paclearner/shunit2-documentation]] - &size(11){&color(gray){on https://sites.google.com/site/paclearner/shunit2-documentation};};
-- [[Baut (Bash Unittest Tool)>https://github.com/moritetu/baut]] - - &size(11){&color(gray){on https://github.com/moritetu/baut};};