shell条件判断if中的-a到-z的意思

本文详细介绍了Linux shell脚本中用于文件和字符串条件判断的各种标志,包括文件是否存在、类型、权限、大小等,并通过实例演示了如何使用这些条件判断。同时,文章还涵盖了数字比较和复杂逻辑判断,是理解shell脚本控制流程的重要参考资料。

1、shell条件判断if中的-a到-z的意思

[ -a FILE ]  如果 FILE 存在则为真。  
[ -b FILE ]  如果 FILE 存在且是一个块文件则为真。块文件比如(/dev/sda1)  
[ -c FILE ]  如果 FILE 存在且是一个字特殊文件则为真。(不知道是个啥用)  
[ -d FILE ]  如果 FILE 存在且是一个目录则为真。  
[ -e FILE ]  如果 FILE 存在则为真。  

[ -f FILE ]  如果 FILE 存在且是一个普通文件则为真。 
(普通文件就是一般类型的文件,当用 ls -l 查看某个目录时,第一个属性为‘-‘的文件就是正规文件,或者叫普通文件。)

[ -g FILE ] 如果 FILE 存在且已经设置了SGID则为真。

[root@localhost scripts]# touch SUID
[root@localhost scripts]# chmod u+s SUID
[root@localhost scripts]# touch SGID
[root@localhost scripts]# chmod g+s SGID
[root@localhost scripts]# ll
-rw-r-Sr--. 1 root root      0 3月  10 14:03 SGID
-rwSr--r--. 1 root root      0 3月  10 14:02 SUID

[root@localhost scripts]# if [ -g SGID ];then echo "满足" ;else echo "不满足";fi
满足
[root@localhost scripts]# if [ -g SUID ];then echo "满足" ;else echo "不满足";fi
不满足

[ -h FILE ]  如果 FILE 存在且是一个软连接则为真。  

软连接格式:ln -s 源文件 软连接文件
[root@localhost scripts]# ln -s 1_user_passwd 1_user_passwd_ln
[root@localhost scripts]# ll
-rw-r--r--. 1 root root     55 3月   7 10:48 1_user_passwd
lrwxrwxrwx. 1 root root     13 3月  10 14:07 1_user_passwd_ln -> 1_user_passwd

[root@localhost scripts]# if [ -h 1_user_passwd_ln ];then echo "满足" ;else echo "不满足";fi
满足
[root@localhost scripts]# if [ -h 1_user_passwd ];then echo "满足" ;else echo "不满足";fi
不满足

硬链接格式:ln 源文件 硬连接文件
[root@localhost scripts]# ln 2_backuplog 2_backuplog_hard
[root@localhost scripts]# ll
-rw-r--r--. 2 root root     90 3月   7 11:44 2_backuplog
-rw-r--r--. 2 root root     90 3月   7 11:44 2_backuplog_hard

[root@localhost scripts]# if [ -h 2_backuplog ];then echo "满足" ;else echo "不满足";fi
不满足
[root@localhost scripts]# if [ -h 2_backuplog_hard ];then echo "满足" ;else echo "不满足";fi
不满足

[ -k FILE ]  如果 FILE 存在且已经设置了粘制位则为真。

Linux-粘滞位的使用_Jerry00713的博客-优快云博客  

[ -p FILE ]  如果 FILE 存在且是一个名字管道(F如果O)则为真。(不知道是个啥用)  
管道是linux里面进程间通信的一种方式,其他的还有像信号(signal)、信号量、消息队列、共享内存、套接字(socket)等 

 [ -r FILE ]  如果 FILE 存在且是可读的则为真。

[root@localhost scripts]# (umask 626;touch read.txt)
[root@localhost scripts]# ll
----r-----. 1 root root      0 3月  10 14:55 read.txt

[root@localhost scripts]# if [ -r read.txt ];then echo "满足" ;else echo "不满足";fi    # 注意root用户下全部可读
满足

[root@localhost scripts]# chown root.jerry read.txt
[root@localhost scripts]# ll
----r-----. 1 root jerry      0 3月  10 14:55 read.txt

[root@localhost scripts]# su jerry
[jerry@localhost scripts]$ if [ -r read.txt ];then echo "满足" ;else echo "不满足";fi
满足
[jerry@localhost scripts]$ su jerry2
[jerry2@localhost scripts]$ if [ -r read.txt ];then echo "满足" ;else echo "不满足";fi
不满足

[ -s FILE ]  如果 FILE 存在且大小不为0则为真。 判断文件有无内容

