입출력 리다이렉션

조준형·2022년 12월 29일
0

Linux

목록 보기
4/7

표준입출력

쉘 명령에 대해서는 3가지 기본 입출력 장치가 사용된다.

  • stdin(file decriptor 0, standard input)
    • 표준 입력장치로써 명령에 데이터를 공급한다. 디폴트는 키보드
  • stdout(file descriptor 1, standard output)
    • 표준 출력장치로써 명령의 출력이 전달된다. 디폴트는 터미널 화면
  • stderr(file descriptor 2, standard error)
    • 에러 메시지가 출력되는 장치로써 디폴트는 터미널 화면

출력 리다이렉션

‘>’ 기호를 사용하여 출력 스트림을 변경할 수 있음

  • $echo It is cold today! > winter.txt. # 새 파일을 생성하고 쓴다.
  • $echo It is cold today! > winter.txt # 기존에 있는 파일의 내용을 덮어쓴다
  • $echo It is cold today! >> winter.txt # 기존 내용에 추가한다(append)

에러 리다이렉션

2>를 사용하여 에러 스트림을 변경

  • $find / # root아래의 파일들을 모두 찾는다
  • $find /> allfiles.txt 2> error.txt # 파일 목록과 에러 메시지를 구분하여 저장
  • $find /> allfiles.txt 2> /dev/null # 에러 메시지를 무시
  • $find /> allfiles.txt 2>&1 # 2>&1는 stdout과 stderr를 같은 스트림으로 보낸다
  • $find /2>&1 > allfiles.txt #stderr(2)

파이프(|)

이전 명령의 출력을 다음 명령의 입력으로 전달한다

  • $ command1 | command2
    • ls | more
    • ls -l | wc -l

입력 리다이렉션

<(또는 0<)를 사용하여 표준 입력장치를 변경

  • $sort < names.txt
  • $sort < names.txt > sorted-names.txt
profile
코린이

0개의 댓글