shell腳本怎樣判斷文件是否存在
更新時間:2023年06月06日 14:54:55 作者:大飛飛魚
這篇文章主要介紹了shell腳本怎樣判斷文件是否存在問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
shell腳本判斷文件是否存在
在進行l(wèi)inux系統(tǒng)相關(guān)應(yīng)用程序開發(fā)時,少不了要書寫一些shell腳本,有時候要用到判斷文件或者目錄是否存在的腳本,本文筆者做一下筆記,已備后查。
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其實shell對于文件冊測試有好幾種選項開關(guān)
現(xiàn)在例舉如下:
| 表達式 | 測試含義 |
|---|---|
| -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)限校驗
判斷目錄是否存在
#判斷目錄是否存在,判斷非加!號, [ ! -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)限
#檢測文件是否可讀 -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)前用戶
#檢測文件是否屬于當(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)前用戶相同用戶組
#檢測文件是否存在,并且默認(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
比較文件之間是否為新建
#檢測文件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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Linux應(yīng)用調(diào)試之strace命令詳解
strace常用來跟蹤進程執(zhí)行時的系統(tǒng)調(diào)用和所接收的信號。下面通過本文給大家分享Linux應(yīng)用調(diào)試之strace命令,需要的朋友參考下吧2017-12-12
詳解shell 遍歷文件夾內(nèi)所有文件并打印絕對路徑
本篇文章主要介紹了shell 遍歷文件夾內(nèi)所有文件并打印絕對路徑,具有一定的參考價值,有興趣的可以了解一下。2017-01-01
Shell腳本一鍵安裝Nginx服務(wù)自定義Nginx版本
這篇文章主要為大家介紹了Shell腳本一鍵安裝Nginx服務(wù),用戶可自定義Nginx版本的腳本示例,有需要的朋友可以借鑒參考下,希望能夠參考下2022-03-03

