Redirection/Pipe

4287 ワード

Ⅰ. cat
  • $ cat file:Stdoutにファイルを出力
  • ファイルが指定されていない場合のStdin受信
  • jsg@jsg-ubuntu:~$ cat testdir/hello.txt
    Hello World
    jsg@jsg-ubuntu:~$ cat
    writing input
    writing input
    Ⅱ. tee
  • $ tee file:stdinをstdoutとファイルに接続する
  • jsg@jsg-ubuntu:~$ echo tee demonstration | tee redirection.txt
    tee demonstration
    jsg@jsg-ubuntu:~$ cat redirection.txt
    tee demonstration
    Ⅲ. Redirection
  • チャンネルをファイルに向ける
  • 1. A > File
  • Aのstdoutがファイルに接続されている
  • Aはnull command(":",省略)ファイルクリア
  • jsg@jsg-ubuntu:~$ (date && errorcommand) > redirection.txt
    errorcommand: 명령을 찾을 수 없습니다
    jsg@jsg-ubuntu:~$ cat redirection.txt 
    2020. 12. 18. (금) 18:33:54 KST
    jsg@jsg-ubuntu:~$ : > redirection.txt
    jsg@jsg-ubuntu:~$ cat redirection.txt 
    jsg@jsg-ubuntu:~$ 
    2. A n> File
  • Aのn番fdがファイルに接続されている
  • jsg@jsg-ubuntu:~$ (date && errorcommand) 2> redirection.txt
    2020. 12. 18. (금) 18:35:26 KST
    jsg@jsg-ubuntu:~$ cat redirection.txt 
    errorcommand: 명령을 찾을 수 없습니다
    3. A &> File
  • Aのstdoutとstderrがファイルに接続されている
  • jsg@jsg-ubuntu:~$ (date && errorcommand) &> redirection.txt
    jsg@jsg-ubuntu:~$ cat redirection.txt 
    2020. 12. 18. (금) 18:38:00 KST
    errorcommand: 명령을 찾을 수 없습니다
    4. A n>&m
  • Aのn番fd代替ファイル
  • jsg@jsg-ubuntu:~$ cat test.sh
    #! bin/bash
    echo Error >&2
    jsg@jsg-ubuntu:~$ bash test.sh > /dev/null
    Error
    5. A >> File
  • A>Bですが上書きせずに追加
  • jsg@jsg-ubuntu:~$ echo "Appended text" >> redirection.txt 
    jsg@jsg-ubuntu:~$ cat redirection.txt 
    2020. 12. 18. (금) 18:38:00 KST
    errorcommand: 명령을 찾을 수 없습니다
    Appended text
    6. A < File
  • Aのstdinがファイルに接続されている
  • jsg@jsg-ubuntu:~$ cat < redirection.txt 
    2020. 12. 18. (금) 19:09:42 KST
    7. A <$n
  • n番fdがAのstdin
  • 6. A << HERE
  • 次のHEREが出るまで全ての入力をAに転送
  • jsg@jsg-ubuntu:~$ cat << HERE
    sentences
    more sentences
    HERE
    sentences
    more sentences
    4. A <<< text
  • Aのstdinがテキストに接続されている
  • jsg@jsg-ubuntu:~$ cat <<< redirection.txt
    redirection.txt
    Ⅵ. Pipe
  • プロセス間での一方向通信を許可する
  • 1. A | B
  • Aの出力→Bの入力接続
  • AはBに接続するには終了しなければならない
  • jsg@jsg-ubuntu:~$ ls | wc -l	   # ls 에서 받은 출력값을 입력값으로 받아 count함
    11
    2. A >>(B)
  • A|Bは似ているが、Aが終端状態でなくてもうまく迂回できる
  • jsg@jsg-ubuntu:~$ cat test.sh
    #! /bin/bash
    while :
    do
    	sleep 5
    	echo Good $(date)
    	echo Err $(date) >&2
    done
    
    jsg@jsg-ubuntu:~$ cat log.sh
    #! /bin/bash
    while read line
    do
    	if [ $1 == ERR ]
    	then
    		echo $line >> err.txt
    	else
    		echo $line >> log.txt
    	fi
    done
    
    jsg@jsg-ubuntu:~$ ./test.sh > >(./log.sh NOR) 2> >(./log.sh ERR) &
    [1] 29207
    jsg@jsg-ubuntu:~$ cat log.txt
    Good 2020. 12. 18. (금) 21:10:00 KST
    jsg@jsg-ubuntu:~$ cat err.txt
    Err 2020. 12. 18. (금) 21:10:00 KST
    Err 2020. 12. 18. (금) 21:10:05 KST
    4. named pipe
  • ファイル形式で表示
  • $ mkfifo pipe_name生成
  • # terminal 1
    jsg@jsg-ubuntu:~$ mkfifo myfifo
    jsg@jsg-ubuntu:~$ ls -l myfifo
    prw-rw-r-- 1 jsg jsg 0 Dec 10 20:45 myfifo
    jsg@jsg-ubuntu:~$ cat testdir/hello.txt > myfifo
    
    # terminal 2
    jsg@jsg-ubuntu:~$ cat myfifo
    Hello World
    Ⅴ. 雑誌式
    1. noclobber
  • set -o noclobberカバー防止
  • >|強制力
  • jsg@jsg-ubuntu:~$ set -o noclobber
    jsg@jsg-ubuntu:~$ echo Rewrite > redirection.txt 
    bash: redirection.txt: cannot overwrite existing file
    jsg@jsg-ubuntu:~$ echo Rewrite >| redirection.txt 
    jsg@jsg-ubuntu:~$ cat redirection.txt
    Rewrite
    2. code block
  • code blockとして括弧を使用
  • jsg@jsg-ubuntu:~$ {
    echo Date is ....
    date
    } > redirection.txt 
    jsg@jsg-ubuntu:~$ cat redirection.txt
    Date is ....
    2020. 12. 18. (금) 21:30:09 KST