1. 程式人生 > >Shell程式設計-條件判斷

Shell程式設計-條件判斷

1.test
語法 test expression
或[ expression ]

[ expression ]更加常用

2例子
1)判斷字串是否相等
test str1==str2
例子:
[[email protected]_0_16_centos ~]# test '12'=='12'
[[email protected]_0_16_centos ~]# echo $?
0

另一種寫法:
[[email protected]_0_16_centos ~]# [ "12"=="12" ];echo $?
0
2)判斷不相等
test str1!=str2

3)測試字串是否不為空
test str1

例子:
[[email protected]
_0_16_centos ~]# test $sum1;echo $?
0
表示非空

[[email protected]_0_16_centos ~]# test $sum2;echo $?
1
表示為空


4)判斷字串是否不為空
test -n str1
注意變數需要加上雙引號:
[[email protected]_0_16_centos ~]# test -n "sum";echo $?

5)判斷字串是否為空
test -z str1






3.整數測試
使用-lt, -le, -ne, -gt, -ge等
例子
[[email protected]_0_16_centos ~]# [ 1 -ge 1 ];echo $?
0

[
[email protected]
_0_16_centos ~]# [ 1 -lt 1 ]; echo $?
1
[[email protected]_0_16_centos ~]#





4.檔案測試
test -d file   指定檔案是否目錄
test -e file   檔案是否存在
test -f file   指定檔案是否常規檔案
test -L file   檔案存在並且是一個符號連結
test -r file   檔案是否可讀
test -w file   指定檔案是否可寫
test -x file   指定檔案是否可操作


測試:
[[email protected]_0_16_centos ~]# test -d /home/ee;echo $?
1
[
[email protected]
_0_16_centos ~]# test -d /home/es;echo $?
0
[[email protected]_0_16_centos ~]#

也可以這樣寫
[[email protected]_0_16_centos es]# [ -d tody ];echo $?
0
[[email protected]_0_16_centos es]# [ -d tody1 ];echo $?
1
[[email protected]_0_16_centos es]#


例子:連結檔案
在當前目錄中建立一個連結目錄
ln -s /etc/services .
執行結果:
lrwxrwxrwx 1 root root   13 May 11 14:27 services

-> /etc/services


[[email protected]_0_16_centos es]# [ -L services ];echo $?
0
[[email protected]_0_16_centos es]# [ -L services1 ];echo $?
1
[[email protected]_0_16_centos es]# [ -L tody ];echo $?
1
[[email protected]_0_16_centos es]#


測試許可權
[[email protected]_0_16_centos es]# [ -r read.sh ];echo $?
0

[[email protected]_0_16_centos es]# touch shell5
[[email protected]_0_16_centos es]# [ -x shell5 ];echo $?
1
[[email protected]_0_16_centos es]#


那麼這個有什麼用呢?
可以通過上面的方式判斷是否已經存在檔案




5.多重條件校驗
條件1 -a 條件2    邏輯與   兩者都成立,則為真
條件1 -o 條件2    邏輯或   兩者其一成立,則為真
! 條件   邏輯非 取反


[[email protected]_0_16_centos es]# [ 3 -gt 2 -a 2 -gt 3

];echo $?
1
[[email protected]_0_16_centos es]# [ 3 -gt 2 -o 2 -gt 3

];echo $?
0
[[email protected]_0_16_centos es]#


[[email protected]_0_16_centos es]# [ -d /home/es -a -d

/home/ee ];echo $?
1
[[email protected]_0_16_centos es]# [ -d /home/es -o -d

/home/ee ];echo $?
0
[[email protected]_0_16_centos es]#


例子:
[[email protected]_0_16_centos es]# [ -d /home/es -a -d

/home/ee ]&&echo $?
[[email protected]_0_16_centos es]#
需要區別-a,-o和&&,;的區別





















相關推薦

shell程式設計條件判斷

