基础练习

练习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