对应《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: