對應《Linux命令行與shell腳本編程大全》 -- 章節12.4.3
test1: -d file :檢查目錄
#!/bin/bash
dir="/home//shell/dir"
if [ -d $dir ]
then
echo "dir is exist"
cd $dir
ls
else
echo "dir is not exist!"
fi
結果:
dir is exist
one two
test2:-e file :檢查對象是否存在
#!/bin/bash
dir="/home//shell/dir"
file="one"
if [ -e $dir ]
then #dir exist
echo "OK!$dir is exist!"
echo "Now start check the file:$file"
if [ -e $dir/$file ]
then #file exist
echo "OK!$file is exist!"
date >> $dir/$file
cat $dir/$file
else #file not exist
echo "Sorry!$file is not exist!"
fi
else #dir not exist
echo "Sorry!$dir is not exist!"
fi
結果:
OK!/home//shell/dir is exist!
Now start check the file:one
OK!one is exist!
test3: