欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

shell腳本怎樣判斷文件是否存在

 更新時(shí)間:2023年06月06日 14:54:55   作者:大飛飛魚  
這篇文章主要介紹了shell腳本怎樣判斷文件是否存在問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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 filepathfile exists. all files type
-b filepathfile exists and is a block special file.
-c filepathfile exists and is a character special file.
-d filepathfile exists and is a directory.
-e filepathfile exists (等同于 -a).
-f filepathfile exists and is a regular file.
-g filepathfile exists and has its setgid(2) bit set.
-G filepathfile exists and has the same group ID as this process.
-k filepathfile exists and has its sticky bit set.
-L filepathfile exists and is a symbolic link.
-n filepathstring length is not zero.
-o filepathNamed option is set on.
-O filepathfile exists and is owned by the user ID of this process.
-p filepathfile exists and is a first in, first out (FIFO) special file ornamed pipe.
-r filepathfile exists and is readable by the current process.
-s filepathfile exists and has a size greater than zero.
-S filepathfile exists and is a socket.
-t filepathfile descriptor number fildes is open and associated with aterminal device.
-u filepathfile exists and has its setuid(2) bit set.
-w filepathfile exists and is writable by the current process.
-x filepathfile 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)文章

最新評(píng)論