1. 程式人生 > >Shell多分支if語句

Shell多分支if語句

一 語法 if [ 條件判斷式 ] then 當條件判斷式1成立時,執行程式1 elif[ 條件判斷式2 ] then 當條件判斷式2成立時,執行程式2 省略更多條件 else 當所有條件都不成立時,最後執行程式 fi 二 實現計算器 #!/bin/bash read -t 30 -p "please input num1:" num1 read -t 30 -p "please input num2:" num2 read -t 30 -p "please input a operator:" ope if [ -n "$num1" -a -n "$num2" -a -n "$ope" ] then test1=$(echo $num1|sed 's/[0-9]//g') test2=$(echo $num2|sed 's/[0-9]//g') if [ -z "$test1" -a -z "$test2" ] then if [ "$ope" == "+" ] then sum=$(($num1 + $num2)) elif [ "$ope" == "-" ] then sum=$(($num1 - $num2)) elif [ "$ope" == "*" ] then sum=$(($num1 * $num2)) elif [ "$ope" == "/" ] then sum=$(($num1 / $num2)) else echo "Please enter a valid symbol" exit 10 fi else echo "please input a num" exit 11 fi else echo "please input a content:" exit 12 fi echo "$num1 $ope $num2 : $sum" 三 測試
please input num1:re please input num2:23 please input a operator:1 please input a num [[email protected] shell]# echo $? 11 [[email protected] shell]# ./shell5.sh please input num1: please input num2: please input a operator: please input a content: [[email protected] shell]# echo $? 12 [
[email protected]
shell]# ./shell5.sh please input num1:12 please input num2:23 please input a operator:gf Please enter a valid symbol [[email protected] shell]# echo $? 10 [[email protected] shell]# ./shell5.sh please input num1:32 please input num2:12 please input a operator:- 32 - 12 : 20 [
[email protected]
shell]# ./shell5.sh please input num1:76 please input num2:45 please input a operator:* 76 * 45 : 3420 [[email protected] shell]# ./shell5.sh please input num1:87 please input num2:56 please input a operator:/ 87 / 56 : 1 [[email protected] shell]# ./shell5.sh please input num1:45 please input num2:23 please input a operator:+ 45 + 23 : 68 四 判斷使用者輸入的是什麼檔案 #!/bin/bash read -t 30 -p "please input a file name:" file if [ -z "$file" ] then echo "qing shuru neirong" exit 11 elif [ ! -e "$file" ] then echo "qing shuru wenjianming" exit 12 elif [ -f "$file" ] then echo "$file is a regular file" elif [ -d "$file" ] then echo "$file is a mulu" else echo "$file is another file" fi

相關推薦

Shell分支if語句

一 語法 if [ 條件判斷式 ] then 當條件判斷式1成立時,執行程式1 elif[ 條件判斷式2 ] then 當條件判斷式2成立時,執行程式2 省略更多條件 else 當所有條件都不成立時,最後執行程式 fi 二 實現計算器 #!/bin/bash read -t 30 -p "please i

shell算術運算和檔案測試及分支if語句

shell算術運算 1、let 算術運算表示式 例如:let C=$A+$B 2、$[ 算術表示式 ] 例如:中括號和表示式之間必須有空格 3、$((算術表示式)) 例如: 4、expr 算術表示式 例如:表示式要用反引號,運算子之間要有空格 shell中的條件測試型別 整數測試 -eq

shell算術運算和文件測試及分支if語句

給定 括號 如果 ESS 結構 cto color 單分支 算術表達式 shell算術運算 1、let 算術運算表達式 例如:let C=$A+$B 2、$[ 算術表達式 ] 例如:中括號和表達式之間必須有空格 3、$((算術表達式)) 例如: 4、expr 算術表達式 例

分支if語句

bash 多分支 file sysinit elif 多分支if 分支 sys unknown #!/bin/bashFILE=/etc/rc.d/rc.sysinitif [ ! -e $FILE ] ; theecho "No such file."

6、分支if結構和case循環語句

多分支if case循環編程我們有面向過程 控制結構 順序結構 選擇結構 if語句 循環結構 for 選擇結構:if:單分支、雙分支、多分支if CONDITION;then語句fiif CONDITION;t

shell 中的if語句

