• Linux/shell 判断设备文件(/dev)是否存在


    目录

    一、在shell中使用脚本判断

    二、在程序中使用acess命令判断

    1、头文件

    2、函数原型以及参数


    一、在shell中使用脚本判断

    可以通过以下脚本命令来判断设备文件是否存在

    1. #!/bin/sh
    2. if [ -c "/dev/ttySTM2" ]; then
    3. echo "ttySTM2 is exist"
    4. else
    5. echo "ttySTM2 not exist"
    6. fi

    1. if [ -c /dev/ttySTM2 ]; then
    2. echo "ttySTM2 is exist"
    3. else
    4. echo "ttySTM2 not exist"
    5. fi

    文件类型或者设备类型相对应的判断条件

    * -b file = True if the file exists and is block special file.     如果该文件存在并且是块特殊文件。
        * -c file = True if the file exists and is character special file.如果该文件存在并且是字符特殊文件
        * -d file = True if the file exists and is a directory.   如果该文件存在并且是一个目录。
        * -e file = True if the file exists.         如果该文件存在
        * -f file = True if the file exists and is a regular file   如果该文件存在并且是一个普通文件
        * -g file = True if the file exists and the set-group-id bit is set.   如果该文件存在并且设置了组ID位。
        * -k file = True if the files’ “sticky” bit is set.    如果文件的sticky “粘性”位被设置。
        * -L file = True if the file exists and is a symbolic link.   该文件存在并且是一个符号链接。
        * -p file = True if the file exists and is a named pipe.   该文件存在并且是一个命名管道。
        * -r file = True if the file exists and is readable.   文件存在并且是可读的
        * -s file = True if the file exists and its size is greater than zero. 文件存在,它的大小是大于零
        * -S file = True if the file exists and is a socket.     文件存在并且是一个套接字
        * -t fd = True if the file descriptor is opened on a terminal.   文件描述符是在一个终端上打开的
        * -u file = True if the file exists and its set-user-id bit is set. 文件存在,它的设置用户ID位被设置了
        * -w file = True if the file exists and is writable.     文件存在并且可写
        * -x file = True if the file exists and is executable.     文件存在并且是可执行的
        * -O file = True if the file exists and is owned by the effective user id.    文件存在并且是所拥有的有效用户ID
        * -G file = True if the file exists and is owned by the effective group id. 文件存在并且拥有有效的gruop id。

    二、在程序中使用acess命令判断

    1、头文件

    #include

    2、函数原型以及参数

    int access(const char *pathname, int mode);

    filename:可以填写文件夹路径或者文件路径
    mode
    mode    含义
    0    (F_OK) 只判断是否存在
    2    (R_OK) 判断写入权限
    4    (W_OK) 判断读取权限
    6    (X_OK) 判断执行权限
    返回值
    若存在或者具有权限,返回值为0;不存在或者无权限,返回值为-1。
    错误代码
    代码    含义
    EACCESS    参数pathname 所指定的文件不符合所要求测试的权限
    EROFS    欲测试写入权限的文件存在于只读文件系统内
    EFAULT    参数pathname指针超出可存取内存空间
    EINVAL    参数mode 不正确
    ENAMETOOLONG    参数pathname太长
    ENOTDIR    参数pathname为一目录
    ENOMEM    核心内存不足
    ELOOP    参数pathname有过多符号连接问题
    EIO I/O    存取错误
    特别提醒:使用access()作用户认证方面的判断要特别小心,例如在access()后再做open()的空文件可能会造成系统安全上的问题。

    1. #define dev_fn "/dev/ttySTM2"
    2. int panduan=access(dev_fn,0);
    3. if(panduan!=-1)
    4. {
    5. printf("dev_fn exit");
    6. }
    7. else
    8. {
    9. printf("dev_fn not exit");
    10. }

  • 相关阅读:
    前端实现打字效果
    企业财务数字化转型怎么才能落地?_光点科技
    Wireshark数据抓包分析之传输层协议(TCP协议)
    使用词袋模型(BoW)测试提取图像的特征点和聚类中心
    Electron+Vue3整合-开发时整合-全部ts开发 + 一条命令启动vue3和electron两个服务
    huggingface transformers库中LlamaForCausalLM
    Redis
    使用大模型提效程序员工作
    撤销git 命令
    死锁是什么?有什么条件?怎样处理?
  • 原文地址:https://blog.csdn.net/qq_43445867/article/details/128152068