第六课 bash编程入门之变量、条件判断、条件测试和for循环等

课程内容:
shell

    一、bash编程入门之变量类型、for循环和算术计算
express

    二、bash编程入门之逻辑运算和条件判断
编程

    三、bash条件测试
bash

    四、bash条件测试
ide


1、bash编程入门之变量类型、for循环和算术计算测试

    bash弱类型:ui

    变量=值(任何无需事先声明,可直接使用;值默认都是字符型 a=abc a=3)this

            普通赋值:a=4   spa

            加强性赋值:命令行

                字符: a=$[$a+1]  +=、-=、*=、/=、%=    a=$a+1 等同于a+=1

                字符串: export PATH=$PATH:/usr/local/bin

                自加:var++ 、var--

            unset 撤销变量赋值(不然一直占用内存)

    算术运算:bash会对数字执行隐式的类型转换

            let VAR_NAME=Integer_value  定义整型

            declare -i Var_name=Integer_value 定义整型

         操做符:+ - * / %(取模) **(次方) 双目运算符(须要至少2个操做数)

    算术运算的方式:

        let Var_Name=expression

        Var_Name=$[expression]

        $((expression)) 如:echo “the sum is: $(($sum1+$sun2))”


    for循环:

        新建10个用户:tuser601-yuser610

        for username in ;do

            useradd $username

        done

        遍历list元素,遍历结束,循环退出。

        #for i in `seq 601 610`; do echo $i;done

        #for i in `seq 601 610`; do echo user$i;done

        #for i in `seq 601 610`; do useradd tuser$i;done

        #for i in {601..610};do useradd tuser$i;done

    bash中的字串链接方式:

        #strl='hello'  #str2='bash'

        #echo "$str1 $str2"  结果为:hello bash

        #echo '$str1 $str2'  结果为:$hello $bash


        #animal=pig  变量引用后方跟直接字串时,变量名要加{} 

        #echo "there are some ${ainimal}s"  显示There are some pigs. #{}指定变量范围


        求100之内全部正整数的和:

        分析:声明一个变量赋值为0,而后让该变量加1,再加2,再加3…………加100,加到最后就是咱们问题所要的结果。

        declare -i sum=0

        for i in {i..100};do

            let sum+=$i

        done

        echo $sum

    练习:分别计算100之内全部偶数之和奇数之和;

        #!/bin/bash

        declare -i evensum=0

        declare -i oddsum=0

        for i in `seq 1 2 100`;do

            oddsum=$[$oddsum+$i]

        done

        for i in `seq 1 2 100`;do

            evensum=$[$evensum+$i]

        done

        echo "evensum: $evensum,oddsum:$oddsum."

    练习:计算当前系统上全部用户的ID之和

        #!/bin/bash

        declare -i idsum=0

        for i in `cut -d: -f3 /etc/passwd`;do

            let idsum+=$i 或者 idsum=$[$idsum+$userid]

        done

        echo $idsum

    练习:新建10个用户user401-410,并求他们的ID之和

    练习:写一个脚本,

        一、建立用户user501-510

        二、建立目录/tmp/dir-当前日期时间

        三、在/tmp/dir-当前日期时间目录中建立10个空文件file101-110

        四、将file101的属主改为user501,一次类推。  

    练习:写一个脚本

        一、脚本能够接受一个以上的文件路径做为参数;

        二、显示每一个文件所拥有的行数;

        三、显示本次共对多少个文件执行了行数统计;

        四、显示全部文件的总行数;


        #!/bin/bash

        #

        declare -i totalLines=0

        declare -i noFiles=0

        for file in $*; do

    curFileLines=`wc -l $file | cut -d' ' -f1`

    echo "$file has $curFileLines."

    let noFiles++

         let totalLines+=$curFileLines

        done

        echo "Total Files: $noFiles."

        echo "Total Lines: $totalLines."

    练习:新建10个用户tuser401-tuser410,并求他们的ID之和;

        #!/bin/bash

        #

        declare -i idsum=0

        for i in {401..410}; do

           useradd tuser$i

           userID=`id -u tuser$i`

           let idsum+=$userID

        done

        echo "ID sum: $idsum."

    练习:写一个脚本

        一、建立用户tuser501-tuser510; 

        二、建立目录/tmp/dir-当前日期时间;

        三、在/tmp/dir-当前日期时间 目录中建立9个空文件file101-file110

        四、将file101的属主改成tuser501,依次类推,一直将file110的属主改成tuser510;

    练习:写一个脚本

        分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/inittab文件中以#            开头的行的行数和空白行数;

        #!/bin/bash

        for file in /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/inittab; do

           echo "The lines contain #  in $file is `grep -E "^#" $file | wc -l`." 

           echo "The space lines in $file is `grep -E "^[[:space:]]*$" $file | wc -l`." 

        done

    练习:写一个脚本

        显示当前系统上全部默认shell为bash的用户的用户名、UID及其全部此类用户的UID之和;

        #!/bin/bash

        #

        grep "/bin/bash$" /etc/passwd | cut -d: -f1,3

        declare -i sum=0

        for userID in `grep "/bin/bash$" /etc/passwd | cut -d: -f3`; do

        let sum+=$userID

        done

        echo "$sum"


