- 산술연산
- expr
- /usr/bin/expr command로 지원. bash shell 에 종속되지 않음.
- 정수형 산술연산(+,-,*,/,%), 논리연산( |,& ), 관계연산(=,!=,>,>=,<,<=)
- expr 10 + 5
- expr 10 - 5
- expr 5 '*' 2
- expr 25 '/' 5
- x=5
- expr $x > 4
- expr $x > 8
- sum = `expr $x + 10`
- type expr
- let
- bash shell 안에 포함되어 있음.
- 정수형 산술연산, bit 연산 ( <<,>>, &, |), 논리연산 (&&, ||), 단항연산 (++,+=,-=)
- let sum=x+5
- let x++
- let x+=1
- type let
- ((sum=x+5)
- ((x++))
- ((x-=1))
- expr
- 조건부 loop
- while
- while 다음의 command가 성공하는 동안 do~done 사이의 명령어를 반복 실행
- while 다음의 command가 성공하는 동안 do~done 사이의 명령어를 반복 실행
- while 조건명령어
do
반복명령어
done - Ex)
- num 변수에 1을 넣고 5가 될때까지 반복 실행
num=1 while test $num -le 5 do echo Number: $num ((num++)) done
- while
- getent passwd <유저명> : /etc/passwd 파일에서 유저를 검색한다.
- Ex)
- 계정 생성 프로그램(while 사용)
- username을 입력 받고 해당 계정이 있을때는 "계정이 존재한다" 표시
- 계정이 new user인 경우 생성해준다.
- 계정 생성 프로그램(while 사용)
#!/bin/bash
#Description: Create a user account.
echo -n "New username:"
read username
while getent passwd $username &> /dev/null
do
echo "Sorry, that account $username is already taken. Please pick a differnet username."
echo -n "New username:"
read username
done
sudo useradd -m -s /bin/bash $username
- read 를 통해 username을 입력받는다
- getent를 통해 입력받은 계정명 확인 후 계정이 존재 시 Sorry 부분 출력 후 username을 다시 입력받는다.
- 계정이 존재하지 않으면 done 으로 while 문을 빠져나와 useradd 명령어 진행된다.
- until
- until 다음의 command가 실패할 때 까지 do~done 사이의 명령어를 반복 실행
- until 조건명령어
do
반복명령어
done
- until 조건명령어
- until 다음의 command가 실패할 때 까지 do~done 사이의 명령어를 반복 실행
num=1
until test $num -gt 5
do
echo Number: $num
((num++))
done
- Ex)
- 계정 삭제 프로그램(until 사용)
- username을 입력 받고 해당 계정이 없을 시 "username does not exist." 표시 계정이 있을 경우 삭제한다.
- 계정 삭제 프로그램(until 사용)
#!/bin/bash
echo -n "username to remove:"
read username
until getent passwd $username &> /dev/null
do
echo "username does not exist."
echo -n "username to remove: "
read username
done
sudo userdel -r $username
- while loop과 if 문을 함께 사용한 예제
#!/bin/bash
num=0
while [ $num -lt 5 ]
do
echo "Number: $num"
((num++))
if [[ "$num" == '2' ]]; then
break
fi
done
- for-Loop
- 주어진 list 만큼 do~done 사이의 명령어를 반복 실행
- for item in [LIST]
do
[commands]
done
- for item in [LIST]
- 주어진 list 만큼 do~done 사이의 명령어를 반복 실행
- 1번 부터 5번까지 반복 출력
for NUM in 1 2 3 4 5
do
echo $NUM
done
- 1번 부터 5번까지 반복 출력
- ( ) 형식으로도 사용 가능.
for NUM in $(seq 5)
do
echo $NUM
done
- 현재 디렉토리 경로 내 각 파일을 나열한다.
for file in *
do
ls $file
done
- Ex) 현재 디렉토리의 파일을 백업 디렉토리로 복사
if [ ! -d ~/backup ]
then
mkdir ~/backup
fi
for FILE in *
do
cp $FILE /backup/$FILE.old
done
- home 디렉토리에 /backup 폴더가 없다면 만든다
- 현재 디렉토리 내부 파일 전체를 선택하여 카피 후 backup 폴더에 파일명.old 로 복사한다.
- Ex)
- 작업 디렉토리를 입력 받아 해당 디렉토리에 파일의 수와 디렉토리 수를 출력하는 프로그램을 작성하시오.
#!/bin/bash
# Description: Count files and dirs
echo -n "Input a directory name: "
read direc
until [ -d "$direc" ]
do
echo "It is not a directory"
echo -n "Input a directory name: "
read direc
done
echo "-------------------"
echo "$direc:"
echo "files : $(ls -l "$direc" | grep ^- | wc -l)"
echo "dirs : $(ls -l "$direc" | grep ^d | wc -l)"
echo "------------------"
- read 로 디렉토리명을 입력 받은 후 until로 디렉토리명이 틀리면 다시 디렉토리명을 입력 받을수 있게 한다.
정확한 디렉토리명 입력시 아래 명령문을 통해 파일,디렉토리 수를 출력한다
'리눅스 > Bash Script' 카테고리의 다른 글
[Bash Script] Bash Script 프로젝트 : CloudUploader(AWS CLI, S3) 설정 (0) | 2023.11.12 |
---|---|
[Bash Script] Bash Script 프로젝트 : CloudUploader(AWS CLI, S3) (0) | 2023.11.12 |
[Bash Script] Branching(exit, test, if-then-fi, case) (0) | 2023.11.08 |
[Bash Script] Input & output(echo, read) (0) | 2023.11.07 |
[Bash Script] Bash shell의 Rule (1) | 2023.11.07 |