Linux shell编程学习笔记13:文件测试运算
Linux Shell 脚本编程和其他编程语言一样,支持算数、关系、布尔、逻辑、字符串、文件测试等多种运算。前面几节我们依次研究了 Linux shell编程 中的 字符串运算、算术运算、关系运算、布尔运算 和 逻辑运算,今天我们来研究 Linux shell编程中的文件测试运算。
一、文件测试运算符说明
| 操作符 | 说明 | 备注 |
|---|---|---|
| -b file | 检测文件是否是块设备文件,如果是,则返回 true。 | block |
| -c file | 检测文件是否是字符设备文件,如果是,则返回 true。 | char |
| -d file | 检测文件是否是目录,如果是目录,则返回 true。 | directory |
| -f file | 检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。 | file |
| -g file | 检测文件是否设置了 SGID 位,如果是,则返回 true。 | set Group ID |
| -k file | 检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。 | |
| -p file | 检测文件是否是有名管道,如果是,则返回 true。 | name pipe |
| -u file | 检测文件是否设置了 SUID 位,如果是,则返回 true。 | Set User ID |
| -r file | 检测文件是否可读,如果是,则返回 true。 | readonly |
| -w file | 检测文件是否可写,如果是,则返回 true。 | writeable |
| -x file | 检测文件是否可执行,如果是,则返回 true。 | excecutable |
| -s file | 检测文件是否不为空(文件大小是否大于0),不为空返回 true。 | space |
| -e file | 检测文件(包括目录)是否存在,如果是,则返回 true。 | exist |
| -S file | 检测文件是否 socket | socket |
| -L file | 检测文件是否存在并且是一个符号链接 | link |
二、文件测试运算实例
为了进行文件测试实例演示,我们使用文件 /init 和 根目录 / 来作为操作对象。
我们先用ls -l 命令查看文件 /init 的详细属性:
user @ host: / $ ls -l init
-rwxr-xr-x 1 root root 492 Apr 12 2023 init
user @ host : / $

ls -l 命令返回信息中的第一列共有10个字符,可以分个部分:
第一个部分是文件类型,由第1个字符表示,它可能是下列值之一:
- :表示普通文件
d :表示目录
l :表示符号链接
c :表示字符设备文件
b :表示块设备文件
s :表示套接字文件
p :表示管道文件
第二部分表示访问权限,包括第2-第10个字符,以3个字符为一组,共分为3组,第一组由前三个字符组成,表示所有者的权限,第二组由中间三个字符组成,表示所属组的权限,第三组由最后三个字符,表示其他用户的权限。每个字符的含义如下:
r :表示读取权限
w :表示写入权限
x :表示执行权限
- :表示没有对应权限
由命令返回结果可以看出,\init 是一个文件,文件所有者具有读取(r)写入(w)执行(x)权限,所属组和其他用户具有读取(r)和执行(x)权限。
我们再用ls -ld 命令查看 / 的详细属性:
user @ host : / $ ls -ld /
drwxr-xr-x 17 root root 380 Apr 12 2023 //

由命令返回结果可以看出,\ 是一个目录,目录所有者具有读取(r)写入(w)执行(x)权限,所属组和其他用户具有读取(r)和执行(x)权限。
(一)检测文件是否是块设备文件
user @ host : / $ f="/init"
user @ host : / $ if[-b $f ]; then echo "$f is a block file"; else echo "$f is not a block file"; fi
/init is not a block file
user @ host : / $

可见 /init 不是块设备文件
(二)检测文件是否是字符设备文件
user @ host : / $ f="/init"
user @ host : / $ if [-c $f]; then echo "$f is a character file"; else echo "$f is not a character filel"; fi
/init is not a character file
user @ host : / $

可见 \init 不是字符设备文件。
(三)检测文件是否是目录
user @ host : / $ f="/init"
user @ host : / $ if [-d $f ]; then echo"$f is a directory"; else echo "$f is not a directory"; fi
/init is not a directory
user @ host : / $

