devops

Linux 운영체제 2, Standard Stream, Pipeline, Redirection 본문

DevOps/OS

Linux 운영체제 2, Standard Stream, Pipeline, Redirection

vata500 2022. 4. 21. 15:02
반응형

표준스트림과 stdin, stdout, stderr

특정한 프로그래밍 언어 인터페이스뿐 아니라 유닉스 및 유닉스 계열 운영 체제에서 컴퓨터 프로그램과 그 환경(일반적으로 단말기) 사이에 미리 연결된 입출력 통로를 가리킨다.

일반적으로 유닉스에서 동작하는 프로그램은 실행 시 세 개의 스트림이 자동으로 열린다. 이를 표준 스트림이라고 부른다. 하나는 입력을 위한 스트림(Standard input, STDIN, 0), 하나는 출력을 위한 스트림(Standard Output, STDOUT, 1), 하나는 오류 메시지를 출력하기 위한 스트림(Standard Error, STDERR, 2)이며, 이 세 개의 표준 스트림은 사용자의 터미널에 부착된다.

표준 입력 (stdin)
표준 입력은 프로그램으로 들어가는 데이터(보통은 문자열) 스트림이다. 프로그램은 read 명령을 이용하여 데이터 전송을 요청한다. 모든 프로그램이 입력을 요구하는 것은 아니다. 이를테면 dir이나 ls 프로그램(디렉터리에 들어 있는 파일 이름을 보여 주는 명령)은 실행시 옵션과 같은 명령줄 매개변수를 받을 수는 있으나, 동작 중에 데이터 스트림의 입력 없이 명령을 수행한다.

별도의 리다이렉션 없이 프로그램을 시작한 경우, 표준 입력 스트림은 키보드에서 받아온다.

표준 입력을 위한 파일 서술자는 0이다. POSIX <unistd.h> 정의는 STDIN_FILENO이다. 이에 해당하는 <stdio.h> 변수는 FILE* stdin;이다. 이와 비슷하게 <iostream>에서는 std::cin이다.

표준 출력 (stdout)
표준 출력은 프로그램이 출력 데이터를 기록하는 스트림이다. 프로그램은 write 명령을 이용하여 데이터 전송을 요청한다. 모든 프로그램이 출력을 요구하는 것은 아니다. 이를테면 파일 이름 바꾸기 명령(일반적으로 mv, move, ren)은 명령 수행이 성공적이어도 아무 메시지도 나타내지 않는다.

리다이렉션 없이 표준 출력은 프로그램을 시작한 텍스트 터미널이 된다.

표준 출력을 위한 파일 서술자는 1이다. POSIX <unistd.h> 정의는 STDOUT_FILENO이다. 이에 해당하는 <stdio.h> 변수는 FILE* stdout;이다. 이와 비슷하게 <iostream>에서는 std::cout이다.

표준 오류 (stderr)
표준 오류는 프로그램이 오류 메시지나 진단을 출력하기 위해 일반적으로 쓰이는 또다른 출력 스트림이다. 표준 출력과는 독립적인 스트림이며 별도로 리다이렉트될 수 있다.

표준 오류를 위한 파일 서술자는 2이다. POSIX <unistd.h> 정의는 STDERR_FILENO이다. 이에 해당하는 <stdio.h> 변수는 FILE* stderr;이다. <iostream> 표준 헤더는 이 스트림에 관련하여 두 개의 변수 std::cerr과 std::clog를 제공한다. 전자의 것은 버퍼링이 되지 않지만 후자는 버퍼링을 이용한다.

파이프라인(Pipeline)과 리다이렉션(Redirection)이 무엇인지 예시를 들어 설명. 

Pipeline

파이프라인은 파이프라인 앞 명령어의 출력값을 파이프라인 뒤 명령어의 입력값으로 사용할 수 있게 해준다.
위의 예시에서는 ls -l의 출력값을 cut -d'.' -f1의 입력값으로 쓰며 cut -d'.' -f1의 출력값을 sort의 입력값으로 쓰며 마지막 tee dirents 명령어까지 이어지게된다. 

사용은 명령어 중간에 | 를 활용하면된다.

예시 devops가 들어간 로그를 오름차순으로 정렬하고 8번째 글자만 잘라낸다.

Redirection

기본적으로 명령어의 결과는 표준 출력 방식인 모니터에 출력이 된다. 하지만 리다이렉션을 이용하면 명령의 출력을 변경할 수 있다. 리다이렉션을 이용하여 파일에 기록할 수 있다.

기존 파일의 내용을 삭제하고 새로 결과를 저장할 때는 >를,
기존 파일의 내용 뒤에 결과를 추가할 때는 >>를 사용.

  • > : 덮어씀

파이프라인 테스트한 로그를 >를 사용하여 devops.txt에 덮어씀

  • >> : 추가됨

해당 로그 내용을 >>를 사용하여 다시 devops.txt에 추가함

  • < / << : 입력 방향 재지정

cat 명령어에 <를 사용하여 devops.txt를 입력
<< 입력하면 위와같이 게속 입력만 하게되며 입력했던 값을 넣으면 다시 출력됨..?
<<<를 사용하면 입력값(문자)이 그대로 출력됨

출력 명령어

WC
The wc command can be used to count the number of lines, words and characters in a file:

CAT
cat will print the contents of a file to the screen:

HEAD/TAIL
Sometimes you might not want to view a whole file, but rather just a small portion to have a glimpse at what the file looks like.

GREP
grep stands for "global regular expression print". It allows you to search for a string of characters (called a regular expression) in a specified file, and print all lines containing the search string to the screen. For example, to search for the word "error" in a file:

SED
The most common use for the sed command is for string substitution (find and replace). sed has a very specific command structure as follows:

CUT
The cut command allows you to easily extract particular columns from a file. For example, to extract columns 1 and 3:

PASTE
The paste command allows you to join files horizontally whereby each line of the specified files will be joined by a tab, turning each line into a column. For example, if you had a list of names in a file called names.txt and a list of ages in a file called ages.txt, you could create a single file with the first column being names, and the second column being age like so:

SORT
The sort command is useful for sorting lines or columns within a file. By default, sort will order based on the characters at the start of the line in this order:

AWK
awk is a simple, yet incredibly powerful Linux command for working with column-based data. awk has too much functionality for us to describe in detail here, but there are a range of great online tutorials that cover awk in more depth. Jonathon Palardy's awk tutorial is excellent for beginners!

FIND
Last, but certainly not least, the find command. As the name suggests, this command allows you to search for particular files or directories within a given file hierarchy

반응형
Comments