Redirection/Pipe
4287 ワード
Ⅰ. catファイルが指定されていない場合のStdin受信 チャンネルをファイルに向ける 1. A > FileAのstdoutがファイルに接続されている Aはnull command(":",省略)ファイルクリア Aのn番fdがファイルに接続されている Aのstdoutとstderrがファイルに接続されている Aのn番fd代替ファイル A>Bですが上書きせずに追加 Aのstdinがファイルに接続されている n番fdがAのstdin 6. A << HERE次のHEREが出るまで全ての入力をAに転送 Aのstdinがテキストに接続されている プロセス間での一方向通信を許可する 1. A | BAの出力→Bの入力接続 AはBに接続するには終了しなければならない A|Bは似ているが、Aが終端状態でなくてもうまく迂回できる ファイル形式で表示 用
1. noclobbercode blockとして括弧を使用
$ cat file
:Stdoutにファイルを出力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
Ⅲ. Redirectionjsg@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> Filejsg@jsg-ubuntu:~$ (date && errorcommand) 2> redirection.txt
2020. 12. 18. (금) 18:35:26 KST
jsg@jsg-ubuntu:~$ cat redirection.txt
errorcommand: 명령을 찾을 수 없습니다
3. A &> Filejsg@jsg-ubuntu:~$ (date && errorcommand) &> redirection.txt
jsg@jsg-ubuntu:~$ cat redirection.txt
2020. 12. 18. (금) 18:38:00 KST
errorcommand: 명령을 찾을 수 없습니다
4. A n>&mjsg@jsg-ubuntu:~$ cat test.sh
#! bin/bash
echo Error >&2
jsg@jsg-ubuntu:~$ bash test.sh > /dev/null
Error
5. A >> Filejsg@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 < Filejsg@jsg-ubuntu:~$ cat < redirection.txt
2020. 12. 18. (금) 19:09:42 KST
7. A <$njsg@jsg-ubuntu:~$ cat << HERE
sentences
more sentences
HERE
sentences
more sentences
4. A <<< textjsg@jsg-ubuntu:~$ cat <<< redirection.txt
redirection.txt
Ⅵ. Pipejsg@jsg-ubuntu:~$ ls | wc -l # ls 에서 받은 출력값을 입력값으로 받아 count함
11
2. A >>(B)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 blockjsg@jsg-ubuntu:~$ {
echo Date is ....
date
} > redirection.txt
jsg@jsg-ubuntu:~$ cat redirection.txt
Date is ....
2020. 12. 18. (금) 21:30:09 KST
Reference
この問題について(Redirection/Pipe), 我々は、より多くの情報をここで見つけました https://velog.io/@legendre13/Redirection-Pipeテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol