Ⅰ. 연산자
1. 논리 연산자
| 연산자 | 설명 |
|---|
| A -a B | And |
| A -o B | Or |
| ! (A) | Not |
2. 산술 연산자
| 연산자 | 설명 |
|---|
| a -eq b | a 는 b와 같음 |
| a -ne b | a 는 b와 같지 않음 |
| a -gt b | a 는 b보다 크다 |
| a -ge b | a 는 b보다 크거나 같다 |
| a -lt b | a 는 b보다 작다 |
| a -le b | a 는 b보다 작거나 같다 |
3. 문자열 연산자
| 연산자 | 설명 |
|---|
| A = B | 문자열 A는 문자열 B 와 같음 |
| A == B | 문자열 A는 문자열 B 와 같음 |
| A != B | 문자열 A는 문자열 B 와 같지 않음 |
| -z A | 문자열 A의 길이가 0임 |
| -n A | 문자열 A의 길이가 0이 아님 |
| [[ A -gt B ]] | 문자열간의 대소 비교 |
4. 파일 연산자
| 연산자 | 설명 |
|---|
| -r file | 해당 file이 존재하고 읽기 가능 |
| -w file | 해당 file이 존재하고 쓰기 가능 |
| -x file | 해당 file이 존재하고 실행 가능 |
| -e file | 해당 file이 존재 |
| -d dir | 해당 dir이 존재 |
| -p pipe | 해당 pipe가 존재 |
| -S socket | 해당 socket이 존재 |
| -f file | 해당 file이 존재하고 일반 파일임 |
| -s file | 해당 file이 존재하고 용량이 0이 아님 |
| -h file | 해당 file이 존재하고 symbolic link임 |
| -b file | 해당 file이 존재하고 block device임 |
| -c file | 해당 file이 존재하고 character device임 |
| -t FD | 해당 FD가 존재하고 터미널에 연결됨 |
| file1 -nt file2 | file1 이 file2보다 newer함 |
| file1 -ot file2 | file1 이 file2보다 older함 |
| file1 -ef file2 | file1과 file2의 inode가 같음 |
5. control 연산자
| 연산자 | 설명 |
|---|
| A && B | A가 참(0)일 경우만 B를 실행 |
| A | |
Ⅱ. If 문
- [ ] 앞 뒤로 공백이 있어야 함
- condition은 -a 나 -o 로 결합 가능
1. 방법 1
if [ contition ]
then
command
fi
jsg@jsg-ubuntu:~$ if [ $USER != 'root' ]; then
echo 'change to root'
fi
change to root
2. 방법 2
[ condition ] && command
jsg@jsg-ubuntu:~$ [ $USER != 'root' ] && echo 'change to root'
change to root
3. 방법 3
test condition && command
jsg@jsg-ubuntu:~$ test $USER != 'root' && echo 'change to root'
change to root
4. elif / else
if [ condition ]; then
command
elif [ condition ]; then
command
elif [ condition ]; then
command
...
else
command
fi
Ⅲ. Case 문
case $var in
value1)
command # $var 이 value1 일 경우 실행
;;
value2)
command # $var 이 value2 일 경우 실행
;;
...
*)
command # 나머지 경우일 경우 실행
;;
esac
###test.sh###
#! bin/bash
case $1 in
y*|Y*)
echo Correct
;;
n*|N*)
echo Wrong
;;
*)
echo Not Sure
;;
esac
###terminal###
jsg@jsg-ubuntu:~$ bash test.sh yes
Correct
jsg@jsg-ubuntu:~$ bash test.sh No
Wrong
jsg@jsg-ubuntu:~$ bash test.sh maybe
Not Sure
Ⅳ. While 문
- condition이 true일 동안 loop 실행
1. condition loop
while [ condition ]
do
command
done
###test.sh###
#! bin/bash
idx=5
while [ $idx -gt 0 ]; do
echo $idx
idx=$(expr $idx - 1)
done
###terminal###
jsg@jsg-ubuntu:~$ bash test.sh
5
4
3
2
1
2. 무한 루프
while [ 1 ] / while : / while true
do
command
done
###test.sh###
#! bin/bash
idx=5
while : ; do
echo $idx
idx=$(expr $idx - 1)
if [ $idx -eq 0 ]; then
break
fi
done
###terminal###
jsg@jsg-ubuntu:~$ bash test.sh
5
4
3
2
1
Ⅴ. Until 문
- condition 이 false 일 동안 실행
until [ condition ]
do
commands
done
Ⅵ. Foreach 문
for value in var1 var2 ...
do
command
done
jsg@jsg-ubuntu:~$ cat test.sh
#! bin/bash
for file in *
do
echo $file
done
jsg@jsg-ubuntu:~$ bash test.sh
Desktop
Documents
Downloads
Music
...
Ⅶ. Select 문
PS3=question
select var in value1 value2 ...
do
command
done
jsg@jsg-ubuntu:~$ cat test.sh
#! bin/bash
PS3="Which color do you like? (choose number)>>"
select userinput in "Blue" "Red" "Yellow" "White" "Exit"
do
if [ $userinput = "Exit" ]; then
break
fi
echo "Your favorite color : $REPLY) $userinput"
done
echo "+ End of Question"
jsg@jsg-ubuntu:~$ bash test.sh
1) Blue
2) Red
3) Yellow
4) White
5) Exit
Which color do you like? (choose number)>>3
Your favorite color : 3) Yellow
Which color do you like? (choose number)>>5
+ End of Question