1.使用shell脚本判断文件的类型:
当没有给文件时,输出“Please show me a file after script",当文件不存在时,输出"文件名称 is not exist",当文件的类型是其他类型时,输出每个文件的文件类型名称
<1>建立一个shell脚本(幻数是sh),查看/mnt目录下的文件
[root@localhost mnt]# vim check_file.sh
[root@localhost mnt]# ll
shell脚本中写入的内容是:
#!/bin/bash
[ -z "$1" ] && {
echo "Error: Please show me a file after script"
exit
}
[ -e "$1" ] || {
echo "$1 is not exist"
exit
}
[ -L "$1" ] && {
echo "$1 is a link file"
exit
}
[ -f "$1" ] && {
echo "$1 is a common file"
exit
}
[ -d "$1" ] && {
echo "$1 is a directory"
exit
}
[ -c "$1" ] && {
echo "$1 is a char file"
exit
}
[ -b "$1" ] && {
echo "$1 is a block file"
exit
}
[ -S "$1" ] && {
echo "$1 is a socket file"
exit
}
<2>测试:判断文件的类型
[root@localhost mnt]# sh check_file.sh ##当没有输入任何文件时
[root@localhost mnt]# sh check_file.sh file1 ##当输入的文件是链接文件时(file1文件也是文本文件,但是因为在脚本中判断是链接文件比判断是文本文件更前,所以只显示是链接文件)
[root@localhost mnt]# sh check_file.sh /mnt ##当输入的是一个目录时
[root@localhost mnt]# sh check_file.sh file2 ##当输入的文件不存在时
[root@localhost mnt]# sh check_file.sh /dev/vda ##当输入的文件是一个块设备文件时
[root@localhost mnt]# sh check_fi