ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 파일 로깅을 위한 필수 명령어 tee
    2016. 2. 7. comments

    일반적으로 Shell script에서 화면 출력은 echo나 printf등의 명령어를 사용하면 된다.

    #! /bin/bash 
    
    echo 'Hello World'

    이 결과물을 파일로 로그를 남기기 위해서는 매끄럽지 않지만 echo를 두번 남기는 방법도 있다.

    #! /bin/bash
    
    echo 'Hello World' 
    echo 'Hello World' > /var/tmp/tmp.log

    하지만 위와 같이 코드를 작성하면 실수하기 좋고 심지어 코드 중복 작업이다. 물론 실수를 줄이기 위해 함수로 만들어주어도 되겠지만 이러한 상황에 편하게 사용하기 좋은 명령어를 소개하려 한다. 

    tee 명령어

    TEE(1)                    BSD General Commands Manual                   TEE(1)

    NAME

         tee -- pipe fitting

    SYNOPSIS

         tee [-ai] [file ...]

    DESCRIPTION

         The tee utility copies standard input to standard output, making a copy in zero or more files.  The output is unbuffered.

         The following options are available:

         -a      Append the output to the files rather than overwriting them.

         -i      Ignore the SIGINT signal.

         The following operands are available:

         file  A pathname of an output file.

         The tee utility takes the default action for all signals, except in the event of the -i option.

         The tee utility exits 0 on success, and >0 if an error occurs.

    tee 명령어는 stdin으로 전달받은 내용을 stdout으로 내용을 복사하고 파일로 남길 수 있도록 도와준다.

    아래 명령어를 예시로 사용해보자. 현재 시간과 hello world가 문자가 화면에 출력되고 지정한 /var/tmp/tmp.log에 내용들이 함께 저장될 것이다.

    date +"%d-%m-%Y %T: $1" |tee -a /var/tmp/tmp.log
    
    echo 'hello world' | tee -a /var/tmp/tmp.log
    반응형

    댓글

Designed by Tistory.