bash中的條件判斷 條件測試型別     1、整數測試     2、字元測試     3、檔案測試 條件測試表達式     [ ex

Linux Shell程式設計 條件判斷語法

if條件判斷語句 單分支 if 條件語句 語法格式: if [條件判斷式];then 程式 fi 或者 if [條件判斷式] then 程式 fi 在使用單分支 if 條件查詢時需要注意幾點: if 語句使用 fi 結尾,和一般語言使用大括號結尾不同。 [

Shell程式設計-條件判斷

1.test語法 test expression或[ expression ][ expression ]更加常用2例子1)判斷字串是否相等test str1==str2例子:[[email protected]_0_16_centos ~]# test '12'=

Shell條件判斷語法與判斷條件

expression 表達式 字符串 linux 影響 一,簡介Shell各種判斷結構和運算符的用法是shell編程的基礎,了解shell的判斷、運算符和一些退出狀態對後面的學習有很重要的影響。shell有一個內部命令test經常用於對判斷語句進行測試一種或幾種狀態的條件是否成立二. 判斷

shell test條件判斷

strong 終端 light 目錄 存在 符號 文件描述 -c 描述 test 條件判斷 # 符號 [ ] 等同 test命令 test 10 -lt 5 # 判斷大小 echo $? # 查看上句test命令返回狀態 # 結果0

[shell指令碼]條件判斷和迴圈

基本語法 1、條件判斷 if [ condition1 ];then command 1 elif [ condition2 ];then command 2 else command3 fi 注意: (1)if .. fi標誌著判斷語句的開始和結束; (2)[ ]是條件判斷符,

(一)Shell條件判斷符及特殊變數

目錄 1 shell的條件判斷 簡單條件判斷 條件測試通常有如下3中形式: 語法格式1:test<測試表達式> 語法格式2:[ <測試表達式> ] 語法格式3:

Linux程式設計 23 shell程式設計(結構化條件判斷 命令if -then , if-then ... elif-then ...else,if test)

一.概述   在上一篇裡講到了shell指令碼,shell按照命令在指令碼中出現的順序依次進行處理,對於順序操作已經足夠了,但許多程式要求對shell指令碼中的命令加入一些邏輯流程控制,這樣的命令通常叫做 結構化命令。   1.1 使用if - then語句 --最基本的結構化就是if -then語句,格式

linux學習筆記之shell程式設計(二)條件判斷

條件判斷 檔案存在與否 -d 是否存在為目錄 -e 是否是檔案 -f 是否存在為檔案 [-d /root && echo "yes" || echo "no"] -檔案讀寫執行許可權#### -r 讀許可權 -w 寫

Linux shell程式設計中的判斷條件

-b file            若檔案存在且是一個塊特殊檔案,則為真 -c file            若檔案存在且是一個字元特殊檔案,則為真 -d file            若檔案存在且是一個目錄,則為真 -e file            若檔案存在,

Linux shell程式設計——if條件判斷

if 語句格式if  條件then Commandelse Commandfi                              別忘了這個結尾If語句忘了結尾fitest.sh: line 14: syntax error: unexpected end of fi

linux基礎之shell程式設計(2)-條件判斷,算數運算,測試

bash中如果實現條件判斷? 條件測試型別 整數測試 字元測試 檔案測試 條件測試的表示式 有三種 [ expression ] --方括號與表示式之間一定要有一個空格 [[ expression ]] test exp

shell程式設計條件判斷與流程控制)學習筆記

流程控制語句 一、條件判斷式 二、單分支if語句 三、雙分支if語句 四、多分支if語句 五、case語句 六、for迴圈 七、while迴圈和until迴圈 一  條件判斷式 1 按照檔案型別進行判斷 2 按照檔案許可權進行判斷 3 兩個檔案之間進行比較

shell 編程 -- 條件判斷

如果 -s size 裏的 字符串 ron -o 普通 修改 1.按照文件類型進行判斷(常用的)-b 判斷該文件是否存在-d 判斷是否存在,並且是否為目錄(是目錄就為真)-e 判斷該文件是否存在(存在為真)-f 判斷文件是否存在,並且是否為普通文件(是普通文件為真)-L 判

linux--shell編程(三)條件判斷及算術運算

寫一個腳本 head bsp 如何 cut inittab 引用 有用 color 練習:寫一個腳本判斷當前系統上是否有用戶的默認shell為bash; 如果有,就顯示有多少個這類用戶;否則,就顯示沒有這類用戶; #!/bin/bash # grep ‘\<ba

shell 編程if條件判斷與if 真假判斷

if 真假 與 條件判斷if條件判斷與if真假判斷 目錄: 1.正確寫法 2.錯誤寫法 3.總結一、正確寫法 在編寫shell腳本時,為簡化代碼的行號及結構的簡約型,通常將命令執行結果和判斷通過一條語句進行編寫(在C語言編寫程序時,經常遇到此種寫法),如:[[email protected]

Shell腳本的條件判斷與比較

shell腳本 中一 不能 rst ali 大小 表達式 操作符 內容 條件測試常用的語法:語法格式說明[ <測試表達式> ][]的邊界和內容之間至少有一個空格[[ <測試表達式> ]][[]的邊界和內容之間至少有一個空格註意:&&

shell 條件判斷

may 字符串相同 use after 文件的 suid last 描述 non-zero 1.字符串判斷 str1 = str2 當兩個串有相同內容、長度時為真 str1 != str2 當串str1和str2不等時為真 -n str1

if 條件判斷判斷總結---shell腳本

選項 但是 語言 echo ng2 other 朋友 系統 solar 本文主要介紹了Shell腳本IF條件判斷和判斷條件總結,本文先是給出了IF條件判斷的語法,然後給出了常用的判斷條件總結,需要的朋友可以參考下。 前言: 無論什麽編程語言都離不開條件判斷。S

[Shell]條件判斷與流程控制:if, case, for, while, until

設備 重啟 硬鏈接 HA rpm 套接字 字符串 regular 符號 【條件判斷】 1. 按文件類型進行判斷   -b 文件 判斷該文件是否存在,並且為塊設備文件(是塊設備文件為真)   -c 文件 判斷該文件是否存在,並且為字符設備文件(是字符設