shell 判斷檔案 目錄是否為空

2021-05-22 16:34:14 字數 635 閱讀 3260

剛開始寫shell,很多都不會在網上東找找西找找.

#判斷檔案目錄是否為空

第一種:

emptydir.sh

#!/bin/sh

directory=$1

if [ "`ls -a $directory`" = "" ]; then

echo "$directory is indeed empty"

else

echo "$directory is not empty"

fi第二種:

count.sh

#!/bin/sh

count=`ls $*|wc -w`

if [ "$count" > "0" ];

then

echo "file size $count"

else

echo "empty!"

fi#目錄是否存在

ifmkdir.sh

#!/bin/sh

dir="test"

if [ ! -d $dir ]; then

echo "$dir not exists"

mkdir "$dir"

else

echo "$dir exists!"

fi

shell判斷檔案是否為空

if s filename 如果檔案存在且為空,s代表存在不為空,將他取反 then okfi s 表示檔案是否存在並且是否為非空 判斷檔案的引數如下 e 檔案存在 f file 是乙個 regular 檔案 不是目錄或者裝置檔案 s 檔案長度不為 0 d 檔案是個目錄 b 檔案是個塊裝置 軟盤,c...

C 判斷檔案是否為空

在 頭部引入 system.io 命名空間 第一種方法 using filestream fs new filestream c a.txt filemode.open 第二種方法 fileinfo fi new fileinfo c a.txt if fi.length 0 相比之下,第二種方法更...

shell判斷目錄 檔案是否存在

編寫指令碼 vim a.sh bin bash a ls grep zabbix b usr local src zabbix if d a then touch b echo b已建立 else echo a fi具體引數如下 根據自己需要選擇相對應的引數 e 判斷 a是否存在 d 判斷 a是否存...