2010年9月13日 星期一

Shell script 練習(while do...done,for do...done)

<1>mywhile.sh
#由0開始+1到值變為10後停止
#!/bin/bash
myvar=0
while [ $myvar -ne 10 ]
do
        echo $myvar
        myvar=$(( $myvar + 1 ))
done
exit 0

執行方式:
$./mywhile.sh

修正版:
a.
#修正為手動輸入參數,判斷是否為數字0-9
#~/bin/bash
myvar="$1"
for (( i=0; i<=9; i=i+1 ))
do
        if [ "$i" == "$myvar" ]; then
                while [ $myvar -ne 10 ]
                do
                        echo $myvar
                        myvar=$(( $myvar + 1 ))
                done
        fi
done
echo "Please input num 0-9"
exit 0

執行方式:
1.$./mywhile.sh 12515    出現錯誤訊息
2.$./mywhile.sh abc    出現錯誤訊息
3.$./mywhile.sh 5     正確執行,在畫面上秀出5,6,7,8,9

b.
#!/bin/bash
[ "$#" != "1" ] && echo "parameter too much" && exit 1
myvar="$1"

s=$(echo $1 |tr -d 0-9)
[ -n "$s" ] && echo "$myvar not number" && exit 1

if [ $myvar -gt "9" -o $myvar -lt "0" ]; then
        echo "Please input 0-9"
        exit 0
else
        while [ $myvar -ne 10 ]
        do
                echo $myvar
                myvar=$(( $myvar + 1 ))
        done
fi

exit 0

<2>myfor1.sh
#秀出所給的所有參數
#!/bin/bash
for thing in "$@"                   #thing 表示變數
do
        echo you typed $thing
done
exit 0

執行方式:
1../myfor1.sh aaa bbb ccc  畫面依序秀出you typed aaa,you typed bbb,you typed ccc

進階練習:
a. 
#將後面給的參數IP,交由ping來執行,然後在螢幕上秀出有無成功
#!/bin/bash

for ip in "$@"
do
        ping -c 2  $ip &> /dev/null
        if [ "$?" != "0" ]; then
                echo ping $ip fail
        else
                echo ping $ip ok
        fi
done
exit 0

執行方式:
1.$./myfor1.sh 192.168.120.1 168.95.1.1 192.168.1.1
正確執行,依序秀出ping 192.168.120.1 ok,ping 168.95.1.1 ok,ping 192.168.1.1 fail
b.
#在畫面上秀出類似99乘法表
#!/bin/bash
#writer:Marcus Tsai 20100913
read -p "請輸入第一個數字:" fn
s=$(echo $fn |tr -d 0-9)
[ -n "$s" ] && echo "Error: $fn 不是數字" && exit 1
read -p "請輸入第二個數字:" sn
s=$(echo $sn |tr -d 0-9)
[ -n "$s" ] && echo "Error: $sn 不是數字" && exit 1

for (( i=1 ; i<=$fn ; i=i+1 ))
do
        for (( j=1 ; j<=$sn ; j=j+1 ))
        do
                total=$(($i*$j))
                echo "$i X $j = $total"
        done
done
exit 0
b修正版:
#在畫面上將結果分開秀出,而不是都在同一列,並增加錯誤判斷
#!/bin/bash
#Author : marcus Tsai 20100913
read -p "請輸入第一個數字1-9:" fn
s=$(echo $fn |tr -d 0-9)
[ -n "$s" ] && echo "Error: $fn 不是數字" && exit 1

[ $fn -gt "10" -o $fn -lt "1" ] && echo "Error: 請輸入1-9的數字" && exit 1
read -p "請輸入第二個數字1-9:" sn
s=$(echo $sn |tr -d 0-9)
[ -n "$s" ] && echo "Error: $sn 不是數字" && exit 1
[ $sn -gt "10" -o $sn -lt "1" ] && echo "Error: 請輸入1-9的數字" && exit 1
for (( i=1 ; i<=$sn ; i=i+1 ))
do
        for (( j=1 ; j<=$fn ; j=j+1 ))
        do
                total=$(($i*$j))
                printf "%s\t " "echo $j X $i = $total"
        done
    echo -e "\n"
done
exit 0

沒有留言:

張貼留言