shell練習題


基礎練習

練習1:打印圖形-打印n行n列矩陣的圖形

#!/bin/bash

#我的shell
#Date: 2017-8-25
#Author: XianWei

#判斷輸入的是否爲數字
while((1))
do
{
        read -p "Please input a number:" num
        if [ $num -gt 0 ] > /dev/null 2>&1
        then
                break           #如果輸入的是一個數字,則跳出循環
        else
                echo "Error,Please input a number"
        fi
}
done



#打印符號
for((i=0;i<$num;i++))           #5 row
do
{
        for((j=0;j<`echo "2 * $num"|bc`;j++))   #10 cols
        do
                echo -n "■"
        done
        echo 
}
done

測試結果

root@vmUbu:/home/dell/shell# ./27.sh   
Please input a number:5
■■■■■
■■■■■
■■■■■
■■■■■
■■■■■
root@vmUbu:/home/dell/shell# ./27.sh 
Please input a number:10
■■■■■■■■■■
■■■■■■■■■■
■■■■■■■■■■
■■■■■■■■■■
■■■■■■■■■■
■■■■■■■■■■
■■■■■■■■■■
■■■■■■■■■■
■■■■■■■■■■
■■■■■■■■■■
root@vmUbu:/home/dell/shell# ./27.sh 
Please input a number:adc   
Error,Please input a number
Please input a number:4
■■■■
■■■■
■■■■
■■■■
root@vmUbu:/home/dell/shell#

練習2:打印圖形-打印三角形圖形


結果如下:

root@vmUbu:/home/dell/shell# ./28.sh 
Please input a number:5
    *
   ***
  *****
 *******
*********
root@vmUbu:/home/dell/shell# ./28.sh 
Please input a number:6
     *
    ***
   *****
  *******
 *********
***********
root@vmUbu:/home/dell/shell#

代碼:

#!/bin/bash

#我的shell
#Date: 2017-2-11
#Author: XianWei

#判斷輸入的是否爲數字
while((1))
do
{
        read -p "Please input a number:" num
        if [ $num -gt 0 ] > /dev/null 2>&1
        then
                break           #如果輸入的是一個數字,則跳出循環
        else
                echo "Error,Please input a number"
        fi
}
done



#打印符號
for((i=1;i<=$num;i++))
do
{
        #每行打印空格數
        for((j=1;j<=`echo "$num - $i"|bc`;j++)) 
        do
                echo -n " "
        done

        #每行打印*的個數
        for((h=1;h<=`echo "2 * $i - 1" |bc`;h++))
        do
                echo -n "*"
        done
        printf "\n" 
}
done


練習3:case的基本使用

代碼

#!/bin/bash

#Date:2017-2-12
#Author: XianWei

read -p "select a number from 1-4:" num

case $num in

1)
        echo -e "\e[1;36m  you have inputed number \"1\"  \e[0m";
        ;;

2)
        echo -e "\e[1;36m  you have inputed number \"2\"  \e[0m";
        ;;
3)
        echo -e "\e[1;36m  you have inputed number \"3\"  \e[0m";
        ;;
4)
        echo -e "\e[1;36m  you have inputed number \"4\"  \e[0m";
        ;;
*)
        echo -e "\e[41m  Error! \e[0m";
        echo -e "\e[1;36m  you have inputed a wrong number \e[0m";
        ;;
esac


練習:腳本中IFS的使用

代碼:

#!/bin/bash

oldIFS=$IFS;    #保存原分隔符
IFS=",";        #將分隔符設置爲:

line="root:x:0:0:root:/root:/bin/bash";
line2="root,x,0,0,root,/root,/bin/bash";

echo -e "\e[1;35m $line \e[0m";

count=0;
for i in $line2
do
        [ $count -eq 0 ] && user=$i;    #前面成功,才之後面     
        [ $count -eq 6 ] && shell=$i;
        let count++;            #let使變量自增
done

#彩色打印字符
echo -e "\e[1;36m  $user\'s shell is $shell        \e[0m";

IFS=$oldIFS;    #還原分隔符


結果

root@vmUbu:/home/dell/shell# ./repeat.sh 
 root:x:0:0:root:/root:/bin/bash 
  root\'s shell is /bin/bash

練習題:until的使用

#!/bin/bash

#Date: 2017-02-12
#Author: XianWei

i=10;
until [ $i -le 0 ]
do
        let i--;
        echo -n -e "\e[1;36m $i \e[0m\c";
done 

echo

測試

root@vmUbu:/home/dell/shell# ./until.sh 
 9  8  7  6  5  4  3  2  1  0



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