shell script

Younghwan Cha·2022년 12월 26일
0

linux

목록 보기
1/24
post-thumbnail

shell script


linux shell 은 명령어와 프로그램을 실행할 때 사용하는 인터페이스이다.
shell 은 kernel 과 사용자를 연결하는 데, 사용자로 부터 명령을 받아 그것을 해석하고 프로그램을 실행하는 역할을 한다.

shell 의 종류


shebang ( #!/bin/bash )


shell script 를 작성할 경우에, script 시작부분에서 다음과 같은 문구를 자주 보았을 것이다.

#!/bin/bash

...

여기서, #!shebang 또는 hashbang 이라고 부른다.
shebang 은 서로 다른 종류의 shell 을 다루는 shell scripting 에서 중요한 역할을 수행한다.
#! 은 2 Byte 의 매직 넘버 (Magic Number) 로 스크립트의 맨 앞에서 이 파일이 어떤 명령어 해석기의 명령어 집합인지를 시스템에 알려주는 역할을 합

[what is shebang?] https://linuxhandbook.com/shebang/

특수 매개 변수

special Parameters

$$: 현재 스크립트의 PID
$?: 최근에 실행된 명령어, 함수, 스크립트 자식의 종료 상태
$!: 최근에 실행한 백그라운드 명령의 PID
$-: 현재 옵션 플래그
$_: 지난 명령의 마지막 인자로 설정된 특수 변수

background 실행

command 마지막에 & 를 붙일 경우 background 에서 실행시킨다.

If a command is terminated by the control operator &, 
the shell executes the command in the background in a subshell. 
The shell does not wait for the command to finish, and the return status is 0.

https://stackoverflow.com/questions/13338870/what-does-at-the-end-of-a-linux-command-mean

case

case 는 다음과 같은 형식을 따른다

case $OPTION in
	case1)
    	command;;
    case2)
    	command;;
    *)
    	command;;
esac

또한, option 에서 wild card 를 통해 다양한 pattern matching 을 사용 할 수 있다.

case $OPTION in
	# * 는 0개 이상, + 는 1개 이상
	a*       ) a, aa, aab
    a?       ) ab, ac, ad
    a[bc]    ) ab | ac
    a+(1|2)  ) a1 | a2
    *.jpg|*.png) jpg file | png file
    !(*.jpg|*.png)) jpg file | png file
esac

$* vs $@

"$*"	All the positional parameters (as a single word) *
"$@"	All the positional parameters (as separate strings)

는입력되는모든parameter를한개의단어로취급하고*는 입력되는 모든 parameter를 한개의 단어로 취급하고@는 공백으로 구분된 별도의 문자열로 취급한다는 의미다

$ ./prog aa bb cc

=================
$* section
aa bb cc
=================
$@ section
aa
bb
cc
=================

[$* vs $@] https://jybaek.tistory.com/477

read


case $OPTION in
	case1)
    	read -p '[y/n]' $REPLY
        echo $REPLY
    *)
    	command;;
esac

Read a line from the standard input and split it into fields.

-p: stdout 으로 값을 입력 받기 전에 줄 바꿈없이 PROMT 문자열을 출력합니다

if 조건문

비교식

[ -z ${A} ] : A 문자열의 길이가 0이면 TRUE
[ -n ${A} ] : A 문자열의 길이가 0이 아니면 TRUE
[ ${A} -eq ${B} ] : A와 B값이 같으면 TRUE
[ ${A} -ne ${B} ] : A와 B값이 다르면 TRUE
[ ${A} -gt ${B} ] : A가 B보다 크면 TRUE
[ ${A} -ge ${B} ] : A가 B보다 크거나 같으면 TRUE
[ ${A} -lt ${B} ] : A가 B보다 작으면 TRUE
[ ${A} -le ${B} ] : A가 B보다 작거나 같으면 TRUE

[ 조건식A -a 조건식B ] : 조건식 A와 B가 모두 TRUE이면 TRUE (&& 와 동일)
[ 조건식A -o 조건식B ] : 조건식 A가 TRUE거나 조건식B가 TRUE면 TRUE (|| 와 동일)

파일관련

[ -d ${A} ] : A 파일이 디렉토리면 TRUE
[ -e ${A} ] : A 파일이(노드, 디렉토리, 소켓 등등 모두) 존재하면 TRUE
[ -L ${A} ] : A 파일이 심볼릭 링크면 TRUE
[ -r ${A} ] : A 파일이 읽기 가능하면 TRUE
[ -s ${A} ] : A 파일의 크기가 0 보다 크면 TRUE
[ -w ${A} ] : A 파일이 쓰기 가능하면 TRUE
[ -x ${A} ] : A 파일이 실행 가능하면 TRUE
[ -c ${A} ] : A 파일이 Special character file 이면 TRUE
[ -f ${A} ] : A 파일이 디렉토리가 아닌 일반 regular 파일이면 TRUE
[ -S ${A} ] : A 파일이 소켓이면 TRUE
[ ${A} -nt ${B} ] : A 파일 B 파일보다 최신파일이면 참
[ ${A} -ot ${B} ]  : A 파일이 B 파일보다 이전파일이면 참
[ ${A} -ef ${B} ] : A 파일과 B 파일이 같은 파일이면 참

shell 정보

먼저, 설치되어 있는 shell 들의 정보를 보도록 하자.
/etc/shell 경로에 shell 의 정보가 담겨 있다.

cat /etc/shell

> /bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/usr/bin/sh
/bin/dash
/usr/bin/dash

[ref]
https://hand-over.tistory.com/32

[아래 블로그에 bash 기본에 관련된 내용이 잘 정리되어 있으니 참고하자]
https://blog.gaerae.com/2015/01/bash-hello-world.html

profile
개발 기록

0개의 댓글