본문 바로가기

Developments/Linux

[Linux 실습] Ch8.1. 리다이렉션 기본(Redirection Basic)

오픈 소스 비영리 기관 'Open Source Initiative'의 로고. https://opensource.org/

본 글은 학교 '오픈소스 소프트웨어 입문' 과목을 수강하며 실습(또는 공부)한 내용을 정리한 글입니다.

개인 기록 용도로 작성한 글이라, 직접 이 글을 보고 실습을 따라하기엔 어려움이 많을 것으로 예상됩니다.
혹시 본 글을 보며 Linux를 공부하실 목적이라면, 일단 키워드 위주로 AI에게 물어봐가면서, 본인의 Linux 터미널에서 직접 타이핑해가며 공부하시기를 추천드립니다.

질문이나 의견이 있다면 자유롭게 댓글 달아주세요!

Redirection

stdin, stdout, stderr, pipe - 0< 1> 2>  |
0<   <    <<EOF    <<<input
1>  > 
2>  
&>  1>&2   2>&1  
|   |&          (Pipe executed in subshell)
exec 4<&0    exec 4>&0  Create and connect
exec 3<&-    exec 3>&0  Remove 

Command

mknod		make b/c device
mkfifo 		make named pipe
중간고사의 25%가 이거라고 했음 중요.
다시 돌아가서, 컴퓨터 시스템의 구성 요소에는
(컴퓨터) 하드웨어, 소프트웨어, 데이터, 유저였음.
근데 하드웨어 시스템은 저렇게 화살표 방향이 중요했음.
그리고 소프트웨어는 app.랑 시스템 소프트웨어가 있는데,
시스템 소프트웨어중에 대표적인 게 OS였음.
근데 OS는 컴퓨터 하드웨어가 없으면 OS는 정의가 안됨.
OS는 컴퓨터 하드웨어를 효과적으로 쓸 수 있게 만들어주는 시스템 소프트웨어임.

근데, 하드웨어 관점에서 유저의 아웃풋 장치를 컴퓨터의 인풋 장치에,
그리고 그 반대도 연결하는데, 이걸 연구하는 학문이 HCI.

근데, 운영체제 입장에서 봤을 땐, 유저는 쉘에 연결되는데,
터미널이 쉘을 실행시켜야 하니까, 터미널이 실행되면,
stdin, stdout, stderr 3개의 스트림이 생김.
그렇게 터미널이 쉘을 실행시키면 쉘 프롬포트가 뜨고 커서가 깜빡거림.
이 때, stdout과 stderr는 터미널의 화면에 할당됨.
그리고 stdin은 키보드에 할당됨.
이 때, stdout와 stderr은 화면으로 출력되는데,
이 화면으로 출력되는 애들의 출력 방향을 바꾸고 싶은 게
리다이렉션임.

이 떄, stdin을 0<라고 쓰고, 
stdout을 1>라고 쓰고,
stderr을 2>라고 쓰고,
pipe를 |라고 씀.
(정확힌 저 앞에 스트림을 리다이렉션하는 기호가 뒤에 꺼인듯)

pipe는 앞에 명령의 stdout을 뒤의 명령의 stdin에 연결해 줌.

0<과 <는 같음. 즉, 0을 생략할 수 있음.
1>과 >은 같음. 즉, 1을 생략할 수 있음.
그러나, 2>와 >는 다름. >는 1>임. 2>에서 2는 생략 불가.
실습
cd lab0325
touch a b c

ls: ls 다음에 나오는 파일이나 디렉토리의 content(내용)을 보여줌.

여기서 ls d하면,
에러 메시지가 stderr로 출력됨.

foss110@ajousw:~/lab0325$ ls a d
ls: cannot access 'd': No such file or directory
a
-> 첫 줄의 에러 메시지는 stderr로,
두 번째 줄의 a는 stdout로 출력되었음.

!시험문제!

1. ls a d 1> out.txt 실행 시 화면에 나오는 결과는?
-> d에 대한 에러 메시지가 뜸. (그냥 똑같이 따라 적어라 시험에선.)
foss110@ajousw:~/lab0325$ ls a d 1> out.txt
ls: cannot access 'd': No such file or directory
그리고 out.txt에 ls a의 실행 결과가 들어감.
2. cat out.txt 실행 시 화면에 나오는 결과는?
-> a