2、bash编程入门之逻辑运算和条件判断

    一、逻辑运算:布尔运算:2种状态真、假

        与、或、非、异或        

        与运算:

            真,假:

                    真 && 真 = 真

                    真 && 假 = 假

                    假 && 真 = 假

                    假 && 假 = 假

        或运算:

            真,假

                    真 || 真 = 真

                    真 || 假 = 真

                    假 || 真 = 真

                    假 || 假 = 假

        非运算:

            真,假

        异或运算:

        命令都有其状态返回值:(成功:0,真    失败:1-255, 假)


bash条件测试:

命令执行成功与否即为条件测试

test EXPR

[ EXPR ]

` EXPR `


比较运算:

>, <, >=, <=, ==, !=


测试类型:根据比较时的操做数的类型

整型测试:整数比较

字符测试:字符串比较

文件测试:判断文件的存在性及属性等


注意:比较运算一般只在同一种类型间进行


整型测试:

-gt: 例如 [ $num1 -gt $num2 ]

-lt: 

-ge: 

-le:

-eq:

-ne:


字符串测试:

双目

>: [[ "$str1" > "$str2" ]]

<:

>=

<=

==

!=


单目:

  -n String: 是否不空,不空则为真,空则为假

  -z String: 是否为空,空则为真,不空则假


过程式编程:

顺序

选择

循环:for


选择:if和case


if: 三种使用格式

单分支的if语句

if 测试条件; then

    选择分支

fi

表示条件测试状态返回值为值,则执行选择分支;

if ! id $username &> /dev/null; then

useradd $username

fi

练习:写一个脚本,接受一个参数,这个参数是用户名;若是此用户存在,则显示其ID号;


双分支的if语句:

if 测试条件; then

        选择分支1

else

选择分支2

fi

        两个分支仅执行其中之一。


        练习:经过命令行传递两个整数参数给脚本,脚本能够返回其大者。


练习:经过命令行传递任意个整数给脚本,脚本能够返回其大者。


练习:经过命令行给定一个文件路径,然后判断:

若是此文件中存在空白行,则显示其空白行的总数;

不然,则显示无空白行;

if grep "^[[:space]]*$" $1 &> /dev/null; then

echo "$1 has $(grep "^[[:space]]*$" $1 | wc -l) blank lines."

else

echo "No blank lines"

fi


注意:若是把命令执行成功与否看成条件,则if语句后必须只跟命令自己,而不能引用。


if [ $(grep "^[[:space:]]*$" $1 | wc -l) -lt 1 ]


多分支的if语句:

if 条件1; then

分支1

elif 条件2; then

分支2

elif 条件3; then

分支3

...

else

分支n

fi

练习:传递一个参数给脚本:

若是参数为quit,则显示说你要退出;

若是参数为yes,则显示说你要继续

其它任意参数,则说没法识别;


练习:传递一个用户名给脚本:

若是此用户的id号为0,则显示说这是管理员

若是此用户的id号大于等于500,则显示说这是普通用户

不然,则说这是系统用户;


#!/bin/bash

#

if [ $# -lt 1 ]; then

    echo "Usage: `basename $0` username"

    exit 1

fi


if ! id -u $1 &> /dev/null; then

    echo "Usage: `basename $0` username"

    echo "No this user $1."

    exit 2

fi


if [ $(id -u $1) -eq 0 ]; then

echo "Admin"

elif [ $(id -u $1) -ge 500 ]; then

echo "Common user."

else

        echo "System user."

fi


if 测试条件; then

测试条件:在bash中是命令 (test EXPR, [ EXPR ] ) 或由 ` EXPR `

if 命令;

在bash运行至if时,其后的命令会被执行,其状态结果则做为判断标准:

0:表示真

1-255:表示假

若是条件包含比较之意,则必须使用自定义shell进程的状态返回值:exit [n]












3、条件测试

相关文章
相关标签/搜索