shell编程题(十七)

题目:

终端输入一个文件名,判断是否是设备文件。

答案:

#!/bin/bash

echo -e "please input a filename to judge it is or not a device file.

"
read -p "Input a filename: " filename 
if [ -b $filename -o -c $filename ]; then
    echo "$filename is a device file"
    exit 0
else
    echo "$filename is not a device file"
    exit 1
fi

扩展:

文件类型判断:

    1. [-b]:表示判断文件属性是否是装置文件里面的可供储存的接口设备(可随机存取装置);
    2. [-d]:表示判断文件属性是否是目录;
    3. - ]:表示判断文件属性是否是文件;(存疑,不知道是不是这种表示方法)
    4. [ -l ]:表示判断文件属性是否为链接文档(link file);
    5. [ -c ]:表示判断文件属性是否为装置文件里面的串行端口设备,例如键盘、鼠标(一次性读取装置)。
原文地址:https://www.cnblogs.com/wanghao-boke/p/12152261.html