shell腳本怎樣判斷文件是否存在
shell腳本判斷文件是否存在
在進(jìn)行l(wèi)inux系統(tǒng)相關(guān)應(yīng)用程序開發(fā)時(shí),少不了要書寫一些shell腳本,有時(shí)候要用到判斷文件或者目錄是否存在的腳本,本文筆者做一下筆記,已備后查。
shell判斷文件是否存在的腳本如下:
//[ 與 ] 的前后必須有空格符 if [ -f /path/file.ext ] then echo "The file exist" else echo "The file doesn't exist" fi //判斷某鏈接是否存在 if [ -L /path/link ] then echo "The link exist" else echo "The link doesn't exist" fi
其實(shí)shell對(duì)于文件冊(cè)測(cè)試有好幾種選項(xiàng)開關(guān)
現(xiàn)在例舉如下:
表達(dá)式 | 測(cè)試含義 |
---|---|
-a filepath | file exists. all files type |
-b filepath | file exists and is a block special file. |
-c filepath | file exists and is a character special file. |
-d filepath | file exists and is a directory. |
-e filepath | file exists (等同于 -a). |
-f filepath | file exists and is a regular file. |
-g filepath | file exists and has its setgid(2) bit set. |
-G filepath | file exists and has the same group ID as this process. |
-k filepath | file exists and has its sticky bit set. |
-L filepath | file exists and is a symbolic link. |
-n filepath | string length is not zero. |
-o filepath | Named option is set on. |
-O filepath | file exists and is owned by the user ID of this process. |
-p filepath | file exists and is a first in, first out (FIFO) special file ornamed pipe. |
-r filepath | file exists and is readable by the current process. |
-s filepath | file exists and has a size greater than zero. |
-S filepath | file exists and is a socket. |
-t filepath | file descriptor number fildes is open and associated with aterminal device. |
-u filepath | file exists and has its setuid(2) bit set. |
-w filepath | file exists and is writable by the current process. |
-x filepath | file exists and is executable by the current process. |
shell腳本之文件是否存在、權(quán)限校驗(yàn)
判斷目錄是否存在
#判斷目錄是否存在,判斷非加!號(hào), [ ! -d '/home' ] if [ -d '/home' ] then ?? ?echo "目錄/home存在==========" else ?? ?echo "目錄/home不存在=========" fi
判斷文件是否存在
#判斷文件是否存在 if [ -f '/home/docker.log' ] then ?? ?echo "文件/home/docker.log存在=============" else ?? ?echo "文件/home/docker.log不存在===========" fi
判斷目錄/文件是否存在
#判斷文件是否存在,目錄或文件存在都成立 if [ -e '/home' ] then ?? ?echo "/home存在==============" else ?? ?echo "/home不存在============" fi
判斷文件權(quán)限
#檢測(cè)文件是否可讀 -r ,可寫 -w ,可執(zhí)行 -x if [ -r '/home/script/file.log' ] then ?? ?echo "文件/home/script/file.log存在并可讀==============" else ?? ?echo "目錄/home/script/file.log不存在或不可讀==================" fi
判斷文件是否屬于當(dāng)前用戶
#檢測(cè)文件是否屬于當(dāng)前用戶 file_path=/home/script/file.log if [ -O $file_path ] then ?? ?echo "文件$file_path屬于當(dāng)前用戶=================" else ?? ?echo "文件$file_path不屬于當(dāng)前用戶===============" fi
判斷文件是否與當(dāng)前用戶相同用戶組
#檢測(cè)文件是否存在,并且默認(rèn)組與當(dāng)前用戶相同 file_path=/home/script/file.log if [ -G $file_path ] then ?? ?echo "文件$file_path所屬組與當(dāng)前用戶相同=================" else ?? ?echo "文件$file_path所屬組與當(dāng)前用戶不相同================" fi
比較文件之間是否為新建
#檢測(cè)文件file1是否比file2新 file1=/home/script/file.log file2=/home/script/file_1.log if [ $file1 -nt $file2 ] then ?? ?echo "文件$file1比文件$file2新==============" fi if [ $file1 -ot $file2 ] then ?? ?echo "文件$file1比文件$file2舊===============" fi
復(fù)合條件判斷文件
#判斷既是文件 又 可讀 ,用 && ,或用 || file=/home/script/file.log if [ -f $file ] && [ -r $file ] then ? ? ? ? echo "文件$file是文件,并且可讀=============" fi
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Shell腳本命令行批處理bash?sh?cmd?bat函數(shù)極簡(jiǎn)學(xué)法
這篇文章主要為大家介紹了Shell腳本中文英文多語言國際化和命令行批處理(bash?sh?cmd?bat)中定義函數(shù)的簡(jiǎn)單寫法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-09-09linux實(shí)現(xiàn)解壓.tar.gz文件的示例詳解
這篇文章主要為大家詳細(xì)介紹了linux實(shí)現(xiàn)解壓.tar.gz文件的相關(guān)知識(shí),文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-08-08shell實(shí)現(xiàn)Fisher–Yates shuffle洗牌算法介紹
大家好,本篇文章主要講的是shell實(shí)現(xiàn)Fisher–Yates shuffle洗牌算法介紹,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下哦2021-11-11shell腳本中echo顯示內(nèi)容帶顏色的實(shí)現(xiàn)方法
這篇文章主要介紹了shell腳本中通過echo顯示帶顏色內(nèi)容的實(shí)現(xiàn)方法,需要的朋友可以參考下2013-11-11shell 創(chuàng)建子進(jìn)程及并行延時(shí)執(zhí)行命令方法
本文主要介紹了shell 創(chuàng)建子進(jìn)程及并行延時(shí)執(zhí)行命令方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01通過短信發(fā)送LOG歸類號(hào)碼發(fā)送情況的shell腳本
通過短信發(fā)送LOG歸類號(hào)碼發(fā)送情況的shell腳本,有需要的朋友可以參考下2013-02-02