Linux shell编程学习笔记13:文件测试运算

news2025/3/13 20:58:27

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检测文件是否 socketsocket
-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

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1103972.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

PowerShell批量修改DNS域名解析

批量添加DNS A记录 $dnsServerName"" # DNS服务器的服务器名称,如果是在DNS服务器本机执行则可留空 $containerName"test.com" # 域名的后缀也就是DNS Zone Name $mydns[WMIClass]"ROOT\MicrosoftDNS:MicrosoftDNS_resourceRecord"…

yarn : 无法加载文件 C:\Program Files\nodejs\yarn.ps1

问题描述: 问题分析: 这个错误提示说明在电脑系统上禁止运行 PowerShell 脚本,因此导致无法加载 Yarn 的安装脚本。这是由于系统的执行策略(Execution Policies)设置所导致的。 解决方法: 1. 以管理员身…

关于6轴球腕机械臂的肩部奇异描述纠正

对于常见的球腕6轴机械臂构型,在大多数资料中奇异点描述如下: 肩部奇异点(Shoulder singularity): 肩部奇异点是在机器人手腕的中心与J1轴关节在同一条直线上时发生。这种情况下,会导致关节轴1和4试图瞬间旋…

【Java-框架-SpringMVC】(01) SpringMVC框架的简单创建与使用,快速上手 - 简易版

前言 【描述】 "SpringMVC"框架的简单创建与使用,快速上手; 【环境】 系统"Windows",软件"IntelliJ IDEA 2021.1.3(Ultimate Edition)";“Java版本"1.8.0_202”,“Spring"版…

【Machine Learning】02-Advanced Learning Algorithms

02-Advanced Learning Algorithms 2. Advanced Learning Algorithms2.1 Neural Network2.1.1 概述2.1.2 Neural network model2.1.3 TensorFlow的实现2.1.4 Neural network implementation in Python2.1.5 强人工智能(AGI) 2.2 Vectorization2.2.1 矩阵使…

Hadoop分布式文件系统-HDFS

1.介绍 HDFS (Hadoop Distributed File System)是 Hadoop 下的分布式文件系统,具有高容错、高吞吐量等特性,可以部署在低成本的硬件上。 2.HDFS 设计原理 2.1 HDFS 架构 HDFS 遵循主/从架构,由单个 NameNode(NN) 和多个 DataNode(DN) 组成:

keep-alive 是 Vue 的一个内置组件,用于缓存其他组件的实例,以避免重复渲染和销毁,它可以在需要频繁切换的组件之间提供性能优化

目录 keep-alive 使用 keep-alive 的示例代码: 手动清除组件缓存的示例代码: keep-alive 组件有以下几个优点: keep-alive 的原理: 使用 keep-alive 组件,你可以包裹需要缓存的组件,然后这些组件在切…

基于Springboot实现在线答疑平台系统项目【项目源码+论文说明】

基于Springboot实现在线答疑平台系统演示 摘要 社会的发展和科学技术的进步,互联网技术越来越受欢迎。网络计算机的生活方式逐渐受到广大师生的喜爱,也逐渐进入了每个学生的使用。互联网具有便利性,速度快,效率高,成本…

Brew包的基本安装(手把手教学)

大家在使用mac或者linux系统的过程中,大致了解Homebrew的用处,不多说直接进入正题 相信大家已经看到了Homebrew官网的安装介绍了,我们依然使用终端(不去下载应用过于麻烦) 一、开始安装 在按照官网安装时发现会安装失…

Linux常见基本指令合集及其效果展示

Linux基本命令 文章目录 Linux基本命令1. whoami2. who3. clear4. pwd5. 查看文件信息5.0 什么是文件5.1 ls5.2 ls -l5.3 ls -a5.4 ls -a -l 6. 补充知识:对于Linux系统目录的认知6.1 什么是路径 7. cd8. touch9. mkdir10. rmdir11. rm12. man13. cp14. mv15. nano1…

(Python) Python中三种时间格式的转换方法

1. 时间元组 1.1. 时间元组和时间戳的互相转化 import time,datetime # 获取当前时间的时间元组 t time.localtime() print(t) # 时间元组转时间戳 timestamp time.mktime(t) print(timestamp) # time.struct_time(tm_year2019, tm_mon10, tm_mday23, tm_hour23, tm_min15,…

clion 此文件不属于任何项目目标

如果你已经尝试了所有办法都不行,那就试试这个,不需要重启ide,啥都不用干。版本clion 2023.2.2

华为OD机考算法题:评论转换输出

题目部分 题目评论转换输出难度难题目说明在一个博客网站上,每篇博客都有评论。每一条评论都是一个非空英文字母字符串。评论具有树状结构,除了根评论外,每个评论都有一个父评论。当评论保存时,使用以下格式: 首先是评…

24届好未来数开笔试

目录 选择、多选SQL题目描述输入 目标解答解析 题目分享 选择、多选 Java, int x 1, float y 2, x/y 0.5 2. Hive 的数据结构 基本数据类型 复合数据类型 text 不是 Hive 内外表 建表时如果不显示声明表的类型为 外表 Kafka 通过()避免任务重复执行…

利用QT通过http协议,来实现上传文件功能

#假如你有一台服务器,你最想做哪些事?# 大体上的软件界面如下: 主要阐述一哈写的这个软件实现的功能: 通过名称找到指定的文件,并且将文件按照后缀名的格式进行分开,最后再将所有找到的文件,进…

Vue3-属性绑定、定时任务

1.Vue3-属性绑定 2.定时任务 1.Vue3-属性绑定 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title><!-- vue.js --><script src"https://unpkg.com/vue3/dist/vue.gl…

android开源投屏工具scrcpy简介

目录 一&#xff0c;初识scrcpy 1.1 scrcpy介绍 1.2 scrcpy特点 二&#xff0c;scrcpy指令说明 2.1 画面设置 2.1.1 缩小分辨率 2.1.2 修改画面比特率 2.1.3 限制画面帧率 2.1.4 画面裁剪 2.1.5 锁定屏幕朝向 2.2 屏幕录制 2.3 连接方式 2.3.1 无线 2.3.2 多设备…

云计算:掌控未来,一触即发!

&#x1f389;&#x1f389;欢迎来到我的CSDN主页&#xff01;&#x1f389;&#x1f389; &#x1f3c5;我是尘缘&#xff0c;一个在CSDN分享笔记的博主。&#x1f4da;&#x1f4da; &#x1f449;点击这里&#xff0c;就可以查看我的主页啦&#xff01;&#x1f447;&#x…

Systrace学习笔记

Systrace学习笔记 1.Systrace快捷键2.线程状态3.CPU info4.图形化4.1 Frames帧4.2 用户活动4.3 CPU活动4.4 系统事件 5. SystemServer5.1 SystemServer简介5.2 窗口动画5.3 AMS(ActivityManagerService)5.4 WMS(WindowMagerService)5.5 ServiceThread5.6 HandlerThread 6. Surf…

软考-网络安全体系与网络安全模型

本文为作者学习文章&#xff0c;按作者习惯写成&#xff0c;如有错误或需要追加内容请留言&#xff08;不喜勿喷&#xff09; 本文为追加文章&#xff0c;后期慢慢追加 by 2023年10月 网络安全体系相关安全模型 BLP机密性模型 BLP&#xff08;Biba-格雷泽-麦克拉伦&#x…