1. 程式人生 > >linux shell測試檔案狀態

linux shell測試檔案狀態

寫指令碼是,有時要判斷字串是否相等,可能還要檢查檔案狀態或是數字測試。

Test命令用於測試字串,檔案狀態和數字。

對檔案、字串和數字使用test命令

對數字和字串使用expr命令

 expr命令測試和執行數值輸出。使用最後退出狀態命令$?可測知test和expr,二者均以0表示正確,1表示返回錯誤。

test一般有兩種格式,即:

test condition 或 [ condition ]

使用方括號時,要注意在條件兩邊加上空格。

檔案狀態測試

-d 目錄        -s 檔案長度大於0、非空

-f 正規檔案   -w 可寫

-L 符號連線  -u 檔案有suid位設定

-r 可讀         -x 可執行 

example:

#!/bin/sh
#this is a shell command about test the file is a dir
echo "begin test the ~/input"
test -d ~/input
echo $? 

測試時使用邏輯操作符

測試檔案狀態是否為OK,但是有時要比較兩個檔案狀態。shell提供三種邏輯操作完成此功能。

-a 邏輯與,操作符兩邊均為真,結果為真,否則為假。

-o 邏輯或,操作符兩邊一邊為真,結果為真,否則為假。

example:

測試兩個檔案是否均可讀

#!/bin/sh
#this is a command to test -a 
echo "begin test"
ls -l ~/test/
test -w file1 -a -w file2
echo $?
echo "end test"

result:

begin test
total 8
-rw-rw-r-- 1 hadoop hadoop 0 Feb  6 02:45 file1
-rw-rw-r-- 1 hadoop hadoop 0 Feb  6 02:45 file2
0
end test

example:

測試其中的一個檔案是否可執行

#!/bin/sh
#this is a command to test -o
echo "test begin"
ls -l 
[ -x file1 -o -x file2 ]
echo $?
echo "test end" 

result:

test begin
total 8
-rwxrw-r-- 1 hadoop hadoop 0 Feb  6 02:45 file1
-rw-rw-r-- 1 hadoop hadoop 0 Feb  6 02:45 file2
0
test end

example:

測試檔案file1是否可讀、可寫、可執行

#!/bin/sh
#this is a command to test a file "rwx"
echo "begin test"
ls -l file1
[ -r file1 -a -w file1 -a -x file1 ]
echo $?
echo "end test" 

result:

begin test
-rwxrw-r-- 1 hadoop hadoop 0 Feb  6 02:45 file1
0
end test

 字串測試

字串測試時錯誤捕獲很重要的一部分,特別在測試使用者輸入或比較變數時尤為重要。

test "string"

test string_operator "string"

test "string" string_operator "string"

[ string_operator string ]

[ string string_operator string ]

這裡,string_operator可為

= 兩個字串相等

!= 兩個字串不等

-z 空串

-n 非空串

給變數editor賦值為

[[email protected] test]$ editor=vi
[[email protected] test]$ echo $editor
vi
 [[email protected] test]$ editor="vi test"
[[email protected] test]$ echo $editor
vi test

 注意:在變數和字串之間的等號不能有空格

example:

#!/bin/sh
#this is a command to test string
editor=vi
echo $editor 
echo "test begin"
echo "is the string null?"
[ -z $editor ]
echo $?
echo "is the string vi?"
[ $editor = "vi" ]
echo $?
echo $editor
tape="/dev/rmt0"
tape2="/dev/rmt1"
echo "tape:$tape"
echo "tape2:$tape2"
echo "is the tape = tape2?"
[ "$tape"="$tape2" ]
echo $?
echo "is the tape != tape2?"
[ "$tape"!="$tape2" ]
echo $?
echo "end test"

result:

vi
test begin
is the string null?
1
is the string vi?
0
vi
tape:/dev/rmt0
tape2:/dev/rmt1
is the tape = tape2?
0
is the tape != tape2?
0
end test