교수) 이렇게 내도 중간고사 평균 75점밖에 안나옴.

3. ls a d 2> err.txt 실행 시 화면에 나오는 결과는?
-> a

4. ls a d 2> err.txt 1> out.txt 실행 시 화면에 나오는 결과는?
-> 아무것도 안 나옴.

---
이제부터 variation이 막 들어감.
100점 중 25점을 교수가 리다이렉션에 쓰는 이유가 있음.

1. 리다이렉션은 그 위치와 무관하게 실행됨.
ls a d 2> err.txt > out.txt
2> err.txt > out.txt ls a d
위 두개가 같은 명령어임. 즉, 리다이렉션의 위치는 아무데나 있어도 됨.
2> err.txt ls a d > out.txt
위처럼 실행할 수도 있음.

2. 리다이렉션 대상 파일은 명령어 실행 전에 우선적으로 생성됨.
pipe는 앞에 명령어의 stdout을 뒤에 명령어의 stdin으로 집어넣음.
foss110@ajousw:~/lab0325$ ls | wc
      5       5      22
foss110@ajousw:~/lab0325$ ls | wc -l
5
(그냥 줄 수만 센 거.)

foss110@ajousw:~/lab0325$ ls > ls.txt
foss110@ajousw:~/lab0325$ wc -l ls.txt
6 ls.txt
이거 이번왠 왜 6일까?
-> ls 명령어을 먼저 실행하고 그 결과를 ls.txt에 넣는 게 아니라,
일단 ls.txt 파일을 먼저 만들어 놓고 ls 명령어를 실행했단 거임.
리다이렉션 대상 파일은 명령어 실행 전에 우선적으로 생성된다고!!!

foss110@ajousw:~/lab0325$ ls d > ttt.txt
ls: cannot access 'd': No such file or directory
foss110@ajousw:~/lab0325$ ls
a  b  c  err.txt  ls.txt  out.txt  ttt.txt
따라서, 위와 같은 경우에도 ls가 에러가 나지만,
ttt.txt 파일은 ls 실행 전에 우선 생성됨.

foss110@ajousw:~/lab0325$ lllsss > ttttttt
lllsss: command not found
foss110@ajousw:~/lab0325$ ls
a  b  c  err.txt  ls.txt  out.txt  ttttttt  ttt.txt
심지어, 위처럼 아예 없는 명령어를 입력해도 파일이 생성됨.

3. 중요!!!
ls > ls.txt
wc ls.txt
-> 8 8 45 ls.txt
이거 ls.txt라는 파일이 원래 있었기 때문에, 그 개수가 변하지 않음.
(뭔 소리지 8 유지된단 건 알겠는데)
(아마 ls 결과인 8개에서 9가 되지 않는다는듯)

ls {a..z}
foss110@ajousw:~/lab0325$ ls {a..z}
ls: cannot access 'd': No such file or directory
ls: cannot access 'e': No such file or directory
ls: cannot access 'f': No such file or directory
ls: cannot access 'g': No such file or directory
ls: cannot access 'h': No such file or directory
ls: cannot access 'i': No such file or directory
ls: cannot access 'j': No such file or directory
ls: cannot access 'k': No such file or directory
ls: cannot access 'l': No such file or directory
ls: cannot access 'm': No such file or directory
ls: cannot access 'n': No such file or directory
ls: cannot access 'o': No such file or directory
ls: cannot access 'p': No such file or directory
ls: cannot access 'q': No such file or directory
ls: cannot access 'r': No such file or directory
ls: cannot access 's': No such file or directory
ls: cannot access 't': No such file or directory
ls: cannot access 'u': No such file or directory
ls: cannot access 'v': No such file or directory
ls: cannot access 'w': No such file or directory
ls: cannot access 'x': No such file or directory
ls: cannot access 'y': No such file or directory
ls: cannot access 'z': No such file or directory
a  b  c

저기서 에러메시지를 딴데 좀 버리고 싶으면,
ls {a..z} 2> err.txt하면
a  b  c
만 출력됨.

근데, 저건 err.txt 파일을 생성하잖아. 난 그냥 버리고 싶은데.
-> 버릴 때 쓰는 파일이 있음.
/dev/null
-> 저 파일로 입력되는 내용들은 전부 버림.
ls {a..z} 2> /dev/null