shell的執行流控制

2021-10-21 13:47:53 字數 1556 閱讀 3017

case $1 in

word1|word1)

action1

;;word2|word2)

action2

;;*)

action3

esac

dnf install expect -y

問題指令碼:

#!/bin/bash

read -p 「what』s your name:」 name

read -p 「how old are you:」 age

read -p 「which objective:」 obj

read -p 「are you ok?」 ok

echo $name is $age』s old stady $obj feel $ok

應答指令碼:

#!/usr/bin/expect

set timeout 1

set name[ lindex $ar** 0 ]

set age[ lindex $ar** 1 ]

set obj[ lindex $ar** 2 ]

set ok[lindex katex parse error: expected '}', got 'eof' at end of input: … "name"

「old」 …obj\r」;exp_continue}

「ok」

}expect eof

格式如下

ip 主機名稱

例如:172.25.254.1 westos_studen1.westos.org

continue #終止當此次前迴圈提前進入下個迴圈(跳出單次迴圈)

break #終止當前所在語句所有動作進行語句外的其他動作(影響的是當前所在的迴圈)

exit #指令碼退出

$0返回值所表示的大概含義:

0 表示成功

2 許可權拒絕

1~125 表示執行失敗,指令碼命令,系統命令錯誤或引數傳遞錯誤

126 找到該命令,但是無法執行

127 為找到要執行的命令

128 命令被系統強制結束

$0的返回值可以自己設定:

shell指令碼 流控制

語法結構 如果 條件判斷中有多條命令,則根據最後一條命令的執行結果進行評估 ifcommand then command elif command then command else command fi bin bash x 5if x 5 then echo x equals 5 else ec...

Shell程式設計 流控制

if語句 1 寫if語句時不要隨意加空格,比如if 1 2 這是假,而if 1 2 就是真了,因為1和 之間有空格。注意與1,2之間的空格是合法的,總結一下就是,shell中表示式中最好不要加空格,比如a 1,1 2,2 2等等,如果加上空格,往往會產生錯誤。2 使用test,格式 if test ...

shell指令碼中的控制流結構

控制結構包括 流控制和迴圈 流控制 if then else,測試結果或者為真 0 或者為假 1 迴圈 for until while 一 if then else 語句 格式 if 條件1 then命令1 elif條件2 then 命令2 else 命令3 fi 簡單的格式 if 條件 then ...