可见/init 不是一个目录而是一个文件。
user @ host : / $ f="//"
user @ host : / $ if [ -d $f ];then echo "$f is a directory"; else echo "$f is not a directory";fi
// is a directory
user @ host : / $ f="/"
user @ host : / $ if [ -d $f ]; then echo "$f is a directory"; else echo "$f is not a directory"; fi
/ is a directory
user @ host : / $

可见,/ 是一个目录,而不是文件。
(四)检测文件是否是普通文件
user @ host : / $ f="/init"
user @ host : / $ if [-f $f ]; then echo"$f is a file"; else echo "$f is not a file"; fi
/init is a file
user @ host : / $ f="/"
user @ host : / $ if [ -d $f ]; then echo "$f is a file"; else echo "$f is not a file"; fi
/ is not a fle
user @ host : / $

可见,/init是一个文件,/ 不是一个文件。
(五)检测文件是否设置了 SGID 位
user @ host : / $ f="/init"
user @ host : / $ if [ -g $f ];then echo "$f has set the SGID"; else echo "$f has not set the SGID "; fi
/init has not set the SGID
user @ host : / $ f="/"
user @ host : / $ if [ -g $f ];then echo "$f has set the SGID"; else echo "$f has not set the SGID "; fi
/ has not set the SGID
user @ host : / $

可见 /init 和 / 都没有设置SGID。
(六)检测文件是否设置了粘着位(Sticky Bit)
user @ host : / $ f="/init"
user @ host : / $ if [ -k $f ];then echo "$f has set the Sticky Bit"; else echo "$f has not set the Sticky Bit"; fi
/init has not set the Sticky Bit
user @ host : / $ f="/"
user @ host : / $ if [ -k $f ];then echo "$f has set the Sticky Bit"; else echo "$f has not set the Sticky Bit"; fi
/ has not set the Sticky Bit
user @ host : / $

可见 /init 和 / 都没有设置粘着位(Sticky Bit)
(七)检测文件是否是有名管道
user @ host : / $ f="/init"
user @ host : / $ if [ -p $f ];then echo "$f is a named pipe "; else echo "$f is not a named pipe"; fi
/init is not a named pipe
user @ host : / $ f="/"
user @ host : / $ if [ -p $f ];then echo "$f is a named pipe "; else echo "$f is not a named pipe"; fi
/ is not a named pipe
user @ host : / $

可见 /init 和 / 都不是有名管道。
(八)检测文件是否设置了 SUID 位
user @ host : / $ f="/init"
user @ host : / $ if [ -u $f ];then echo "$f has set the SUID"; else echo "$f has not set the SUID"; fi
/init has not set the SUID
user @ host : / $ f="/"
user @ host : / $ if [ -u $f ];then echo "$f has set the SUID"; else echo "$f has not set the SUID"; fi
/ is has not set the SUID
user @ host : / $

可见 /init 和 / 都没有设置 SUID 位
(九)检测文件是否可读
user @ host : / $ f="/init"
user @ host : / $ if [ -r $f ]; then echo "$f is readable"; else echo "$f is not readable"; fi
/init is readable
user@host:/ $

可见 /init是可以读取的。
(十)检测文件是否可写
user @ host : / $ f="/ init"
user @ host : / $ if [ -w $f ];then echo "$f is writable"; else echo "$f is not writable"; fi
/init is not writable
user @ host : / $ f="/"
user @ host : / $ if [ -w $f ];then echo "$f is writable"; else echo "$f is not writable"; fi
/ is not writable
user @ host : / $

可见 /init 和 / 都不可写入。
(十一)检测文件是否可执行
user @ host : / $ f="/init"
user @ host : / $ if [ -x $f ];then echo "$f is executable"; else echo "$f is not executable"; fi
/init is executable
user @ host : / $ f="/"
user @ host : / $ if [ -x $f ];then echo "$f is executable"; else echo "$f is not executable"; fi
/ is executable
user @ host : / $

可见 /init 和 / 都可以执行。
(十二)检测文件是否不为空(文件大小是否大于0)
user @ host : / $ f="/init"
user @ host : / $ if [ -s $f ];then echo "$f is not space"; else echo "$f is space"; fi
/init is not space

