天天看點

shell中的語句(for,while,if,case,expect,exit,break,continue)

1.for語句

格式:

for i in 1 2 3		//i一次取值1,2,3
for i in {1..3}		//i一次取值1,2,3
for i in `seq 1 10`		//i從1到10,每次加1(預設值)
for i in `seq 1 2 10`	//i從1到10,每次加2
for((i=1;i<10;i++))	//與c語言類似,隻是變成了(())
do 
	echo $i
done
           

for語句示例:

for NAME in westos linux 666
do
	echo $NAME
done
           
shell中的語句(for,while,if,case,expect,exit,break,continue)

2.while語句

格式:

while 條件
do
done
           

例1:與c語言類似

#!/bin/bash
read -p "please input a data: " WORD
while(("$WORD"<3))
do
	echo "$WORD<3"
	((WORD=$WORD+1))
done
           
shell中的語句(for,while,if,case,expect,exit,break,continue)

例2:

#!/bin/bash
while true		//成立--不成立(false)
do
	clear		//清屏
	uptime
	sleep 5		//睡眠5秒;每5秒執行一次uptime
done
           
shell中的語句(for,while,if,case,expect,exit,break,continue)
shell中的語句(for,while,if,case,expect,exit,break,continue)

每5秒執行一次,while後的條件一直成立

3.if 語句

格式: 類比c語言

if			//條件1
then
elif		//條件2
then
else		//其他條件
fi
           

例1:

if [ "$1" == "linux" ]
then
	echo linux
elif [ "$1" == "westos" ]
then
	echo westos
else
	echo bey
fi
           
shell中的語句(for,while,if,case,expect,exit,break,continue)

例2:

建立檔案userfile中的使用者,密碼在檔案passwdfile中

#!/bin/bash
if [ $# -lt 2 ]
then
	echo "Error: please input tow files following srcipt!"
elif [ ! -e $1 ]
then
	echo "Error: $1 is not exits!"
elif [ ! -e $2 ]
then
	echo "Error: $2 is not exits!"
else
	Num=`awk 'BEGINN=0}{N++}END{print N}' userfile`
	for I in `seq 1 $Num`
	do 
		USERNAME=`sed -n ${I}p userfile`
		PASSWD=`sed -n ${I}p passwdfile`
		useradd $USERNAME
		echo $PASSWD | passwd --stdin $USERNAME > /dev/null
		echo $USERNAME is created
	done
fi
           
shell中的語句(for,while,if,case,expect,exit,break,continue)

4.case 語句

格式:

case
word1 )
action1
;;
word2)
action2
;;
........
*)
action_last
esac
           

列1:

#!/bin/bash
case $1 in
        1)			//$1=1時,輸出a
        echo a
        ;;
        2)			//$1=2時,輸出b
        echo b
        ;;
        *)			//$1不等于1或2時,輸出c
        echo c
esac
           
shell中的語句(for,while,if,case,expect,exit,break,continue)

5.expect

expect 是自動應答指令用于互動式指令的自動執行
spawn 是 expect 中的監控程式,其運作後會監控指令提出的互動問題
send 發送問題答案給互動指令
“\r” 表示回車
exp_continue 标示當問題不存在時繼續回答下面的問題
expect eof 标示問題回答完畢退出 expect 環境
interact 标示問題回答完畢留在互動界面
set NAME [ lindex $argv n ] 定義變量

expect指令需要安裝expect

shell中的語句(for,while,if,case,expect,exit,break,continue)

例1:

vim ask.sh

#!/bin/bash	
read -p "What's your name: " NAME
read -p "How old are you: " AGE 
read -p "Which object you study: " OBJ 
read -p "Are you happy: " FEEL
echo $NAME is $AGE\'s old study $OBJ feel $FEEL
           

//執行時:輸入一次執行一次,不能實作自動執行

shell中的語句(for,while,if,case,expect,exit,break,continue)

例2: 例1互動時每次都需要輸入,比較麻煩,為實作自動互動,使用expect

vim ask.sh

#!/bin/bash
read -p "What's your name: " NAME
read -p "How old are you: " AGE 
read -p "Which object you study: " OBJ 
read -p "Are you happy: " FEEL
echo $NAME is $AGE\'s old study $OBJ feel $FEEL
           

vim answer.exp

#!/usr/bin/expect
spawn sh /mnt/ask.sh
expect "name"
send "ylz\r"
expect "old"
send "21\r"
expect "object"
send "linux\r"
expect "happy"
send "sad\r"
expect eof 
           
shell中的語句(for,while,if,case,expect,exit,break,continue)

例3: 例2的互動時固定的,在檔案中,不友善更改

vim answer.exp

#!/usr/bin/expect
set NAME [ lindex $argv 0 ] 
set AGE [ lindex $argv 1 ] 
set OBJ [ lindex $argv 2 ] 
set FEEL [ lindex $argv 3 ] 
spawn sh /mnt/ask.sh
expect {
	"name" { send "$NAME\r" ; exp_continue }
	"old" { send "$AGE\r" ; exp_continue }
	"object" { send "$OBJ\r" ; exp_continue }
    "happy" { send "$FEEL\r" }
}
expect eof 
           
shell中的語句(for,while,if,case,expect,exit,break,continue)

6.腳本中的語句控制器

exit 腳本退出
break 退出目前循環
continue 結束目前循環,進入下一次循環

結合c語言了解,基本一樣

(1)exit

shell中的語句(for,while,if,case,expect,exit,break,continue)
shell中的語句(for,while,if,case,expect,exit,break,continue)

(2)break

shell中的語句(for,while,if,case,expect,exit,break,continue)
shell中的語句(for,while,if,case,expect,exit,break,continue)

(3)continue

shell中的語句(for,while,if,case,expect,exit,break,continue)
shell中的語句(for,while,if,case,expect,exit,break,continue)

繼續閱讀