본문 바로가기
os/Linux

8. Shell Script - 조건문, 반복문

by #moonyz 2014. 10. 28.
** 참고
   and : 곱하기 / -a : 그리고
   or : 더하기 / -o : 또는


1. 조건문
if : 조건이 참, 거짓일때
case : 조건이 여러 가지 일때
#!/bin/bash
echo -n "Enter Fruits"
read fname

if [ $fname = "apple" ] ; then
   echo "I like apple!"
elif [ $fname = "orange" ] ; then
   echo "I like orange!"
else
   if [ $fname = "onion" ] ; then
      echo "This is not Fruits!"
   fi
fi
#!/bin/bash
echo -n "[one]Web [two]Was [three]DB"
echo -n "Enter your choice : "
read choice

case $choice in
     one)
       sh /shell/web_backup.sh ;;
     two)
       sh /shell/was_backup.sh ;;
     three)
       sh /shell/db_backup.sh ;;
     *)
       echo "Enter [ one, two, three ] "
esac
 
2. 반복문
for : 반복횟수를 알고있을 때
while : 반복횟수를 알수 없을때
until : 조건이 참일때까지 반복
#!/bin/bash
echo " "

for i in 2 3 4 5 6 7 8 9
do
    for j in 1 2 3 4 5 6 7 8 9
      do
        gop=`expr $i  $j`
        echo "$i X $j = $gop"
      done

echo " "
done

 
#!/bin/bash
echo " "

dan=2
num=1

while [ $dan -le 9 ]
do
num=1
   while [ $num -le 9 ]
    do
      gop=`expr $dan  $num`
      echo " $dan X $num = $gop"

      num=`expr $num + 1`
    done

   dan=`expr $dan + 1`
   echo " "
done
#!/bin/bash
echo " "
 echo -n "Enter Number : "
 read dan
echo " "

num=1
until [ $num -eq 10 ]
do
  gop=`expr $dan  $num`
  echo " $dan X $num = $gop"

  num=`expr $num + 1`
done
  


'os > Linux' 카테고리의 다른 글

[실습] 1. 디스크추가  (0) 2014.10.28
9. 네트워크 (서브넷 정리 필요)  (0) 2014.10.28
7. Shell Script  (0) 2014.10.28
6. RPM / CRON  (0) 2014.10.28
5. 프로세스관리  (0) 2014.10.28

댓글