**
按照文件类型判断
**
# 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]#