#author("2019-01-05T15:24:42+00:00","default:haikikyou","haikikyou")
[[HaikikyouのIT関連技術メモ]]
#contents
* プロセス置換 [#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};};