๋ฐ์ํ
๐ 9. ์ ธ ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋๋ฐ
1๏ธโฃ ์คํฌ๋ฆฝํธ ์์ฑ & ์คํ ๋ฐฉ๋ฒ
- ์คํฌ๋ฆฝํธ ์์์ ํญ์ #!/bin/bash
- ์คํ ๋ฐฉ๋ฒ
sh script.sh # sh๋ก ์คํ
chmod +x script.sh && ./script.sh # ์คํ ๊ถํ ์ฃผ๊ณ ์ง์ ์คํ
flowchart LR A[์ฌ์ฉ์ ์
๋ ฅ] --> B[Shell Script] B --> C[๋ฆฌ๋
์ค ์ปค๋ ์คํ] C --> D[๊ฒฐ๊ณผ ์ถ๋ ฅ]
2๏ธโฃ ์์ฃผ ์ฌ์ฉํ๋ ์ ธ ์คํฌ๋ฆฝํธ
๐ ๋ก๊ทธ ๋ฐฑ์ & ์ ๋ฆฌ ์คํฌ๋ฆฝํธ
#!/bin/bash
# log_backup.sh
LOG_DIR="/var/log/myapp"# ๋ก๊ทธ๊ฐ ์๋ ํด๋
BACKUP_DIR="/var/backups/myapp"# ๋ฐฑ์ ์ ์ฅ ํด๋
DATE=$(date +%Y%m%d) # ์ค๋ ๋ ์ง (์: 20250916)
mkdir -p "$BACKUP_DIR" # ๋ฐฑ์ ํด๋ ์์ผ๋ฉด ์์ฑ
# 7์ผ ์ง๋ .log ํ์ผ ์ฐพ์์ ๋ฐฑ์ ํ๊ณ ๋ด์ฉ ๋น์ find "$LOG_DIR" -type f -name "*.log" -mtime +7 | whileread file; do tar -czf "$BACKUP_DIR/$(basename $file).$DATE.tar.gz" "$file" # ์์ถ ๋ฐฑ์
> "$file" # ์๋ณธ ๋ก๊ทธ๋ ์ง์ฐ์ง ์๊ณ ๋ด์ฉ๋ง ์ญ์ done
๐ ํ์ ์์ ๋งค์ฐ ๋ง์ด ์ (๋ก๊ทธ ์ฉ๋์ด ์ฐจ์ ์๋ฒ๊ฐ ์ฃฝ๋ ๊ฑธ ๋ฐฉ์ง)
๐ ํ๋ก์ธ์ค ๋ชจ๋ํฐ๋ง & ์ฌ์์
#!/bin/bash
# monitor.sh
PROCESS="myapp" # ๊ฐ์ํ ํ๋ก์ธ์ค ์ด๋ฆ
if ! pgrep -x "$PROCESS" > /dev/null; then # ์คํ ์ค์ธ์ง ํ์ธ
echo "$(date): $PROCESS not running. Restarting..." >> /var/log/monitor.log
systemctl start "$PROCESS" # ์ ๋์๊ฐ๋ฉด ์ฌ์์
fi
๐ ์ด์ํ ํ์ (์๋น์ค๊ฐ ์ฃฝ์์ ๋ ์๋ ๋ณต๊ตฌ)
๐ ์๋ฒ ํฌ์ค์ฒดํฌ
#!/bin/bash
# healthcheck.sh
echo "===== $(hostname) =====" # ์๋ฒ ์ด๋ฆ
echo "Uptime:" # ์๋ฒ ์ผ์ง ์๊ฐ
uptime
echo
echo "Disk usage:" # ๋์คํฌ ์ฌ์ฉ๋
df -h | grep -v tmpfs
echo
echo "Top 5 memory-consuming processes:" # ๋ฉ๋ชจ๋ฆฌ ๋ง์ด ์ฐ๋ ์์ 5๊ฐ
ps aux --sort=-%mem | head -n 6
๐ ์ด์ํ ๊ธฐ๋ณธ (์๋ฒ ์ํ๋ฅผ ๋น ๋ฅด๊ฒ ํ์ธํ ๋)
๐ ๋๋ ํ์ผ ์ด๋ฆ ๋ณ๊ฒฝ
#!/bin/bash# rename.shfor file in *.txt; do# ๋ชจ๋ .txt ํ์ผ์ ๋ํดmv"$file" "${file%.txt}.bak" # ํ์ฅ์ .txt → .bak ๋ก ๋ณ๊ฒฝdone
๐ ํ์ผ ๋๋ ์ฒ๋ฆฌํ ๋ ๊ฐ๋ฐ์๋ค์ด ์์ฃผ ํ์ฉ
๐ ๋ฐฐํฌ ์๋ํ (๋จ์ ๋ฒ์ )
#!/bin/bash
# deploy.sh
APP_DIR=
"/opt/myapp"
# ์ฑ ์ฝ๋ ๊ฒฝ๋ก
BACKUP_DIR=
"/opt/backup"
# ๋ฐฑ์ ์ ์ฅ ๊ฒฝ๋ก
DATE=$(
date
+%Y%m%d%H%M)
# ํ์ฌ ์๊ฐ
tar -czf
"$BACKUP_DIR
/myapp_
$DATE
.tar.gz"
"$APP_DIR
"
# ํ์ฌ ๋ฒ์ ๋ฐฑ์
git -C
"$APP_DIR
" pull origin main
# ์ต์ ์ฝ๋ ๋ฐ๊ธฐ
systemctl restart myapp.service
# ์๋น์ค ์ฌ์์
๐ ์คํํธ์ /์ค์๊ธฐ์ ์์ ์ง์ ์๋ฒ ์ด์ ์ ์์ฃผ ์ฌ์ฉ
3๏ธโฃ ์ค์ต ์์ : .txt → .bak ๋ณํํ๊ธฐ
โ ์ฐ์ต ๋๋ ํ ๋ฆฌ ๋ง๋ค๊ธฐ
mkdir ~/rename-test # ํ
์คํธ์ฉ ํด๋ ๋ง๋ค๊ธฐ cd ~/rename-test
โก ์ํ ํ์ผ ์์ฑ
touch file1.txt file2.txt file3.txt # ๋น ํ
์คํธ ํ์ผ 3๊ฐ ์์ฑ ls
โข ์คํฌ๋ฆฝํธ ์์ฑ
vi rename.sh
๋ด์ฉ:
#!/bin/bash
for
file
in
*.txt;
do
# ๋ชจ๋ .txt ํ์ผ์ ๋ํด
mv
"$file
"
"${file%.txt}
.bak"
# ํ์ฅ์๋ฅผ .bak์ผ๋ก ๋ณ๊ฒฝ
done
โฃ ์คํ ๊ถํ ๋ถ์ฌ
chmod +x rename.sh
โค ์คํ
./rename.sh ls
๐ ๊ฒฐ๊ณผ: file1.bak file2.bak file3.bak
โ Quiz : ๋๋๋ฆฌ๊ธฐ
.bak → .txt ๋ก ๋ค์ ๋ณต์ํ๋ ค๋ฉด?
#!/bin/bashfor
file in *.bak;
do# ๋ชจ๋ .bak ํ์ผ์ ๋ํด
mv"$file" "${file%.bak}.txt" # ํ์ฅ์๋ฅผ .txt๋ก ๋ณ๊ฒฝ
done
โก ๊ธฐ๋ณธif๋ฌธ
if1.sh
if [ ์กฐ๊ฑด]
then
์ฐธ์ผ๊ฒฝ์ฐ์คํ
fi
#!/bin/bash
if [ "KIM" = "KIM" ]
then
echo "It's true."
fi
exit 0
โก if-else๋ฌธ
if2.sh
if [ ์กฐ๊ฑด]
then
์ฐธ์ผ๊ฒฝ์ฐ์คํ
else
๊ฑฐ์ง์ผ๊ฒฝ์ฐ์คํ
fi
#!/bin/bash
if [ "KIM" = "LEE" ]
then
echo "It's true."
else
echo "It's false."
fi
exit 0
โก ์กฐ๊ฑด๋ฌธ์๋ค์ด๊ฐ๋๋น๊ต์ฐ์ฐ์
if3.sh
#!/bin/bash
if [ 100 -eq 200 ]
then
echo "100 and 200 are the same."
else
echo "100 and 200 are different"
fi
exit 0
โก ํ์ผ๊ณผ ๊ด๋ จ๋ ์กฐ๊ฑด
if4.sh
#!/bin/bash
fname=/lib/systemd/system/httpd.service
if [ -f $fname]
then
head -5 $fname
else
echo "Web server is not installed."
fi
exit 0
โก case~esac๋ฌธ
case1.sh
#!/bin/bash
case "$1" in
start)
echo "Start~";;
stop)
echo "Stop~";;
restart)
echo "Restart~";;
*)
echo "I don't know what it is~~";;
esac
exit 0
case2.sh
#!/bin/bash
echo "Is Linux fun? (yes / no)"
read answer
case $answer in yes |y |Y | Yes |YES)
echo "That's good."
echo "Cheer up~ ^^";;
[nN]*)
echo "That's too bad.. ใ ใ ";;
*)
echo "You should have just typed YES or NO" exit 1;;
esac
exit 0
โก AND, OR ๊ด๊ณ์ฐ์ฐ์
andor.sh
#!/bin/bash
echo "Please enter the file name you want to view."
read fname
if [ -f $fname] && [ -s $fname] ;
then
head -5 $fname
else
echo "The file does not exist or its size is 0."
fi
exit 0
โก for~in๋ฌธ
forin.sh
#!/bin/bash
hap=0
for i in 1 2 3 4 5 6 7 8 9 10
do
hap=`expr $hap + $i`
done
echo "The sum of 1 to 10 is " $hap
exit 0
forin2.sh
#!/bin/bash
for fnamein $(ls *.sh)
do
echo "---$fname---"
head -3 $fname
done
exit 0
โก while๋ฌธ
while.sh
#!/bin/bash
while true
do
echo "Ubuntu"
done
exit 0
#!/bin/bash
while :
do
echo "Ubuntu"
done
exit 0
while2.sh
` -> ํ ํฑ๋ณด๋ค๋ ์์ฆ $๋ก ๋ง์ด ์ฌ์ฉ
#!/bin/bash
hap=0 i=1
while [ $i-le 10 ]
do
hap=`expr $hap + $i`
i=`expr $i+ 1`
done
echo "The sum of 1 to 10 is: $hap"
exit 0
#!/bin/bash
hap=0 i=1
while [ $i-le 10 ]
do
hap=$((hap + i))
i=$((i+ 1))
done
echo "The sum of 1 to 10 is: $hap"
exit 0
while3.sh
#!/bin/bash
echo "Please enter your password."
read mypass
while [ "$mypass" != "1234" ]
do
echo "Incorrect. Please re-enter."
read mypass
done
echo "Success~"
exit 0
โก until๋ฌธ
์กฐ๊ฑด์์ด์ฐธ์ผ๋๊น์ง(=๊ฑฐ์ง์ธ๋์) ๊ณ์๋ฐ๋ณต
until.sh
#!/bin/bash
hap=0
i=1
while [ $i-le 10 ]
do
hap=`expr $hap + $i`
i=`expr $i + 1`
done
echo "The sum of 1 to 10 is: $hap"
exit 0
#!/bin/bash
hap=0
i=1
until [ $i-gt 10 ]
do
hap=`expr $hap + $i`
i=`expr $i + 1`
done
echo "The sum of 1 to 10 is: $hap"
exit 0
โก break, continue, exit, return
bce.sh
#!/bin/bash
echo "Starts infinite loop input. (b : break, c : continue, e : exit)"
while true; do
read input
case $input in
b | B)
break
;;
c | C)
echo "If you press continue, it will return to the while condition" continue
;;
e | E)
echo "If you press exit, the program (function) will be completely terminated."
exit 1
;;
*)
echo "Unknown input. Please press b, c, or e."
;;
esac
done
echo "If you press break, it will exit while and print this sentence."
exit 0
๐ฏ ์ ๋ฆฌ
- ์ ธ ์คํฌ๋ฆฝํธ = ๋ฐ๋ณต๋๋ ์ผ์ ์๋์ผ๋ก ํด์ฃผ๋ “์์ ํ๋ก๊ทธ๋จ”
- ํ์
์์ ๋ง์ด ์ฐ๋ ๊ฒ
- ๋ก๊ทธ ์ ๋ฆฌ (์๋ฒ ํฐ์ง ๋ฐฉ์ง)
- ํ๋ก์ธ์ค ๋ชจ๋ํฐ๋ง (์๋น์ค ๋ค์ด๋๋ฉด ์๋ ๋ณต๊ตฌ)
- ์๋ฒ ํฌ์ค์ฒดํฌ (์ด์ํ ์ ๊ฒ)
- ๋๋ ํ์ผ ์ด๋ฆ/ํ ์คํธ ์ฒ๋ฆฌ
- ๋ฐฐํฌ ์๋ํ
- ์คํ์ ์์ค ๋น์ : ์ง์์ ๋งค์ผ ํ๋ ์ผ(์ฒญ์, ์ ๋ฆฌ, ํ์ธ)์ ์ฌ๋์ด ์ง์ ํ์ง ์๊ณ ์๋ ๋ก๋ด์ด ๋์ ํด์ฃผ๋ ๊ฒ ๐
๋ฐ์ํ
'Cloud Engineering Bootcamp > 5. Linux' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
10. ๋ฆฌ๋ ์ค ์ข ํฉ ์ค์ต (0) | 2025.09.17 |
---|---|
08. ๋ฆฌ๋ ์ค ์ ์ฌ์ฉํ๊ธฐ (0) | 2025.09.16 |
07. ๋ฆฌ๋ ์ค ์ฌ์ฉ์ ๊ด๋ฆฌ (0) | 2025.09.15 |
06. ํ๋ก์ธ์ค ๊ด๋ฆฌํ๊ธฐ (0) | 2025.09.15 |
05. ํ์ผ ์ ๊ทผ๊ถํ ๊ด๋ฆฌํ๊ธฐ (0) | 2025.09.15 |