shell 判斷檔案/目錄是否為空白 分類: Linux 2010-05-29 17:06 3512人閱讀 評論(0) 收藏 舉報 shell file
剛開始寫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
-----------------------------------------------------------