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

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

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

 

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: shelllinux

“Linux shell编程学习笔记13:文件测试运算-CSDN博客” 的相关文章