shell编程-条件判断与流程控制-整数字符串的比较

**

按照文件类型判断

**
在这里插入图片描述


# File Name: file.sh
# Author: Finley
# mail: 2206952220@qq.com
# Create Time: Wed 21 Aug 2019 09:18:08 AM CST
#====================================================================================
#!/bin/bash
#判断是否为设备文件
if [ -b /dev ]
        then
        echo "/dev is devive file"
fi
if [ -c /dev ]
        then
        echo "/dev 不是设备字符文件"
fi
if [ -d /etc/nginx ]
        then
        echo "/etc/nginx is direcoty"
fi
if [ -e /etc/nginx/nginx.conf ]
        then
        echo "/etc/nginx/nginx.conf is a file and it exits"
fi
if [ -f /home/admin/shell/iffull.sh ]
        then
        echo "/home/admin/shell/iffull.sh is a file"
fi
if [ ! -s /home/admin/shell/no.txt ]
        then
        echo "no.txt is null"
fi
                                              

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 这种条件判断不会进行太复杂的条件判断,只判断是否有权限,而不会是哪一个用户才有这种权限。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
博客:https://www.cnblogs.com/songgj/p/9115954.html

  • 硬链接:https://baike.sogou.com/v63228740.htm?fromTitle=%E7%A1%AC%E9%93%BE%E6%8E%A5
  • 来源于搜狗百科
[root@izwz97473w2ydu1pgsmzk4z test]# ll
total 8
-rw-r--r-- 1 root root  64 Aug 13 20:25 student.txt
-rw-r--r-- 1 root root 158 Aug 13 19:35 zheng.txt
[root@izwz97473w2ydu1pgsmzk4z test]# ll -i
total 8
1445009 -rw-r--r-- 1 root root  64 Aug 13 20:25 student.txt
1445008 -rw-r--r-- 1 root root 158 Aug 13 19:35 zheng.txt
[root@izwz97473w2ydu1pgsmzk4z test]# ln zheng.txt /opt/code/stu.txt
[root@izwz97473w2ydu1pgsmzk4z test]# [ zheng.txt -ef /opt/code/stu.txt] && echo "yes"||echo "no"
-bash: [: missing `]'
no
[root@izwz97473w2ydu1pgsmzk4z test]# [ /opt/test/zheng.txt -ef /opt/code/stu.txt ] && echo "yes"||echo "no"
yes
[root@izwz97473w2ydu1pgsmzk4z test]# cd /opt/code/
[root@izwz97473w2ydu1pgsmzk4z code]# ls
cppfile  shell  stu.txt
[root@izwz97473w2ydu1pgsmzk4z code]# ll
total 12
drwxr-xr-x 2 root root 4096 Aug 13 20:56 cppfile
drwxr-xr-x 2 root root 4096 Aug 13 11:02 shell
-rw-r--r-- 2 root root  158 Aug 13 19:35 stu.txt
[root@izwz97473w2ydu1pgsmzk4z code]# ll -i
total 12
1444403 drwxr-xr-x 2 root root 4096 Aug 13 20:56 cppfile
1318545 drwxr-xr-x 2 root root 4096 Aug 13 11:02 shell
1445008 -rw-r--r-- 2 root root  158 Aug 13 19:35 stu.txt
[root@izwz97473w2ydu1pgsmzk4z code]# 

  • 整数判断
  • -等于:equal
  • 不等于:unequal
  • 大于:greater than
  • 小于::less than
  • 注意事项:
    之前不是有说,shell中没有明确定义的变量,类型默认为字符串类型吗?这需要注意的是,一旦加入了整数比较的符号。它就会把两侧表达式当成是整数来进行比较。
    还有一点需要注意,就是方括号两(内侧)侧的空格,没有空格,无法正常执行
    在这里插入图片描述
[root@izwz97473w2ydu1pgsmzk4z code]# [ 22 -eq 22] && echo "yes" || echo "no"
-bash: [: missing `]'
no
[root@izwz97473w2ydu1pgsmzk4z code]# [ 22 -eq 22 ] && echo "yes" || echo "no"
yes
[root@izwz97473w2ydu1pgsmzk4z code]# [ 20 -eq 22 ] && echo "yes" || echo "no"
no
[root@izwz97473w2ydu1pgsmzk4z code]# [ 20 -en 22 ] && echo "yes" || echo "no"
-bash: [: -en: binary operator expected
no
[root@izwz97473w2ydu1pgsmzk4z code]# [ 20 -ne 22 ] && echo "yes" || echo "no"
yes
[root@izwz97473w2ydu1pgsmzk4z code]# [ 99 -ft 22 ] && echo "yes" || echo "no"
-bash: [: -ft: binary operator expected
no
[root@izwz97473w2ydu1pgsmzk4z code]# [ 99 -gt 22 ] && echo "yes" || echo "no"
yes
[root@izwz97473w2ydu1pgsmzk4z code]# [ 99 -lt 22 ] && echo "yes" || echo "no"
no
[root@izwz97473w2ydu1pgsmzk4z code]# [ 22 -lt 99 ] && echo "yes" || echo "no"
yes
[root@izwz97473w2ydu1pgsmzk4z code]# [ 22 -ge 2 ] && echo "yes" || echo "no"
yes
[root@izwz97473w2ydu1pgsmzk4z code]# [ 22 -ge 22 ] && echo "yes" || echo "no"
yes
[root@izwz97473w2ydu1pgsmzk4z code]# [ 22 -le 22 ] && echo "yes" || echo "no"
yes
[root@izwz97473w2ydu1pgsmzk4z code]# [ 21 -le 22 ] && echo "yes" || echo "no"
yes
[root@izwz97473w2ydu1pgsmzk4z code]# [ 211 -le 22 ] && echo "yes" || echo "no"
no

在这里插入图片描述

  • 注意使用$符号时候的双引号
[root@localhost ~]# [ -n $name ] && echo yes || echo no
yes
[root@localhost ~]# [ -n $wang ] && echo yes || echo no
yes
[root@localhost ~]# [ -n "$wang" ] && echo yes || echo no
no
[root@localhost ~]# [ -n "$name" ] && echo yes || echo no
no
[root@localhost ~]# 

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 为什么会出现下面这些报错呢
  • 第一:变量名命名有问题
  • 第二:不了解双反引号和$()的区别
./ifroot.sh: line 5: syntax error: unexpected end of file
[root@localhost shelltest]# vim ifroot.sh
[root@localhost shelltest]# vim ifroot.sh
[root@localhost shelltest]# ./ifroot.sh 
./ifroot.sh: line 4: unexpected EOF while looking for matching ``'
./ifroot.sh: line 5: syntax error: unexpected end of file
[root@localhost shelltest]# ./ifroot.sh 
./ifroot.sh: line 4: unexpected EOF while looking for matching ``'
./ifroot.sh: line 5: syntax error: unexpected end of file
[root@localhost shelltest]# vim ifroot.sh
[root@localhost shelltest]# ./ifroot.sh 
$root
[root@localhost shelltest]# vim ifroot.sh
[root@localhost shelltest]# ./ifroot.sh 
root
[root@localhost shelltest]# 

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值