2025-09-16 14:52:51
๋ฐ˜์‘ํ˜•

๐Ÿš 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

 

 


๐ŸŽฏ ์ •๋ฆฌ 

  • ์…ธ ์Šคํฌ๋ฆฝํŠธ = ๋ฐ˜๋ณต๋˜๋Š” ์ผ์„ ์ž๋™์œผ๋กœ ํ•ด์ฃผ๋Š” “์ž‘์€ ํ”„๋กœ๊ทธ๋žจ”
  • ํ˜„์—…์—์„œ ๋งŽ์ด ์“ฐ๋Š” ๊ฒƒ
    • ๋กœ๊ทธ ์ •๋ฆฌ (์„œ๋ฒ„ ํ„ฐ์ง ๋ฐฉ์ง€)
    • ํ”„๋กœ์„ธ์Šค ๋ชจ๋‹ˆํ„ฐ๋ง (์„œ๋น„์Šค ๋‹ค์šด๋˜๋ฉด ์ž๋™ ๋ณต๊ตฌ)
    • ์„œ๋ฒ„ ํ—ฌ์Šค์ฒดํฌ (์šด์˜ํŒ€ ์ ๊ฒ€)
    • ๋Œ€๋Ÿ‰ ํŒŒ์ผ ์ด๋ฆ„/ํ…์ŠคํŠธ ์ฒ˜๋ฆฌ
    • ๋ฐฐํฌ ์ž๋™ํ™”
  • ์ค‘ํ•™์ƒ ์ˆ˜์ค€ ๋น„์œ : ์ง‘์—์„œ ๋งค์ผ ํ•˜๋Š” ์ผ(์ฒญ์†Œ, ์ •๋ฆฌ, ํ™•์ธ)์„ ์‚ฌ๋žŒ์ด ์ง์ ‘ ํ•˜์ง€ ์•Š๊ณ  ์ž๋™ ๋กœ๋ด‡์ด ๋Œ€์‹  ํ•ด์ฃผ๋Š” ๊ฒƒ ๐Ÿš€
๋ฐ˜์‘ํ˜•