可见 /init 不为空。
我们可以用cat命令查看 /init的内容:

(十三)检测文件(包括目录)是否存在
user @ host : / $ f="/init"
user @ host : / $ if [ -e $f ];then echo "$f exists"; else echo "$f does not exist"; fi
/init exists
user @ host : / $ f="/"
user @ host : / $ if [ -e $f ];then echo "$f exists"; else echo "$f does not exist"; fi
/ exists
user @ host : / $ if [ -e null ];then echo "$f exists"; else echo "$f does not exist"; fi
/ exists
user @ host : / $ if [ -e nil ];then echo "$f exists"; else echo "$f does not exist"; fi
/ exists
user @ host : / $ if [ -e /dev/null ];then echo "$f exists"; else echo "$f does not exist"; fi
/ exists
user @ host : / $ f="/dev/null"
user @ host : / $ if [ -e $f ];then echo "$f exists"; else echo "$f does not exist"; fi
/dev/null exists

可见 /init 和 / 以及 /dev/null 都存在。
(十四)检测文件是否 socket
user @ host : / $ f="/init"
user @ host : / $ if [ -S $f ];then echo "$f is a socket"; else echo "$f is not a socket"; fi
/init is not a socket
user @ host : / $ f="/"
user @ host : / $ if [ -S $f ];then echo "$f is a socket"; else echo "$f is not a socket"; fi
/ is not a socket

