Linuxコマンドラインとshellスクリプト(11)--入出力リダイレクト
3396 ワード
入力と出力の理解
ls -al badfile 2> test4
ls -al test test2 test3 badtest 2> test6 1> test7
#エラーはtest 6に入力され、通常のデータはtest 7 ls -al test test2 &> test8
#標準エラーと標準入力をtest 8 スクリプトで出力をリダイレクト
echo "This is an error message" >&2
exec
コマンドを使用してshellスクリプトに実行中に特定のファイル記述子echo "This is the start of the script";
echo "now redirecting all output to another location";
exec 1> testout
exec 2> testerror
echo "This output should go to the testout file";
echo "This output should go to the testerror file" >&2;
スクリプトへの入力のリダイレクト
file="/Users/chenhong/Desktop/shell_workspace/STD.sh";
exec 0< $file
count=1
while read line
do
echo "Line $count:$line";
count=$[ $count + 1 ];
done
独自のリダイレクトを作成
exec 3>testout # exec 3>>testout
echo "This shoud output the STDOUT";
echo "This shoud output the &3" >&3;
exec 3>&-
開いているファイル記述子のリスト
lsof -a -p $$ -d 0,1,2
コマンド出力のブロック
ls -al > /dev/null
cat /dev/null > testfile
メッセージの記録
date | tee testout
date | tee -a testout