bash_ リダイレクト

exec でリダイレクト先を指定する

ファイルを分ける

exec 2>> stderr.log 1>> stdout.log

ファイルを一緒にする

exec >> stdouterr.log 2>&1

リダイレクト 入力と出力 - UNIX & Linux コマンド・シェルスクリプト リファレンス

“{}” のグループをリダイレクト

{
echo "1. hogehoge"
echo "2. fugafuga"
echo "3. foofoo"
echo "4. barbar"
} >>file

ヒアドキュメントをリダイレクト

cat <<__END_OF_MESSAGE__ >>file
1. hogehoge
2. fugafuga
3. foofoo
4. barbar
__END_OF_MESSAGE__

for 文のブロックをリダイレクト

for i in `seq 1 10`; do
  echo "number $i"
done >>file

if [ -f "$str" ]; then
  ls "fugafuga"
  else
  echo "FUGAFUGA"
fi >>file

case "$str" in
"hoge" ) ls "hogehoge" ;;
"fuga" ) cat "fugafuga" ;;
* ) echo "NG..." ;;
esac >>file

関数全体をリダイレクト

func()
{
[ $#- ne 3 ] && return 1

echo "NO MESSAGE..."
echo "NO ERROR MESSAGE..." 1>&2

grep -v "$1" $2 >$3
} >/dev/null 2>&1

複数コマンドの出力をまとめてパイプに流す

複数コマンドの出力をまとめてパイプに流し込みたいときは、 “{}” によるコマンドのグルーピング機能を利用する。

{ echo "hogehoge"; echo "fugafuga"; } | wc -l

→ “{}” で複数コマンドをグループ化することにより出力を一つにまとめる。

コマンドの数が多い場合は、次のように記述すると可読性が高くなる。

{
date
echo "hoge"
echo "HOGE"
echo "fuga"
echo "FUGA"
} | tee -a file

23:54:18.660445 IP (tos 0x0, ttl 249, id 64056, offset 0, flags [DF], proto TCP (6), length 113)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xcd71 (correct), seq 4343:4404, ack 582, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 61

23:54:18.660811 IP (tos 0x0, ttl 249, id 64057, offset 0, flags [DF], proto TCP (6), length 87)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x30ba (correct), seq 4404:4439, ack 677, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 35

23:54:18.661562 IP (tos 0x0, ttl 249, id 64058, offset 0, flags [DF], proto TCP (6), length 83)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x9550 (correct), seq 4439:4470, ack 677, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 31

23:54:18.661955 IP (tos 0x0, ttl 249, id 64059, offset 0, flags [DF], proto TCP (6), length 279)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x8ab1 (correct), seq 4470:4697, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 227

23:54:18.662740 IP (tos 0x0, ttl 249, id 64060, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0xcc8a (correct), seq 4697:5925, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663309 IP (tos 0x0, ttl 249, id 64061, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x4776 (correct), seq 5925:7153, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663464 IP (tos 0x0, ttl 249, id 64062, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0x76f1 (correct), seq 7153:8381, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663886 IP (tos 0x0, ttl 249, id 64063, offset 0, flags [DF], proto TCP (6), length 495)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xda23 (correct), seq 8381:8824, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 443

23:54:18.663905 IP (tos 0x0, ttl 249, id 64064, offset 0, flags [DF], proto TCP (6), length 2508)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xe7ea (incorrect -> 0xb83e), seq 8824:11280, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 2456

23:54:18.732033 IP (tos 0x0, ttl 249, id 64066, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0x9f86 (correct), seq 11280:12508, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.732226 IP (tos 0x0, ttl 249, id 64067, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x7334 (correct), seq 12508:13736, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.732226 IP (tos 0x0, ttl 249, id 64067, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x7334 (correct), seq 12508:13736, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.660445 IP (tos 0x0, ttl 249, id 64056, offset 0, flags [DF], proto TCP (6), length 113)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xcd71 (correct), seq 4343:4404, ack 582, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 61

23:54:18.660811 IP (tos 0x0, ttl 249, id 64057, offset 0, flags [DF], proto TCP (6), length 87)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x30ba (correct), seq 4404:4439, ack 677, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 35

23:54:18.661562 IP (tos 0x0, ttl 249, id 64058, offset 0, flags [DF], proto TCP (6), length 83)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x9550 (correct), seq 4439:4470, ack 677, win 127, options [nop,nop,TS val 3757013497 ecr 318662817], length 31

23:54:18.661955 IP (tos 0x0, ttl 249, id 64059, offset 0, flags [DF], proto TCP (6), length 279)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x8ab1 (correct), seq 4470:4697, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 227

23:54:18.662740 IP (tos 0x0, ttl 249, id 64060, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0xcc8a (correct), seq 4697:5925, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663309 IP (tos 0x0, ttl 249, id 64061, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x4776 (correct), seq 5925:7153, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663464 IP (tos 0x0, ttl 249, id 64062, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0x76f1 (correct), seq 7153:8381, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 1228

23:54:18.663886 IP (tos 0x0, ttl 249, id 64063, offset 0, flags [DF], proto TCP (6), length 495)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xda23 (correct), seq 8381:8824, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 443

23:54:18.663905 IP (tos 0x0, ttl 249, id 64064, offset 0, flags [DF], proto TCP (6), length 2508)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0xe7ea (incorrect -> 0xb83e), seq 8824:11280, ack 782, win 127, options [nop,nop,TS val 3757013498 ecr 318662817], length 2456

23:54:18.732033 IP (tos 0x0, ttl 249, id 64066, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [.], cksum 0x9f86 (correct), seq 11280:12508, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.732226 IP (tos 0x0, ttl 249, id 64067, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x7334 (correct), seq 12508:13736, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228

23:54:18.732226 IP (tos 0x0, ttl 249, id 64067, offset 0, flags [DF], proto TCP (6), length 1280)

ec2-13-228-199-255.ap-southeast-1.compute.amazonaws.com.https > notebookpc.52384: Flags [P.], cksum 0x7334 (correct), seq 12508:13736, ack 782, win 127, options [nop,nop,TS val 3757013568 ecr 318662888], length 1228