bsp != -o 語句 col spa shell clas if語句 if [ t != "." -a t != ".." ] 之前一直不知道 -a 是什麽意思,後來才知道 -a = and ; -o = orshell 中的if語句

shell指令碼值if語句

在shell指令碼中的if語句的語法和java中或者C語言中的語法格式是不一樣的,具體的語法格式如下所示: 整數變量表達式             if [ a -eq b ] //== equal      &nbs

Shell程式設計-06-Shell中的if語句

    在任何一門語言中,判斷語句總是少不了,今天來學習一下Shell中的if語句。 基本語法 單分支情況 第一種語法 if <條件表示式> then 語句 fi 第二種語法 if <條件表示式>;then 語句 fi 其中條件表示式部分可以是tes

[Java] 使用if 語句、else if 和 switch 的區別

多個if是所有的if都會進行判斷if else if是隻要有滿足條件的,就不再對之後的else if進行判斷。 比如 a = 2; if(a==1) c=1; if(a==2) c=2; if(a%

shell編程之文件測試及if分支語句

oot rect 否則 his 內容 pre $2 上一個 調試 文件測試用法: -e :判斷文件是否存在,如果不存在返回的狀態為假 -f :判斷文件是否為普通文件 -d :判斷是否為目錄 -x :判斷當前用戶是否可執行此文件 -w

switch 分支語句,及其流程圖,和if的對比和執行機制

switch : 在程式設計中常見的問題就是檢測一個變數是否符合某個條件,如果條件不符合,再用另一個值來檢測,以此類推.當然也可以用if語句來完成也可以,如輸入月份判斷一年的季節,程式碼如下:public static void main(String[] args) {

js中的if 語句分支語句、巢狀語句的用法

if語句的分類: 1、if 2、if  else 程式碼示例: var score=65; var daily=70; if (score>60 &&daily>60){ alert("順利畢業") }e

用於分支的條件語句if-else語句

這裡寫程式碼片 #include<stdio.h> main() { int a,b,max; printf("Input a,b:"); scanf("%d %d",&a,&b); if(a&

[shell]shellif語句的使用

don 條件 如何 ood 字符串 pre 評分 string 另一個 轉自:http://lovelace.blog.51cto.com/1028430/1211353 bash中如何實現條件判斷?條件測試類型: 整數測試 字符測試 文件測試 一、條

shell腳本的if語句,判斷某程序是否存在,不存在啟動該程序!

if語句 .... png div image 一個 ... 語法 新手 想必大家都知道 “如果......那麽......“ 這種語法的應用吧! 當然呢,linux下對於這種用法也是有所考慮的,很多時候我們都需要寫一個shell腳本,難免會避免if語句。 例如:判斷Apa

使用if語句編寫Shell腳本

shell腳本 if語句 楊書凡 shell $? 在簡單的shell腳本程序中,各條語句將按先後順序依次執行,從而實現批處理的自動化過程。然而,單一的順序結構使得腳本不夠智能,難以處理更加靈活的系統任務。通過正確的使用if語句,使得shell腳本具有一定的判斷能力,以根據不同的條

shell腳本之if語句

if語句 技術分享 ces 分享圖片 color term text ffffff shel 1.單分支if語句:格式:示例:2.雙分支if語句:格式:示例:3.多分支if語句:格式:示例:shell腳本之if語句

Shell腳本應用(if語句的結構)

返回值 測試 執行 數值 表達 邏輯 下一個 否則 語句 1、測試:檢測表達式是否成立,成立則返回值為0,否則為非0方法:1)test 表達式2)【 表達式 】2、文件測試:-d:是否為目錄-f:是否為文件-e:是否存在-r:是否有讀取權限-w:是否有寫入權限-x:是否有

02: shell中的if、case、for等語句

play services 命令 eee chmod ima grep 創建用戶 open 1.1 shell中常用運算符 運算符 描述 示例 文件比較運算符 -e filename 如果 filename 存在,則為真 [ -e /var/log/syslo

Shell腳本(2)-if語句

if語句if語句1、什麽是條件測試操作?要使Shell腳本具有一定的“智能化”,面臨的問題就是區分不同情況下以確定執行何種操作。Shell環境中根據命令執行後的返回狀態值($?)來判斷是否執行成功,當返回值為0時,表示執行成功,否則表示失敗或異常。通過條件測試操作的工具-test命令,可以對特定條件進行測試。