shell脚本:检查文件是否存在Linux认证考试
文章作者 100test 发表时间 2009:12:02 20:42:31
来源 100Test.Com百考试题网
写一个脚本,来检查某个文件是否存在,如果存在,则输出它的详细信息,如果不存在,则提示输出文件不存在。在给出这个脚本之前,先来了解一下如下几个命令:文件upload.zip为例
1. # ll -h upload.zip
-rw-r--r-- 1 root root 3.3M 06-28 23:21 upload.zip
2. # file upload.zip
upload.zip: Zip archive data, at least v1.0 to extract
3. # ls -i upload.zip
1427041 upload.zip
4. # df -h upload.zip
文件系统 容量 已用 可用 已用% 挂载点
/dev/hda3 9.5G 5.7G 3.4G 64% /
下面的脚本将把这些命令融合在一起,来显示一个文件的详细信息。
#!/bin/bash
# This script gives information about a file.
FILENAME="$1"
echo "Properties for $FILENAME:"
if [ -f $FILENAME ]. then
echo "Size is $(ls -lh $FILENAME | awk { print $5 } )"
echo "Type is $(file $FILENAME | cut -d":" -f2 -)"
echo "Inode number is $(ls -i $FILENAME | cut -d" " -f1 -)"
echo "$(df -h $FILENAME | grep -v 文件系统 | awk { print "On",$1", \
which is mounted as the",$6,"partition."} )"
else
echo "File does not exist."
fi
记得要赋予脚本可执行权限哦!!!!
chomd u x wenjian.sh