国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Linux > 正文

使用Bash Shell檢查文件是否存在的方法

2019-10-26 18:49:55
字體:
供稿:網(wǎng)友

前言

大家在工作的時(shí)候可能經(jīng)常會遇到這樣的需求,在類Unix系統(tǒng)的Bash環(huán)境下,怎樣檢查文件是否存在呢?既然有需求,當(dāng)然就有解決的辦法了,Shell中的test命令,可以用來檢測文件的類型或者比較數(shù)值是否相等,該命令也能用來檢查文件是否存在。

可以用如下的命令來進(jìn)行檢查:

test -e filename[ -e filename ] test -f filename[ -f filename ]

下面的命令,則使用Shell的條件表達(dá)式,判斷/etc/hosts文件是否存在:

[ -f /etc/hosts ] && echo "Found" || echo "Not found"

該組合命令會輸出以下內(nèi)容:

Found

更常見的用法,則是將test命令放置在if..else..fi條件判斷的條件表達(dá)式,然后在其中寫上不同的分支邏輯

#!/bin/bashfile="/etc/hosts"if [ -f "$file" ]then echo "$file found."else echo "$file not found."fi

檢測文件屬性的相關(guān)操作符

如果文件存在,并且具有相應(yīng)的屬性,如下的操作符都會返回true:

  -b FILE    FILE exists and is block special  -c FILE    FILE exists and is character special  -d FILE    FILE exists and is a directory  -e FILE    FILE exists  -f FILE    FILE exists and is a regular file  -g FILE    FILE exists and is set-group-ID  -G FILE    FILE exists and is owned by the effective group ID  -h FILE    FILE exists and is a symbolic link (same as -L)  -k FILE    FILE exists and has its sticky bit set  -L FILE    FILE exists and is a symbolic link (same as -h)  -O FILE    FILE exists and is owned by the effective user ID  -p FILE    FILE exists and is a named pipe  -r FILE    FILE exists and read permission is granted  -s FILE    FILE exists and has a size greater than zero  -S FILE    FILE exists and is a socket  -t FD file descriptor FD is opened on a terminal  -u FILE    FILE exists and its set-user-ID bit is set  -w FILE    FILE exists and write permission is granted  -x FILE    FILE exists and execute (or search) permission is granted

以上命令,從man test復(fù)制而來。

使用上述符號的方法一模一樣:

if [ operator FileName ]then  echo "FileName - Found, take some action here"else echo "FileName - Not found, take some action here"fi

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可留言交流。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 玛纳斯县| 茂名市| 鹤壁市| 拜城县| 英吉沙县| 巴里| 闵行区| 本溪| 左贡县| 河北区| 衡水市| 镇远县| 康马县| 绍兴县| 安远县| 周至县| 社旗县| 都江堰市| 淅川县| 石城县| 许昌市| 临沧市| 锡林郭勒盟| 乌兰浩特市| 休宁县| 钦州市| 临湘市| 东平县| 苏尼特右旗| 阳城县| 旬邑县| 广饶县| 额尔古纳市| 滦南县| 栾川县| 白河县| 资兴市| 平阳县| 维西| 长沙市| 临澧县|