1.要求
脚本在执行时
1)如果脚本后未指定代检测文件名时报错“未指定检测文件,请指定”
2)如果脚本后指定的代检测文件不存在时报错“此文件不存在”
3)当文件存在时请检测文件类型并显示到输出中
2.思路
输入文件名时最基本的要求,如果未输入文件名直接报错进退出
所以
[root@westos_ansible mnt]# vim file_check.sh
[root@westos_ansible mnt]# cat file_check.sh
#!/bin/bash
[ -z "$1" ] && {
echo "Error: Please input filename following script"
exit
}
[root@westos_ansible mnt]# sh file_check.sh
Error: Please input filename following script
输入了不存在的文件名时检测:
[root@westos_ansible mnt]# vim file_check.sh
[root@westos_ansible mnt]# cat file_check.sh
#!/bin/bash
[ -z "$1" ] && {
echo "Error: Please input filename following script !!"
exit
}
[ ! -e "$1" ] && {
echo "Error: $1 is not exist !!"
exit
}
[root@westos_ansible mnt]# sh file_check.sh hahaha
Error: hahaha is not exist !!
其中 ! 表示条件反选
输入正确文件名并执行脚本时
显示
[root@westos_ansible mnt]# sh file_check.sh /mnt/
/mnt/ is directory
[root@westos_ansible mnt]# sh file_check.sh /etc/passwd
/etc/passwd is commom file
3.脚本代码
#!/bin/bash
[ -z "$1" ] && {
echo "Error: Please input filename following script !!"
exit
}
[ ! -e "$1" ] && {
echo "Error: $1 is not exist !!"
exit
}
[ -L "$1" ] && {
echo "$1 is link file"
exit
}
[ -f "$1" ] && {
echo "$1 is commom file"
exit
}
[ -d "$1" ] && {
echo "$1 is directory"
exit
}
[ -b "$1" ] && {
echo "$1 is blcok device file"
exit
}
[ -S "$1" ] && {
echo "$1 is socket"
exit
}
[ -c "$1" ] && {
echo "$1 is char"
exit
}