bash 編程中迴圈語句用法_linux shell

來源:互聯網
上載者:User
1.if 是單分支語句,使用格式如下:
if condition ; then
statement
…..
fi
2.if … else 是雙分支語句,使用格式如下:
if condition ; then
statement
….
else
statement
….
fi
3.if …elif…elif…else 是多分支語句,使用格式如下:
if condition ; then
statement
….
elif condition ; then
statement
…..
elif condition ; then
statement
…..
.
.
.
else
statement
….
fi
4.while 語句是迴圈語句,當條件滿足的情況下才迴圈,不滿足則退出迴圈,使用格式如下:
while condition ; do
statement
…..
done
5.until 語句也是迴圈語句,當條件不滿足的情況下迴圈,滿足則不迴圈,使用格式如下:
until condition ; do
statement
…..
done
6.case 也是迴圈語句,使用格式如下:
case $var(變數) ; in
value1)
……

value2)
…..

*)

..
..
..
esac

指令碼練習:

1.計算100以內所有能被3整除的正整數的和。
複製代碼 代碼如下:

#!/bin/bash
declare -i sum=0
for I in {1..100}; do
if [ $[$I%3] -eq 0 ]; then
let sum+=$I
fi
done
echo " the sum is :$sum"

2.計算100以內所有奇數的和以及所有偶數的和
複製代碼 代碼如下:

#!/bin/bash
# echo "exercise"
declare -i sum1=0
declare -i sum2=0
for I in {1..100}; do
if [ $[$I%2] -eq 0 ]; then
let sum1+=$I
else
let sum2+=$I
fi
done
echo " the even sum is :$sum1"
echo " the oddnumber sum is :$sum2"

3.判斷/var/log下的檔案的類型:
如果是普通檔案,則說明其為普通檔案;
如果是目錄檔案,則說明其為目錄檔案;
如果是符號連結檔案,則說明其為符號連結檔案;
否則,說明檔案類型無法識別;
複製代碼 代碼如下:

