- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2018-03-18T02:49:33+00:00","default:haikikyou","haikikyou")
[[HaikikyouのIT関連技術メモ]]
#contents
* 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/moritoru81/baut]] - - &size(11){&color(gray){on https://github.com/moritoru81/baut};};