shell檢測某個(gè)文件/文件夾是否存在詳細(xì)實(shí)例
1、shell檢測某一文件是否存在
當(dāng)你在shell中需要檢查一個(gè)文件是否存在時(shí),通常需要使用到文件操作符-e和-f。第一個(gè)-e用來檢查文件是否存在,而不管文件類型。第二個(gè)-f僅僅用來檢查文件是常規(guī)文件(不是目錄或設(shè)備)時(shí)返回true。
FILE=/etc/resolv.conf if test -f "$FILE"; then echo "$FILE exist" fi FILE=/etc/resolv.conf if [ -f "$FILE" ]; then echo "$FILE exist" fi FILE=/etc/resolv.conf if [[ -f "$FILE" ]]; then echo "$FILE exist" fi
2、shell檢測某一目錄是否存在
Linux系統(tǒng)中運(yùn)算符-d允許你測試一個(gè)文件是否時(shí)目錄。
例如檢查/etc/docker目錄是否存在,你可以使用如下腳本:
FILE=/etc/docker if [ -d "$FILE" ]; then echo "$FILE is a directory" fi [ -d /etc/docker ] && echo "$FILE is a directory"
3、檢查文件是否不存在
和其他語言相似,test表達(dá)式允許使用!(感嘆號)做邏輯not運(yùn)算,示例如下:
FILE=/etc/docker if [ ! -f "$FILE" ]; then echo "$FILE does not exist" fi [ ! -f /etc/docker ] && echo "$FILE does not exist"
4、檢查是否存在多個(gè)文件
不使用復(fù)雜的嵌套if/else構(gòu)造,您可以使用-a(或帶[[的&&預(yù)算符)來測試是否存在多個(gè)文件,示例如下:
if [ -f /etc/resolv.conf -a -f /etc/hosts ]; then echo "Both files exist." fi if [[ -f /etc/resolv.conf && -f /etc/hosts ]]; then echo "Both files exist." fi
5、應(yīng)用實(shí)例
只跑一遍diff的時(shí)候,可能因?yàn)榄h(huán)境不穩(wěn)定導(dǎo)致diff,因此循環(huán)跑某個(gè)場景的diff query。具體實(shí)現(xiàn)如下,get_diff.py結(jié)合具體的場景定,-input_file ${result_dir}/${query_file}_${head} -output_file ${result_dir}/${query_file}_${behind}這兩個(gè)文件一樣。
base="501" exp="506" iter_num=2 query_name="model_iter_v2" data_dir=./data_${query_name} result_dir=./result_${query_name} if [ ! -d "${result_dir}" ]; then mkdir ${result_dir} fi if [ -d "${result_dir}" ]; then rm -rf ${result_dir}/* fi for var in ${data_dir}/*; do query_file=${var##*/} cp ${data_dir}/${query_file} ${result_dir}/${query_file}_1 head=1 while [[ ${head} -lt ${iter_num} ]] do behind=$((${head} + 1)) echo ${query_file}_${head} echo ${query_file}_${behind} python get_diff.py -input_file ${result_dir}/${query_file}_${head} -b ${base} -e ${exp} -output_file ${result_dir}/${query_file}_${behind} > ${query_file}.log sort -t" " -k2,2nr ${result_dir}/${query_file}_${behind}_result > ${result_dir}/${query_file}_${behind} rm ${result_dir}/${query_file}_${behind}_result if [ ${behind} -eq ${iter_num} ]; then cp ${result_dir}/${query_file}_${behind} ./${query_file}_diff fi let head++ done done
總結(jié)
到此這篇關(guān)于shell檢測某個(gè)文件/文件夾是否存在的文章就介紹到這了,更多相關(guān)shell檢測文件夾是否存在內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Shell腳本判斷進(jìn)程是否存在的實(shí)現(xiàn)示例
本文主要介紹了Shell腳本判斷進(jìn)程是否存在的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Linux中shell腳本獲取當(dāng)前工作目錄的方法
今天小編就為大家分享一篇Linux中shell腳本獲取當(dāng)前工作目錄的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06學(xué)習(xí)shell腳本之前的基礎(chǔ)知識[圖文]
在學(xué)習(xí)shell腳本之前,需要你了解很多關(guān)于shell的知識,這些知識是編寫shell腳本的基礎(chǔ),所以希望你能夠熟練的掌握2013-03-03awk實(shí)現(xiàn)Left、join查詢、去除重復(fù)值以及局部變量講解例子
這篇文章主要介紹了awk實(shí)現(xiàn)Left、join查詢、去除重復(fù)值以及局部變量講解例子,awk的高級使用技巧,需要的朋友可以參考下2014-07-07linux上搭建solr的實(shí)現(xiàn)方法(用jetty部署)
下面小編就為大家分享一篇linux上搭建solr的實(shí)現(xiàn)方法(用jetty部署),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12shell命令while循環(huán)中使用sleep命令代碼示例
這篇文章主要介紹了shell命令while循環(huán)中使用sleep命令代碼示例,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02