#!/bin/bash
file1=/var/log/*
for file in $file1 ; do
if [ -f $file ]; then
echo "$file is common file"
elif [ -d $file ]; then
echo "$file is directory file"
else
echo "$file is unknow"
fi
done

4.寫一個指令碼,分別顯示當前系統上所有預設shell為bash的使用者和預設shell為
/sbin/nologin的使用者
並統計各類shell下的使用者總數,顯示結果形如:bash,3user,they
are:root,redhat,gentoo nologn,2user,they are:bin,ftp
複製代碼 代碼如下:

#!/bin/bash
file=/etc/passwd
bsh='/bin/bash'
nobsh='/sbin/nologin'
use=`cat $file | cut -d: -f1`
declare -i d1=0
declare -i d2=0
for I in $use ; do
s=`grep "^$I:" $file | cut -d: -f7`
if [ "$s" = $bsh ] ; then
let d1=$d1+1
muser=$I\,$muser
elif [ "$s" = $nobsh ] ; then
let d2=$d2+1
suser=$I\,$suser
fi
done
echo "BASH,$d1 users ,they are:"
echo $muser
echo
echo "NOLOGIN,$d2 users ,they are:"
echo $suser

5.寫一個指令碼:
(1)如果不存在,就建立檔案/tmp/maintenance;如果存在,就事先刪除
(2)在檔案/tmp/maintenance中添加如下內容:
172.16.0.6
172.16.0.17
172.16.0.20
(3)測試172.16.0.0/16網路內的所有主機是否線上,如果線上就顯示其線上,如果此主機
在/tmp/maintenance檔案中,就顯示其正處於維護狀態;否則,就顯示其狀態未知;
複製代碼 代碼如下:

#!/bin/bash
file=/tmp/maintenace
if [ -e $file ]; then
rm -rf $file &> /dev/null
fi
touch $file
cat >> $file << EOF
172.16.0.6
172.16.0.17
172.16.0.20
EOF
bnet=172.16
for net in {0..254} ; do
for host in {1..254} ; do
if ping -c1 -W1 $bnet.$net.$host &> /dev/null ; then
echo "$bnet.$net.$host is up."
elif grep "$bnet.$net.$host$" $file &> /dev/null ;then
echo "$bnet.$net.$host is under maintenance."
else
echo "$bnet.$net.$host state is unknow."
fi
done
done

6寫一個指令碼,完成以下功能:
(1)、提示使用者輸入一個使用者名稱;
(2)、顯示一個菜單給使用者,形如:
U|u show UID
G|g show GID
S|s show SHELL
Q|q quit
(3)、提醒使用者選擇一個選項,並顯示其所選擇的內容;如果使用者給的是一個非上述所提示的選項,則提醒使用者給出的選項錯誤,並請其重新選擇後執行;
第一種方法:
複製代碼 代碼如下:

#!/bin/bash
read -p "Enter a user name:" username
! id $username &> /dev/null && echo " Come on ,the user you input unexit" && exit 9
cat << EOF
U|u show UID
G|g show GID
S|s show SHELL
Q|q quit
EOF
read -p "Enter your choice:" op
case $op in
U|u)
id -u $username;;
G|g)
id -g $username;;
S|s)
grep "^$username\>" /etc/passwd | cut -d: -f7;;
Q|q)
exit 8 ;;
*)
echo "input option wrong ,quit"
exit 9

esac

第二種方法:
複製代碼 代碼如下:

#!/bin/bash
read -p "Enter a user name:" username
! id $username &> /dev/null && echo "Come on ,you input user notexit" && exit 9
cat << EOF
U|u show UID
G|g show GID
S|s show SHELL
Q|q quit
EOF
read -p "Enter your option:" op
while true; do
case $op in
U|u)
id -u $username
break

G|g)
id -g $username
break

S|s)
grep "^$username\>" /etc/passwd | cut -d: -f7
break

Q|q)
exit 7 ;;
*)
read -p "Wrong option,Enter a right option:" op ;;
esac
done

7寫一個指令碼:
(1)、判斷一個指定的指令碼是否是語法錯誤;如果有錯誤,則提醒使用者鍵入Q或者q無視錯誤並退出,其它任何鍵可以通過vim開啟這個指定的指令碼;
(2)、如果使用者通過vim開啟編輯後儲存退出時仍然有錯誤,則重複第1步中的內容;否則,就正常關閉退出。
第一種方法
複製代碼 代碼如下:

#!/bin/bash
[ ! -f $1 ] && echo "wrong path." && exit 2
until bash -n $1 &> /dev/null ; do
read -p " Q|q to quit .others to edit:" opt
case $opt in
Q|q)
echo "quit..."
exit 3

*)
vim $1

esac
done

第二種方法:
複製代碼 代碼如下:

#!/bin/bash
[ ! -f $1 ] && echo "wrong path." && echo "Quit!" && exit 9
until bash -n $1 &> /dev/null ; do
read -p " Grammar wrong please enter Q|q to quit .others to edit:" opt
case $opt in
Q|q)
echo "quit..."
exit 3

*)
vim $1
bash -n $1 &> /dev/null
val=$?
[ "$val" -ne 0 ] && echo "xiu gai bu cheng gong. "

esac
done

第三種方法
複製代碼 代碼如下:

#!/bin/bash
[ ! -f $1 ] && echo "Wrong scripts." && exit 4
bash -n $1 &> /dev/null
valu=$?
until [ $valu -eq 0 ] ; do
read -p "Q|q to quit ,others to edit:" op
case $op in
Q|q)
echo "Quit."
exit 9

*)
vim $1
bash -n $1 &> /dev/null
valu=$?

esac
done

8 寫一個指令碼:
查看redhat使用者是否登入了系統,如果登入了,就通知當前指令碼執行者“redhat
is logged on.”;否則,就睡眠5秒鐘後再次進行測試;直到其登入狀態止退出;
第一種方法
複製代碼 代碼如下:

#!/bin/bash
who | grep "^redhat\>" &> /dev/null
reval=$?
until [ $reval -eq 0 ] ;do
sleep 5
who | grep "^redhat\>" &> /dev/null
reval=$?
done
echo "redhat is logged on."

第二種方法:
複製代碼 代碼如下:

#!/bin/bash
until who | grep "^redhat\>" &> /dev/null ; do
sleep 5
done
echo "redhat is logged on"

9寫一個指令碼:
(1)、向系統中添加20個使用者,名字為linuxer1-linuxer20,密碼分別為其使用者名稱,要使用while迴圈;
(2)、要求:在添加每個使用者之前事先判斷使用者是否存在,如果已經存在,則不再添加此使用者;
(3)、添加完成後,顯示linuxer1-linuxer20每個使用者名稱及對應的UID號碼和GID號碼,形如 stu1, UID: 1000, GID: 1000
複製代碼 代碼如下:

#!/bin/bash
declare -i I=1
while [ $I -le 20 ] ; do
l=linuxer$I
let I++
! id $l &> /dev/null && useradd $l &> /dev/null && echo "the user:$l" | passwd --stdin $l &> /dev/null && echo "a dd user $l successfully" || echo " The user $l is exit. "
d=`id -u $l`
g=`id -g $l`
echo " $l ,UID:$d,GID:$g "
done

本文出自 “知識體系” 部落格
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.