Shell Script

Test script

1. 개요

조건문, 반복문, 함수, 변수 사용, echo 옵션, 입력 등 기본적인 쉘 스크립트 작성방법을 알 수 있는 test script이다.

 

2. Source code

#!/bin/sh
 
mul_func() {
    cnt=1
    while [ $cnt -le 9 ]
    do
        echo "$multiple * $cnt = `expr $multiple \* $cnt`"
        cnt=`expr $cnt + 1`
    done
}
 
big_func() {
    bignum=0
    if [ $input2 -ge $input1 ] ; then
        if [ $input3 -ge $input2 ]
            then bignum=$input3
        else
            bignum=$input2
        fi
    else
        if [ $input3 -ge $input1 ]
            then bignum=$input3
        else
            bignum=$input1
        fi
    fi
    echo "BIG Number   : $bignum"
}
 
small_func() {
    smallnum=0
    if [ $input2 -le $input1 ] ; then
        if [ $input3 -le $input2 ]
            then smallnum=$input3
        else
            smallnum=$input2
        fi
    else
        if [ $input3 -le $input1 ]
            then smallnum=$input3
        else
            smallnum=$input1
        fi
    fi
    echo "small Number : $smallnum"
}
 
fibo_func() {
    num1=1
    num2=1
    temp=0
    
    echo -n "$num1\t$num2\t"
    while [ $num2 -le $fibonacci ]
    do
        temp=$num2
        num2=`expr $num1 + $num2`
        num1=$temp
    
        if [ $num2 -ge $fibonacci ]
            then break
        fi
    
        echo -n "$num2\t"
    done
    echo
}
 
display_func() {
    topcnt=1
    while [ $topcnt -le $input ]
    do
        cnt=`expr $input - $topcnt`
        while [ $cnt -ge 1 ]
        do
            echo -n " "
            cnt=`expr $cnt - 1`
        done
 
        cnt=1
        while [ $cnt -le $topcnt ]
        do
            echo -n "*"
            cnt=`expr $cnt + 1`
        done
 
        cnt=1
        while [ $cnt -le $topcnt ]
        do
            echo -n "*"
            cnt=`expr $cnt + 1`
        done
 
        echo 
        topcnt=`expr $topcnt + 1 `
    done
}
 
menu_print() {
    clear
    
    echo "--------------------------------------------------------------"
    echo "|  Test Script                                               |"
    echo "|  1. Login user                                             |"
    echo "|  2. The rules of multiplication                            |"
    echo "|  3. Fibonacci Sequence                                     |"
    echo "|  4. If input 3 number then result Big, small number        |"
    echo "|  5. Display Image                                          |"
    echo "--------------------------------------------------------------"
    echo
    echo -n "Choose(q:quit) : "
    read menu
    echo
}
 
while true
do
    menu_print
 
    case $menu in
        1)
            echo -n "1. Login user : "
            echo `who | wc -l`"명"
            echo "`who`"
            ;;
        2)
            echo "2. The rules of multiplication"
            echo -n "INPUT : "
            read multiple
 
            echo 
            mul_func
            ;;
        3)
            echo "3. Fibonacci Sequence"
            echo -n "INPUT : "
            read fibonacci
            
            echo
            fibo_func
            ;;
        4)
            echo "4. If input 3 number then result Big, small number"
            echo -n "INPUT 1 : "
            read input1
            echo -n "INPUT 2 : "
            read input2
            echo -n "INPUT 3 : "
            read input3
 
            echo
            big_func
            small_func
            ;;
        5)
            echo "5. Display Image"
            echo -n "INPUT : "
            read input
            
            echo
            display_func
            ;;
        q)
            break
            ;;
        *)
            echo "Input Error!"
    esac
 
    sleep 3
done
Back To Top