Shell - 명령어 & 리다이렉션

dragonappear·2022년 1월 8일
0

Shell

목록 보기
3/7

Linux Command

set, shopt

set,shopt은 shell 옵션을 설정하는 빌트인 커맨드들이다.

  • set -o/+o : on/off
  • shopt -s/-u : on/off

compgen

compgen은 리눅스에서 실행되는 모든 커맨드들을 나열하는 bash 커맨드이다.

  • $ compgen -c: 바로 실행될수있는 모든 커맨드 나열
  • $ compgen -a: bash alias 모두 나열
  • $ compgen -k: bash keywords 모두 나열
  • $ compgen -A function: bash function 모두 나열
  • $ compgen -b: bash built-ins 모두 나열
  • 나머지는 모두 external command

Order of Executing Commands

bash alias -> Keywords -> Functions -> built-ins -> finally a search in $PATH 순으로 진행된다.

type: command의 종류 출력


Built-in vs External Commands

Built-in Command

  • 쉘 프로그램에 내장되어있는 명령어이다.
  • bash는 fork/exec 하지않고 바로 실행한다.
  • cd,pwd,echo 등등

External Command

  • 쉘 프로그램에 내장되어있지 않은 명령어이다.
  • PATH에 있는 외부 디렉토리를 통해 bash가 명령어를 찾는다.
  • (ex:/user/bin:/bin) :을 통해 디렉토리가 분리되어있다.
  • 명령어 검색은 왼족에서 오른쪽으로 순차적으로 일어난다.

Command Type


Command List

  • 하나 혹은 그 이상의 jobs의 연속은 ;,&로 분리된다.
  • ; -> 순차적으로 명령어/파이프라인을 실행한다.
    • $ date ; sleep1 ; sleep1 ; sleep1 ; date
  • & -> 비동기적으로 실행한다.
    • $ date & sleep1 & sleep1 & sleep1 & date
  • $ date ; sleep1 & sleep1 & sleep1 ; date <=>
    date
    sleep 1 &
    sleep 1 &
    sleep 1
    date

Exit Status

  • 0: 정상적으로 종료되었을때 Process가 반환하는 값

  • !0: 비정상적으로 종료되었을때 Process가 반환하는 값

  • $? : exit status 값이 저장되어있다.

  • exit status은 Bash Conditional commands에 사용된다.

Conditional command List

&&,ll

  • cmd1 && cmd2 : cmd1이 성공해야 cmd2를 실행한다.
  • cmd1 || cmd2 : cmd1이 실패해야 cmd2를 실행한다.
cd ~/tempdir
rm -rf  # 위험한 명령어이므로 아래와 같이 사용하자.
cd ~/tempdir && rm -rf *

Command Group

  • (command1; command2; command3;...)
    • ()은 child/sub shell에서 실행된다.
    • 부모 쉘은 ()를 보면 자식 쉘을 생성한다.
    • 자식 쉘은 커맨드 그룹을 분리한다.
  • {command1; command2; command3;...}
    • 부모쉘에서 커맨드 그룹을 분리한다.


Redirection

Standard Input/Output

  • File Descriptor
    • 파일을 나타내는 정수
    • 각각의 프로세스는 fd를 index로 사용하여 파일을 열고, 자신만의 fd table을 보유한다.
    • 모든 프로세스는 3가지 표준 POSIX file descriptors를 가진다.
      • 0: Stadard Input (keyboard)
      • 1: Stadard Output (screen)
      • 2: Stadard Error (screen)

Standard Input Redirection

0< : 키보드를 통한 standard input 대신에 특정 파일을 통한 읽기

Standard Output Redirection

1> : standard output을 file로 redirect

  • 기존 파일의 내용은 사라진다.
  • 추가하려면 1> 대신에 1>>

Standard Error Redirection

2> : standard error을 file로 redirect

Combination of Input and Output Redirections

More on redirection

output과 error을 같은 파일에 redirect

출력을 버리고 싶을때는 redirect to /dev/null

  • `/dev/null : special virtula file
  • `ls a b > /dev/null 2>&1

0개의 댓글