測試數值

"number"numeric_operator"number"

或者

[ "number"numeric_operator"number" ]

number_operator

-eq 數值相等

-ne 數值不相等

-gt 第一個數大於第二個數

-lt 第一個數小於第二個數

-le 第一個數小於等於第二個數

-ge 第一個數大於等於第二個數

example:

#!/bin/sh
#this is a command to test number
echo "test begin"
number=130
echo "is number eq 130?"
[ "$number" -eq "130" ]
echo $?
echo "is number eq 100?"
[ "$number" -eq "100" ]
echo $?
echo "is number gt 100?"
[ "$number" -gt "100" ]
echo $?
source_count=13
dest_count=15
[ "$dest_count" -gt "$source_count" ]
echo "is 15 gt 13 "
echo $?
[ "990" -le "995" ]
echo "is 990 le 995?"
echo $?
[ "990" -le "995" -a "123" -gt "33" ]
echo "is 990 le 995 and 123 gt 33?"
echo $?
echo "end test" 

result:

test begin
is number eq 130?
0
is number eq 100?
1
is number gt 100?
0
is 13 gt 15 
0
is 990 le 995?
0
is 990 le 995 and 123 gt 33?
0
end test

相關推薦

linux shell測試檔案狀態

寫指令碼是,有時要判斷字串是否相等,可能還要檢查檔案狀態或是數字測試。 Test命令用於測試字串,檔案狀態和數字。 對檔案、字串和數字使用test命令 對數字和字串使用expr命令  expr命令測試和執行數值輸出。使用最後退出狀態命令$?可測知test和expr,二者均以0表示正確,1表示返回錯誤。

linux shell檔案操作

查詢字串所在行 : grep -n “待查詢字串” “檔名” 顯示指定行資訊:sed -n '1,5p' “指定檔案”  表示顯示指定檔案第一至五行的資訊 ----------------------------------------------------------

Linux Shell 文字檔案處理

通過sort/uniq獲取檔案內容的交集、合集和不同之處:假設有a、b兩個文字檔案,檔案本身已經去除了重複內容。下面是效率最高的方法,可以處理任何體積的檔案,甚至幾個G的檔案。(Sort對記憶體沒有要求,但也許你需要用 -T 引數。)可以試著比較一下,你可以看看如果用Ja

Linux shell讀取檔案

在Linux中有很多方法逐行讀取一個檔案的方法,其中最常用的就是下面的腳本里的方法,而且是效率最高,使用最多的方法。為了給大家一個直觀的感受,我們將通過生成一個大的檔案的方式來檢驗各種方法的執行效率。 方法1:while迴圈中執行效率最高,最常用的方法。   funct

Linux shell判斷檔案或目錄是否存在

#!/bin/sh myPath="/var/log/httpd/" myFile="/var /log/httpd/access.log" #這裡的-x 引數判斷$myPath是否存在並且是否具有可執行許可權 if [ ! -x "$myPath"]; then   mkdi

linux shell 獲取檔案中包含特殊字元的第一行行數 和最後一行行數

獲取包含“2018-01-02”的第一行行號cat 1.txt | egrep "2018-01-02" -nR | awk -F ":" '{print $1}'| awk '(NR==1){print $0}'獲取包含“2018-01-02”的最後一行行號cat 1.t

Linux shell程式設計:狀態變數

四大特殊狀態變數:$?、 $$、 $!、 $_ $?的作用是:獲取執行上一個指令的執行狀態返回值,返回0表示上一個命令或者程式執行成功,返回的值為非0則表示上一個命令執行失敗。 $$的作用是:獲取當前執行的shell指令碼的程序號PID。 $!的作用是:獲取上一個後臺工

linux shell 判斷 檔案整數 存在、大小、空、等

轉自:http://hi.baidu.com/nbye2000/item/371624271fdc1dd3a417b642 *      檔案測試操作符    如果下面的條件成立將會返回真.    -e    檔案存在    -a    檔案存在,這個選項的效果與-e相同.

