bash腳本執行的控制語句

一、bash腳本執行的控制語句:

1、順序執行:默認,逐條執行各語句。


2、選擇執行if:分支,條件判斷,執行符合條件的分支。

(1)、單分支:

            if 條件;then

              分支

            fi

(2)、雙分支

            if 條件;then

              分支1

            else

              分支2

            fi

(3)、多分支:

            if 條件1;then

              分支1

            elif 條件2;then

              分支2

            elif 條件3;then

              分支3

            ...

            else

               分支n

            fi

(4)例1:寫一個腳本,實現如下功能:

        1、讓用戶通過鍵盤輸入一個用戶名

        2、如果用戶存在,就顯示其用戶名和UID

        3、否則,就顯示用戶不存在。

    #!/bin/bash

    read -t 10 -p "Enter a username:" userName

    if id $userName $> /dev/null;then

    userID=`id -u $userName`

    else

    echo "$userName not exist"

    fi


例2:寫一個腳本,實現如下功能:

    1、讓用戶通過鍵盤輸入一個用戶名,如果用戶不存在就退出

    2、如果用戶的UID大於等於500,就說明是普通用戶

    3、否則,就說明是管理員或者系統用戶。

#!/bin/bash

read -t 10 -p "Enter a userName" userName

if ! id $userName $> /dev/null;then

  echo "$userName not exist."

  exit 6

fi


userID=`id -u $userName`


if [ $userID -ge 500 ];then

   echo "A common user."

else 

   echo "Admin or System user."

fi


例3:寫一個腳本,實現如下功能:

    1、讓用戶通過鍵盤輸入一個用戶名,如果用戶不存在就退出

    2、如果用戶的UID等於GID,就說它是“good guy”

    3、否則,就說它是“bad guy”


#!/bin/bash

read -t 10 -p "Enter a userName" userName

if ! id $userName $> /dev/null;then

  echo "$userName not exist."

  exit 6

fi


if [ `id -u $userName` -eq `id -g $userName` ];then

   echo "Good guy."

else 

   echo "Bad guy."

fi



3、循環執行:將同一段代碼反覆執行有限次。

(1)、for:實現知道循環次數,

for var_name in 列表;

do

循環體

done


例1、循環執行添加三個用戶;xiaowang,xiaoli,xiaosun

#!/bin/bash

for userName in xiaowang xiaoli xiaosun;

do

useradd $userName

done


(2)列表的生成方法:

生成數字:{start..end},seq [start] [step] end


例1:生成10個用戶,分別爲user101...user110

for userName in `seq 101 110`;

do

 useradd user$userName

done


例2:將上面生成的10個用戶刪除,同時刪除用戶家目錄

for userName in {101..110};

do

 userdel -r user$userName

done


例3:寫一個腳本,用file命令顯示/var/log目錄下的每個文件的內容類型

#!/bin/bash

dirName=/var/log

for  fileName in $dirName/*

do 

file $fileName

done


例4:寫一個腳本,要求如下

    a、創建/tmp/scripttest目錄,用變量名保存目錄名

    b、在目錄裏創建測試文件tfile1-tfile20

    c、創建用戶testuser1和testuser2

    d、將tfile1-tfile10的屬主和屬組改爲testuser1

    e、將tfile11-tfile20的屬主和屬組改爲testuser2

#!/bin/bash

dirName=/tmp/scripttest

mkdir $dirName

for fileNo in {1..20}

 do

  touch $dirName/tfile$fileNo

 done

   useradd testuser1

   useradd testuser2


for fileNo in {1..10}

  do

        chown testuser1:testuser1 $dirName/tfile$fileNo

  done


for fileNo in {11..20}

  do

        chown testuser2:testuser2 #dirName/file$fileNo

  done

或者

#!/bin/bash

dirName=/tmp/scripttest

mkdir $dirName

   useradd testuser1

   useradd testuser2


for fileNo in {1..10}

 do

  touch $dirName/tfile$fileNo

  chown testuser1:testuser1 $dirName/tfile$fileNo

 done


for fileNo in {11..20}

 do

  touch $dirName/tfile$fileNo

  chown testuser2:testuser2 $dirName/tfile$fileNo

 done

例5:用for寫一個腳本,要求如下:

    1、顯示/etc/init.d/functions、/etc/rc.d/rc.sysinit和/etc/fstab各有多少行

    2、輸出格式爲:/etc/init.d/functions: 行數 lines.

                   /etc/rc.d/rc.sysinit: 行數 lines.

                   /etc/fstab: 行數 lines.

    #!/bin/bash

      for fileName in /etc/init.d/functions /etc/rc.d/rc.sysinit /etc/fstab

      do

        lineCount=`wc -l $fileName|cut -d' ' -f1`

        echo "$fileName :$lineCount lines"

      done

例6:顯示/etd/passwd中的第3、7和11行中的用戶名和ID號

    

  #!/bin/bash

      for lineNo in 3 7 11

      do

        head -n #lineNo|tail -1|cut -d: -f1,3

      done


例7:判斷當前系統上的所有用戶是Good guy還是Bad guy,條件如下:

      1、如果用戶的UID等於GID,就說它是“good guy”

      2、否則,就說它是“bad guy”

#!/bin/bash


for userName in `cut -d :-f1 /etc/passwd`; do


if [ `id -u $userName` -eq `id -g $userName` ];then

   echo "Good guy."

else 

   echo "Bad guy."

fi


done



例8:求200內所有3的整數倍的正整數的和

#!/bin/bash

declare -i sum=0

for i in {1..200};do

   if [ $[$i%3] -eq 0 ];then

let sum+=$i

   fi

done

echo "The sum is :$sum."


(3)、while:條件滿足則循環,否則退出。

while 條件測試;do

    循環體;

done

例如:

求100以內所有正整數的和。

declare -i sum=0;i=1

while [ $i -le 100 ];do

 let sum+=$i

 let i++

done

echo $sum


(4)、until:條件不滿足則循環,否則退出。看以看出它與while相反。

until 測試條件;do

 循環體

done


二、檢查bash腳本語法命令:bash -n 腳本文件

    

三、執行腳本:bash 腳本文件,無需修改文件的執行權限


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