[root@localhost scripts]# touch zero
[root@localhost scripts]# du -sh zero
0       zero
[root@localhost scripts]# du -sh 1_user_passwd
4.0K    1_user_passwd

[root@localhost scripts]# if [ -s zero ];then echo "满足" ;else echo "不满足";fi
不满足
[root@localhost scripts]# if [ -s 1_user_passwd ];then echo "满足" ;else echo "不满足";fi
满足

 
[ -t FD ]  如果文件描述符 FD 打开且指向一个终端则为真。  (不知道是个啥用)  

彻底弄懂 Linux 下的文件描述符(fd)_yushuaigee的博客-优快云博客_fd文件描述符

[ -u FILE ]  如果 FILE 存在且设置了SUID (set user ID)则为真。  
[ -w FILE ]  如果 FILE 存在且是可写的则为真。  
[ -x FILE ]  如果 FILE 存在且是可执行的则为真。  

[ -O FILE ]  如果 FILE 存在且属有效用户ID则为真。
如果在root环境下,全部为真。如果非root用户下,此文件的所属主为自己,为真,否则为假

 [ -G FILE ]  如果 FILE 存在且属有效用户组则为真。
如果在root环境下,全部为真。如果非root用户下,此文件的所属组为自己,为真,否则为假 

[ -L FILE ]  如果 FILE 存在且是一个符号连接则为真。软连接(symbolic link)又叫符号连接,跟[ -h FILE ] 一样。
[ -N FILE ]  如果 FILE 存在 and has been mod如果ied since it was last read则为真。   (不知道是个啥用) 

[ -S FILE ]  如果 FILE 存在且是一个套接字则为真。  

[root@localhost ~]# ps -ef |grep mysql
mysql     1687     1  0 04:00 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe
mysql     2387  1687  0 04:00 ?        00:00:28 /usr/sbin/mysqld --basedir=/usr --datadir=/home/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/home/mysql/localhost.localdomain.err --pid-file=/home/mysql/localhost.localdomain.pid --socket=/home/mysql/mysql.sock
root     32142 32051  0 16:11 pts/0    00:00:00 grep --color=auto mysql
[root@localhost ~]# if [ -S /home/mysql/mysql.sock  ];then echo "满足" ;else echo "不满足";fi
满足

[ FILE1 -nt FILE2 ]  如果 FILE1 has been changed more recently than FILE2, or 如果 FILE1 exists and FILE2 does not则为真。  (不知道是个啥用) 
[ FILE1 -ot FILE2 ]  如果 FILE1 比 FILE2 时间要老, 或者 FILE2 存在且 FILE1 不存在则为真。  
[ FILE1 -ef FILE2 ]  如果 FILE1 和 FILE2 指向相同的设备和节点号则为真。 (不知道是个啥用)  
[ -z STRING ]  STRING这个字符串的长度为零则为真。比如 -z $uid
[ -n STRING ] or [ STRING ]  STRING这个字符串的长度为非零则为真 。
[ STRING1 != STRING2 ]    当串STRING1 和STRING2 不等时为真 则为真。 
[ STRING1 < STRING2 ]  如果 “STRING1” sorts before “STRING2” lexicographically in the current locale则为真。  
[ STRING1 > STRING2 ]  如果 “STRING1” sorts after “STRING2” lexicographically in the current locale则为真。  

[ STRING1 == STRING2 ] 、 如果2个字符串相同则为真。  

== 可用于判断变量是否相等,= 除了可用于判断变量是否相等外,还可以表示赋值。
= 与 == 在 [ ] 中表示判断(字符串比较)时是等价的,内容、长度线相等就是等价。如下最后两个语句是等价的:

1

2

3

4

s1="foo"

s2="foo"

[ $s1=$2 ] && echo "equal"

[ $s1==$2 ] && echo "equal"

在 (( )) 中 = 表示赋值, == 表示判断(整数比较),它们不等价,如下((n=5)) 表示赋值,((n==5)) 表示判断。

1

2

3

((n=5))

echo $n

((n==5)) && echo "equal"

2、数字的判断

int1 -eq int2    两数相等为真 
int1 -ne int2    两数不等为真 
int1 -gt int2     int1大于int2为真 
int1 -ge int2    int1大于等于int2为真 
int1 -lt int2      int1小于int2为真 
int1 -le int2     int1小于等于int2为真

3、复杂逻辑判断

-a           与 
-o        或 
 !         非

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值