Linux shell腳本編程 條件判斷語句、循環語句

一、條件判斷語句

(1)創建一個簡單的shell程序,輸入學生的成績,給出該成績對應的等級,大於90分爲A,80-89爲B,70-79爲C,60-69爲D,小於60分爲E。要求使用if…elif….else fi實現。

vim t4.sh,編寫內容如下:

#!/bin/bash
num=$#   # get the number of parameter
if [ $num -lt 1 ]; then
        echo "The number of paramter is less than one!"
else
        if [ $1 -lt 0 -o $1 -gt 100 ]; then 
                echo "The grade must be between 0 and 100!"
        elif [ $1 -ge 90 ]; then
                echo "A"
        elif [ $1 -ge 80 -a $1 -lt 90 ]; then # >=80 && <90
                echo "B"
        elif [ $1 -ge 70 -a $1 -lt 80 ]; then
                echo "C"
        elif [ $1 -ge 60 -a $1 -lt 70 ]; then
                echo "D"
        else
                echo "E"
        fi
fi

測試:
在這裏插入圖片描述
(2)創建一個簡單的Shell程序,實現輸入目錄名,查看當前文件夾下有沒有這個目錄。如果沒有則創建該目錄,若已存在則輸出“exist”。

vim t3.sh,編寫內容如下:

#!/bin/bash
num=$#   # get the number of parameter
if [ $num -lt 1 ]; then
        echo "The number of paramter is less than one!"
else
        if test -d $1; then
                echo "The dirctory already exists!"
        else
                mkdir $1
                echo "The new dirctory $1 has been made!"
        fi
fi

測試:
在這裏插入圖片描述
(3)創建一個簡單的Shell程序,要求帶一個參數,判斷該參數是否是水仙花數。所謂水仙花數是指一個 3位數,它的每個位上的數字的 3次冪之和等於它本身。例如153=13+33+53,153是水仙花數。編寫程序時要求首先進行參數個數判斷,判斷是否帶了一個參數,如果沒有參數則給出提示信息,否則給出該數是否是水仙花數。要求對153,124,370分別進行測試判斷。

vim t2.sh,編寫內容如下:

#!/bin/bash
num=$#   # get the number of parameter
if [ $num -lt 1 ]; then
        echo "The number of paramter is less than one!"
else
        x=$1 # get the first parameter
        if [ $x -gt 999 -o $x -lt 100 ]; then
                echo "The number must be between 100 and 999!"
        else
        		a=$(($x/100))
        		b=$(($x/10%10))
        		c=$(($x%10))
       			echo a=$a b=$b c=$c
        		a=$(($a*$a*$a))
        		b=$(($b*$b*$b))
        		c=$(($c*$c*$c))
        		echo a^3=$a b^3=$b c^3=$c 
        		if [ $(($a+$b+$c)) = $x ]; then
                	echo "Yes! $a + $b + $c = $x"
        		else
                	echo "No!  $a + $b+ $c != $x"
            	fi
       	 fi
fi

測試:
在這裏插入圖片描述

二、循環語句

(1)編寫一個shell腳本,利用for循環把當前目錄下的所有*.sh文件複製到指定的目錄中,併爲沒有執行權限的文件添加執行權限。(可以在當前目錄下先建立幾個*.sh文件,用來測試,複製到的指定目錄可以自己建立一個)

vim for.sh,編寫內容如下:

#!/bin/bash
filelist=`ls ./*sh`
echo $filelist
read -p "Please input dir path: " dir
test -e $dir || mkdir $dir	# dir不存在則自動創建
for i in $filelist
do
        cp $i $dir
        chmod a+x $dir/$i
done

在當前目錄中準備好t1.sh,t2.sh,t3.sh,然後進行測試:
在這裏插入圖片描述
(2) 編寫shell腳本,輸入10個數,利用while循環求其偶數之和。

#!/bin/bash
i=1
sum=0
while [ $i -le 10 ]
do
        read -p "Please input the number $i: " x
        let i++
        if [ $(($x%2)) == 0 ]; then
                let sum=sum+x
        fi
        echo "Now sum=$sum"
done
echo Final sum=$sum

注意,這裏使用到了let命令,ubuntu可能會找不到let命令,因爲/bin/sh指向了dash而不是bash,dash不支持let命令,解決辦法是輸入命令 sudo dpkg-reconfigure dash,之後選擇否即可。
在這裏插入圖片描述
測試:
在這裏插入圖片描述
(3) 編寫shell腳本,利用until循環求1到10的平方和。(until循環的條件其實就是while循環中條件的補集)

#!/bin/bash
i=1
sum=0
until [ $i -gt 10 ]	# 等價於 while [ $i -le 10 ]
do
        let sum=sum+i*i
        let i++
done
echo sum=$sum

測試:
在這裏插入圖片描述
(4)運行下列程序,觀察程序的運行結果。”break 或者 continue“語句分別爲break,break 2,continue,continue 2,觀察四種情況下的實驗結果。

#!/bin/bash
for i in a b c d
do
	echo -n $i  # -n表示不換行
		for j in 1 2 3 4 5 6 7 8 9 10
		do
			if [ $j == 5 ]; then
				break 或者 continue # 修改這行
			fi
			echo -n $j
		done
	echo # 換行
done

①語句改成break,運行結果:
在這裏插入圖片描述
②語句改成break 2,運行結果:
在這裏插入圖片描述
③語句改成continue,運行結果:
在這裏插入圖片描述
④語句改成continue 2,運行結果:
在這裏插入圖片描述
結論:break 2表示先跳出兩層循環再結束當前層的循環,continue 2表示先跳出兩層循環再繼續當前層的下一次循環。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章