-e比较可用于文件和目录。要确定指定对象为文件,必须用-f比较。
$ cat test13.sh
#!/bin/bash
# Check if either a directory or file exists
#
item_name=$HOME
echo
echo "The item being checked: $item_name"
echo
#
if [ -e $item_name ]
then #Item does exist
echo "The item, $item_name, does exist."
echo "But is it a file?"
echo
#
if [ -f $item_name ]
then #Item is a file
echo "Yes, $item_name is a file."
#
else #Item is not a file
echo "No, $item_name is not a file."
fi
#
else #Item does not exist
echo "The item, $item_name, does not exist."
echo "Nothing to update"
fi
#
$ ./test13.sh
The item being checked: /home/Christine
The item, /home/Christine, does exist.
But is it a file?
No, /home/Christine is not a file.
$
这一小段脚本进行了大量的检查!它首先使用-e比较测试$HOME是否存在。如果存在,继续用-f来测试它是不是一个文件。如果它不是文件(当然不会是了),就会显示一条消息,表明这不是一个文件。