Shell - 조건문

dragonappear·2022년 1월 8일
0

Shell

목록 보기
5/7


Test

test expression
[ expression ]

Integer test

File test

String test


Compound Commands

Compound Command는 예약어로 시작되고 그에 상응하는 예약어로 종료된다.


If statements

  • Single-line

  • Multi-line

커맨드라인은 NEWLINE, ; , & 으로 종료된다.

Integer test

#! /bin/bash

if [ $# -lt 2 ]
then
	echo "usage: $0 file1 file2"
    exit 1
fi
echo "program start"
exit 0
$ ./ifex.sh a
usage: ./ifex.sh file1 file2
$ ./ifex.sh a b
program start

String test

#! /bin/bash
echo -n "Enter your password: "
read password
if [ $password ="secret" ]
then
	echo "Welcome!"
else
	echo "Try again!"
fi
$ ./ifex.sh 
Enter your password: secret
"Welcome!"
$ ./ifex.sh 
Enter your password: secre
Try again!

File test

#! /bin/bash
if [ $# -ne 1]
then
	echo "usage: $0 file"
    exit 1
fi

file=$1
if [ -f "$file" ]; then
	echo "$file is a file"
else
	if [ -d "$file" ]; then
    	echo "$file is a directory"
    fi
fi
exit 0
$ ls
a b

$ ./ifex.sh
usage: ./ifex.sh file

$ ./ifex.sh a
a is a file

$ ./ifex.sh b
b is a directory

$ ./ifex.sh c

cat ~/.profile


Case statements

Case pattern : *

Case pattern : |

0개의 댓글