Linux-Shell檔案操作

  學些了Linux Shell命令列使用,對於檔案的操作除了記下來,但還沒有找到相應方法掌握,常常混亂。資料庫有“增刪改查”,因而同樣按理來分類檔案操作方法,較為清晰。同時作為檔案,有許可權、大小等檔案屬性,因而加入“看”來看檔案屬性。這樣就變為“增刪看改查”

Linux Shell指令碼檔案的判斷、中文符號及其字串入參解析

1、shell指令碼中判斷檔案是否存在 if [ -f  "$var" ] then...... 2、shell指令碼中判斷字串為空  if [ -z "$str"] then...... 3、shell指令碼中判斷字串不為空 if[ "$str"] then..... 4、字串入參的注意事項      

Linux Shell編程之測試條件

Linux Shell編程的簡單操作執行命令並利用命令狀態返回值來判斷某些狀態echo $? 0代表成功,非0代表不成功測試表達式數值比較-eq 等於返回值是1,說明2不等於3-ne 不等於返回值是0 說明2不等於3還可以直接使用中括號,而不需要輸入test關鍵字返回值為1 說明2等於2-gt 左側是否大於右

Linux shell計算兩個檔案的交集 並集和差集

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

Linux——x-shell上傳檔案Linux和下載檔案到本地

一.下載安裝x-shell 百度上有很多xshell破解版,大家可以自行搜尋下載並安裝到自選目錄下。 二.連線伺服器 開啟x-shell選擇新建會話 當然,如果直接有檔案就可以直接點連線 點選新建之後會進入如下介面 輸入需要連線的主機IP。 我這裡用的是

Linux Shell 指令碼讀取配置檔案

一、應用場景 為了靈活應用shell指令碼,適當的加入配置檔案,對於後期的維護和優化會有很大幫助。例如指令碼中使用的檔案/檔案路徑,都可以通過讀取配置檔案完成。 配置檔案 filename=boomlee 指令碼檔案 #!/bin/bash workdir=$(cd $(di

linux shell 讀取配置檔案

隨著linux接觸的越來越多,我們難免需要從一些配置檔案中進行讀取配置引數,linux中shell屬於指令碼型語言,讀取時沒有其它語言方便,特將用過的一種方式分享給大家 實戰程式碼: $ more a.txt name=hello world age=22 ip=192.168.

Linux Shell基礎 環境變數配置檔案

source命令:使環境變數配置檔案強制生效 source 命令會強制執行指令碼中的全部命令,而忽略指令碼檔案的許可權。該命令主要用於讓重新配置的環境變數配置檔案強制生效。source 命令格式如下: [[email protected] ~]# source 配置檔案 或 [[ema

LINUX Shell 下求兩個檔案交集和差集的辦法

轉載自https://blog.csdn.net/autofei/article/details/6579320 假設兩個檔案FILE1和FILE2用集合A和B表示,FILE1內容如下: a b c e d a FILE2內容如下:  c d

Linux shell指令碼013(生成配置檔案

#!/bin/bash #生成配置檔案auto-ops.conf的中介軟體配置項,要求auto-ops.conf檔案已經存在 #usage ./scriptname apahce 1.1.1.1 tomcat 1.1.1.2 jboss 1.1.1.3 ###引數規範性校驗 ###

Linux Shell環境下用jq命令處理json檔案

安裝 CentOS下可以直接通過yum安裝: yum install jq -y 使用方法 假設有一個名為test.json的檔案,內容如下: { "Summary": { "Version": "1.0", "Comment

Shell指令碼定時監控Linux下的程序狀態並自動重啟

以mysql為例,先上shell指令碼,如下: #!/bin/bash ps -ef | grep mysqld | grep -v grep if [ $? -ne 0 ] then echo “start process…” /etc/rc.d/init.d