(十五)检测文件是否存在并且是一个符号链接
user @ host : / $ f="/init"
user @ host : / $ if [ -L $f ];then echo "$f is a link"; else echo "$f is not a link"; fi
/init is not a link
user @ host : / $ f="/"
user @ host : / $ if [ -L $f ];then echo "$f is a link"; else echo "$f is not a link"; fi
/ is not a link
相关文章:
Linux shell编程学习笔记13:文件测试运算
Linux Shell 脚本编程和其他编程语言一样,支持算数、关系、布尔、逻辑、字符串、文件测试等多种运算。前面几节我们依次研究了 Linux shell编程 中的 字符串运算、算术运算、关系运算、布尔运算 和 逻辑运算,今天我们来研究 Linux shell编程中的文件测…...
element ui this.$msgbox 自定义组件
this.$msgbox({title: "选择", message: (<com1figs{this.figs} on-selected{this.new_selected}></com1>),showCancelButton: false,showConfirmButton: false,}); 运行报错 Syntax Error: Unexpected token (89:20) 参考: https://gith…...
尚硅谷Flink(四)处理函数
目录 🦍处理函数 🐒基本处理函数 🐒按键分区处理函数(KeyedProcessFunction) 🐵定时器(Timer)和定时服务(TimerService) // 1、事件时间的案例 // 2、处理…...
AXURE RP EXTENSION For Chrome 安装
在浏览器上输入地址:chrome://extensions/ 打开图片中这个选项,至此你就能通过index.html访问...
24、Flink 的table api与sql之Catalogs(java api操作视图)-3
Flink 系列文章 1、Flink 部署、概念介绍、source、transformation、sink使用示例、四大基石介绍和示例等系列综合文章链接 13、Flink 的table api与sql的基本概念、通用api介绍及入门示例 14、Flink 的table api与sql之数据类型: 内置数据类型以及它们的属性 15、Flink 的ta…...
【CNN-GRU预测】基于卷积神经网络-门控循环单元的单维时间序列预测研究(Matlab代码实现)
💥💥💞💞欢迎来到本博客❤️❤️💥💥 🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。 ⛳️座右铭&a…...
计算机毕业设计--基于SSM+Vue的物流管理系统的设计与实现
末尾获取源码 开发语言:Java Java开发工具:JDK1.8 后端框架:SSM 前端:Vue 数据库:MySQL5.7和Navicat管理工具结合 服务器:Tomcat8.5 开发软件:IDEA / Eclipse 是否Maven项目:是 目录…...
GPT4 Plugins 插件 WebPilot 生成抖音文案
1. 生成抖音文案 1.1. 准备1篇优秀的抖音文案范例 1.2. Promept公式 你是一个有1000万粉丝的抖音主播, 请模仿下面的抖音脚本文案,重新改与一篇文章改写成2分钟的抖音视频脚本, 要求前一部分是十分有争议性的内容,并且能够引发…...
通过核密度分析工具建模,基于arcgis js api 4.27 加载gp服务
一、通过arcmap10.2建模,其中包含三个参数 注意input属性,选择数据类型为要素类: 二、建模之后,加载数据,执行模型,无错误的话,找到执行结果,进行发布gp服务 注意,发布g…...
【vue2高德地图api】02-npm引入插件,在页面中展示效果
系列文章目录 提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 系列文章目录前言一、安装高德地图二、在main.js中配置需要配置2个key值以及1个密钥 三、在页面中使用3.1 新建路由3.2新建vue页面3.2-1 index.vue3.2…...
ai智能语音电销机器人怎么选?
智能语音电销机器人哪家好?如何选择一款智能语音电销机器人?这几年生活中人工智能的普及越来越广泛,就如智能语音机器人在生活当中的应用还是比较方便的,有许多行业都会选择这类的智能语音系统来把工作效率提高上去,随…...
NumPy基础及取值操作
目录 第1关:ndarray对象 相关知识 怎样安装NumPy 什么是ndarray对象 如何实例化ndarray对象 使用array函数实例化ndarray对象 使用zeros,ones,empty函数实例化ndarray对象 代码文件 第2关:形状操作 相关知识 怎样改变n…...
vue webpack/vite的区别
Vue.js 可以与不同的构建工具一起使用,其中两个主要的工具是 Webpack 和 Vite。以下是 Vue.js 与 Webpack 和 Vite 之间的一些主要区别: Vue.js 与 Webpack: 成熟度: Webpack 是一个成熟的构建工具,已经存在多年&…...
多线程下的单例设计模式(新手必看!!!)
在项目中为了避免创建大量的对象,频繁出现gc的问题,单例设计模式闪亮登场。 一、饿汉式 1.1饿汉式 顾名思义就是我们比较饿,每次想吃的时候,都提前为我们创建好。其实我记了好久也没分清楚饿汉式和懒汉式的区别。这里给出我的一…...
JDK 21的新特性总结和分析
🌷🍁 博主猫头虎 带您 Go to New World.✨🍁 🦄 博客首页——猫头虎的博客🎐 🐳《面试题大全专栏》 文章图文并茂🦕生动形象🦖简单易学!欢迎大家来踩踩~🌺 &a…...
【VR】【Unity】白马VR课堂系列-VR开发核心基础03-项目准备-VR项目设置
【内容】 详细说明 在设置Camera Rig前,我们需要针对VR游戏做一些特别的Project设置。 点击Edit菜单,Project Settings,选中最下方的XR Plugin Management,在右边面板点击Install。 安装完成后,我们需要选中相应安卓平台下的Pico VR套件,关于怎么安装PICO VR插件,请参…...
Windows服务器安装php+mysql环境的经验分享
php mysql环境 下载IIS Php Mysql环境集成包,集成包下载地址: 1、Windows Server 2008 一键安装Web环境包 x64 适用64位操作系统服务器:下载地址:链接: https://pan.baidu.com/s/1MMOOLGll4D7Eb5tBrdTQZw 提取码: btnx 2、Windows Server 2008 一键安装Web环境包 32 适…...
【LeetCode热题100】--287.寻找重复数
287.寻找重复数 方法:使用快慢指针 使用环形链表II的方法解题(142.环形链表II),使用 142 题的思想来解决此题的关键是要理解如何将输入的数组看作为链表。 首先明确前提,整数的数组 nums 中的数字范围是 [1,n]。考虑一…...
JUC并发编程——Stream流式计算(基于狂神说的学习笔记)
Stream流式计算 什么是Stream流式计算 Stream流式计算是一种基于数据流的计算模式,它可以对数据进行实时处理和分析,而不需要将所有数据存储在内存中。 Stream流式计算是将数据源中的数据分割成多个小的数据块,然后对每个小的数据块进行并…...
【Eclipse】取消按空格自动补全,以及出现没有src的解决办法
【Eclipse】设置自动提示 教程 根据上方链接,我们已经知道如何设置Eclipse的自动补全功能了,但是有时候敲变量名的时候按空格,本意是操作习惯,不需要自动补全,但是它却给我们自动补全了,这就造成了困扰&…...
eNSP-Cloud(实现本地电脑与eNSP内设备之间通信)
说明: 想象一下,你正在用eNSP搭建一个虚拟的网络世界,里面有虚拟的路由器、交换机、电脑(PC)等等。这些设备都在你的电脑里面“运行”,它们之间可以互相通信,就像一个封闭的小王国。 但是&#…...
k8s从入门到放弃之Ingress七层负载
k8s从入门到放弃之Ingress七层负载 在Kubernetes(简称K8s)中,Ingress是一个API对象,它允许你定义如何从集群外部访问集群内部的服务。Ingress可以提供负载均衡、SSL终结和基于名称的虚拟主机等功能。通过Ingress,你可…...
PHP和Node.js哪个更爽?
先说结论,rust完胜。 php:laravel,swoole,webman,最开始在苏宁的时候写了几年php,当时觉得php真的是世界上最好的语言,因为当初活在舒适圈里,不愿意跳出来,就好比当初活在…...
在HarmonyOS ArkTS ArkUI-X 5.0及以上版本中,手势开发全攻略:
在 HarmonyOS 应用开发中,手势交互是连接用户与设备的核心纽带。ArkTS 框架提供了丰富的手势处理能力,既支持点击、长按、拖拽等基础单一手势的精细控制,也能通过多种绑定策略解决父子组件的手势竞争问题。本文将结合官方开发文档,…...
ESP32读取DHT11温湿度数据
芯片:ESP32 环境:Arduino 一、安装DHT11传感器库 红框的库,别安装错了 二、代码 注意,DATA口要连接在D15上 #include "DHT.h" // 包含DHT库#define DHTPIN 15 // 定义DHT11数据引脚连接到ESP32的GPIO15 #define D…...
Neo4j 集群管理:原理、技术与最佳实践深度解析
Neo4j 的集群技术是其企业级高可用性、可扩展性和容错能力的核心。通过深入分析官方文档,本文将系统阐述其集群管理的核心原理、关键技术、实用技巧和行业最佳实践。 Neo4j 的 Causal Clustering 架构提供了一个强大而灵活的基石,用于构建高可用、可扩展且一致的图数据库服务…...
ArcGIS Pro制作水平横向图例+多级标注
今天介绍下载ArcGIS Pro中如何设置水平横向图例。 之前我们介绍了ArcGIS的横向图例制作:ArcGIS横向、多列图例、顺序重排、符号居中、批量更改图例符号等等(ArcGIS出图图例8大技巧),那这次我们看看ArcGIS Pro如何更加快捷的操作。…...
Docker 本地安装 mysql 数据库
Docker: Accelerated Container Application Development 下载对应操作系统版本的 docker ;并安装。 基础操作不再赘述。 打开 macOS 终端,开始 docker 安装mysql之旅 第一步 docker search mysql 》〉docker search mysql NAME DE…...
【Go语言基础【12】】指针:声明、取地址、解引用
文章目录 零、概述:指针 vs. 引用(类比其他语言)一、指针基础概念二、指针声明与初始化三、指针操作符1. &:取地址(拿到内存地址)2. *:解引用(拿到值) 四、空指针&am…...
MySQL JOIN 表过多的优化思路
当 MySQL 查询涉及大量表 JOIN 时,性能会显著下降。以下是优化思路和简易实现方法: 一、核心优化思路 减少 JOIN 数量 数据冗余:添加必要的冗余字段(如订单表直接存储用户名)合并表:将频繁关联的小表合并成…...
