Linux system

Shell Script: Redirection ์‹ค์Šต

NENGIN 2024. 3. 27. 17:37

Redirection์ด๋ž€

๋ช…๋ น์ด ์‹คํ–‰๋˜๊ธฐ ์ „์— ์…ธ์—์„œ ํ•ด์„๋˜๋Š” ํŠน์ˆ˜ ํ‘œ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ

๋ช…๋ น์˜ ์ž…/์ถœ๋ ฅ์„ ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•˜๋Š” ๋ฐฉํ–ฅ์œผ๋กœ Redirectionํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.

๋ช…๋ น์˜ ํŒŒ์ผ ํ•ธ๋“ค์„ ๋ณต์ œํ•˜๊ณ , ์—ด๊ณ , ๋‹ซ๊ณ , ๋‹ค๋ฅธ ํŒŒ์ผ์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ช…๋ น์ด ์ฝ๊ณ  ์“ฐ๋Š” ํŒŒ์ผ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

1๊ฐœ์˜ ์ž…๋ ฅ๊ณผ 2๊ฐœ์˜ ์ถœ๋ ฅ stream ์„ ๊ฐ€์ง, ํ‚ค๋ณด๋“œ๋กœ ์ž…๋ ฅํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ 2๊ฐœ์˜ ์ถœ๋ ฅ stream์œผ๋กœ ์ถœ๋ ฅํ•จ


 

Three Standard File Descriptors

> , >> , <,  0, 1, 2 ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค

  • stdin (File Descriptor 0)
  • stdout (File Descriptor 1)
  • stderr (File Descriptor 2)

 

ํ‘œ์ค€ ์ž…๋ ฅ stdin (0)

ํ‚ค๋ณด๋“œ ์ž…๋ ฅ ๋‚ด์šฉ ์ฆ‰์‹œ ๋ชจ๋‹ˆํ„ฐ๋กœ ์ถœ๋ ฅ

[root@localhost ~]# cat
1 //์ž…๋ ฅ
1 //์ถœ๋ ฅ
2 //์ž…๋ ฅ
2 //์ถœ๋ ฅ
3 //์ž…๋ ฅ
3 //์ถœ๋ ฅ
4 //์ž…๋ ฅ
4 //์ถœ๋ ฅ

 

sort ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์žฌ์ •๋ ฌ

[root@localhost ~]# sort
2 //์ž…๋ ฅ
5
3
4
1 
//[ctrl]+[d]
1 //์ถœ๋ ฅ
2
3
4
5

 

ํ‚ค๋ณด๋“œ๋กœ ์ž…๋ ฅ ๋‚ด์šฉ์„ ํŒŒ์ผ๋กœ ์žฌ์ง€์ •

[root@localhost ~]# cat > /root/tmp.txt
5
8
1
4
3
7
6
2
// [ctrl]+[d]๋กœ ์ข…๋ฃŒ

 

ํŒŒ์ผ์— ์žˆ๋Š” ์ž…๋ ฅ ๋‚ด์šฉ์„ sort ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์žฌ์ •๋ ฌ

[root@localhost ~]# sort < /root/tmp.txt
1
2
3
4
5
6
7
8

 

ํ‘œ์ค€ ์ถœ๋ ฅ stdout (1)

text ํŒŒ์ผ์— ์ถœ๋ ฅ๋‚ด์šฉ ๋„ฃ๊ธฐ

[root@localhost ~]# cat > /root/dir1/dir11.txt
dir11

[root@localhost ~]# cat /root/dir1/dir11.txt 
dir11

[root@localhost ~]# echo textformat >> /root/dir1/dir11.txt 
// ๋ง๋ถ™์ด๊ธฐ : >>

[root@localhost ~]# cat /root/dir1/dir11.txt 
dir11
textformat

 

sort์„ ํ†ตํ•ด ์ •๋ ฌ๋œ ์ถœ๋ ฅ๋‚ด์šฉ์„ ๋‹ค๋ฅธ ํŒŒ์ผ์— ๋„ฃ๊ธฐ

//๊ธฐ์กด tmp.txt ํŒŒ์ผ์˜ ๋‚ด์šฉ
[root@localhost ~]# cat /root/tmp.txt
5
8
1
4
3
7
6
2

// tmp.txtํŒŒ์ผ ๋‚ด์šฉ์„ sort๋กœ ์ •๋ ฌํ•œ๋‹ค
// ์ •๋ ฌ๋œ ์ถœ๋ ฅ๋‚ด์šฉ์„ hahaha.txtํŒŒ์ผ์— ์ถœ๋ ฅํ•œ๋‹ค
[root@localhost ~]# sort < /root/tmp.txt 1> /root/hahaha.txt

// hahaha.txt ํŒŒ์ผ์˜ ๋‚ด์šฉ
[root@localhost ~]# cat /root/hahaha.txt 
1
2
3
4
5
6
7
8

 

ํ‘œ์ค€ ์—๋Ÿฌ ์ถœ๋ ฅ stderr (2)

๊ธฐ์กด ์—๋Ÿฌ ์ถœ๋ ฅ ํ™•์ธ

[root@localhost ~]# cat /root/kim.txt > /root/err1.txt
cat: /root/kim.txt: ๊ทธ๋Ÿฐ ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค

 

์—๋Ÿฌ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ err1.txt ํŒŒ์ผ๋กœ ์ง€์ •

// err1.txt๋กœ ์ง€์ •
[root@localhost ~]# cat /root/kim.txt 2> /root/err1.txt

// err1.txt ์ถœ๋ ฅ ๊ฒฐ๊ณผ : ์—๋Ÿฌ ์ถœ๋ ฅ๋ฌธ
[root@localhost ~]# cat /root/err1.txt 
cat: /root/kim.txt: ๊ทธ๋Ÿฐ ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค

 

์—๋Ÿฌ ์ถœ๋ ฅ(2)์„ ํ‘œ์ค€ ์ถœ๋ ฅ(1)์œผ๋กœ ํƒœ์›Œ๋ณด๋‚ด๊ธฐ

[root@localhost ~]# cat /root/kim.txt 2>&1 /dev/null
cat: /root/kim.txt: ๊ทธ๋Ÿฐ ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค

 

wc ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•œ t1.txt ํŒŒ์ผ์˜ ๊ฒฐ๊ณผ๋ฅผ t2.txt๋กœ ์ง€์ •

// t1.txt ํŒŒ์ผ ์ƒ์„ฑ
[root@localhost ~]# vim /root/t1.txt

a
ab
abc
abcd
abcde

// t2.txt๋กœ ์ง€์ •
[root@localhost ~]# wc < /root/t1.txt > /root/t2.txt

// ์ถœ๋ ค๊ฒฐ๊ณผ
[root@localhost ~]# cat /root/t2.txt 
 5  5 20