【CentOS 7Shell编程3】,if判断文件、目录属性和if判断的一些特殊用法#180207

hellopasswdphp


if判断文件、目录属性

  • [-f file]判断是不是普通文件,且存在
  • [-d file]判断是不是目录,且存在
  • [-e file]判断文件或目录是否存在
  • [-r file]判断文件是否可读
  • [-w file]判断文件是否可写
  • [-x file]判断文件是否可执行
[root@localhost ~]# cd shell/
[root@localhost shell]# vi 1.sh
添加
      1 #!/bin/bash
      2 f="/tmp/user"
      3 if [ -f $f ]
      4 then
      5     echo $f exist
      6 else
      7     touch $f
      8 fi
[root@localhost shell]# sh -x 1.sh 
+ f=/tmp/user
+ '[' -f /tmp/user ']'
+ touch /tmp/user
[root@localhost shell]# ls /tmp/
ks-script-WBlSuE  mysql.sock  pear  php-fcgi.sock  user  yum.log
[root@localhost shell]# 
[root@localhost shell]# 
[root@localhost shell]# 
[root@localhost shell]# 
[root@localhost shell]# sh -x 1.sh 
+ f=/tmp/user
+ '[' -f /tmp/user ']'
+ echo /tmp/user exist
/tmp/user exist
[root@localhost shell]# ls /tmp/
ks-script-WBlSuE  mysql.sock  pear  php-fcgi.sock  user  yum.log
[root@localhost shell]# vi 1.sh 
添加
      1 #!/bin/bash
      2 f="/tmp/user"
      3 if [ -d $f ]
      4 then
      5     echo $f exist
      6 else
      7     mkdir $f
      8 fi
[root@localhost shell]# vi 1.sh 
添加
      1 #!/bin/bash
      2 f="/tmp/user/"
      3 if [ -e $f ]
      4 then
      5     echo $f exist
      6 else
      7     mkdir $f
      8 fi

if判断的一些特殊用法

  • if [ -z "$a" ]这个表示当变量a的值为空时会怎么样
  • if [ -n "$a" ]表示当前变量a的值不为空
  • if grep -q ‘123’ 1.txt;then表示若是1.txt种含有'123'的行时会怎么样
  • if [ ! -e file ];then表示文件不存在时会怎么样
  • if (( $a < 1 ));then...等同于if [ $a -lt 1 ];then ...
  • []中不能使用<,>,==,!....等符号
[root@localhost ~]# cd shell/
[root@localhost shell]# ls
1.sh
[root@localhost shell]# if [ -n 1.sh ]; then echo ok; fi
ok
[root@localhost shell]# echo $b

[root@localhost shell]# if [ -n "$b" ]; then echo $b; else echo "b is null"; fi
b is null
[root@localhost shell]# vi 1.sh 
添加
      1 #!/bin/bash
      2 n=`wc -l /tmp/1.txt`
      3 if [ -z "$n" ]
      4 then
      5     echo error
      6     exit
      7 else
      8     if [ $n -gt 100 ]
      9     then
     10         echo ok
     11     fi
     12 fi

能够改成mysql

[root@localhost shell]# vi 1.sh 
添加
      1 #!/bin/bash
      2 n=`wc -l /tmp/1.txt`
      3 if [ -z "$n" ]
      4 then
      5     echo error
      6     exit
      7 elif [ $n -gt 100 ]
      8 then
      9     echo ok
     10 fi
[root@localhost shell]# sh -x 1.sh 
++ wc -l /tmp/1.txt
wc: /tmp/1.txt: No such file or directory
+ n=
+ '[' -z '' ']'
+ echo error
error
+ exit
[root@localhost shell]# ls /tmp/
ks-script-WBlSuE  mysql.sock  pear  php-fcgi.sock  user  yum.log
1 #!/bin/bash
      2 if [ ! -f /tmp/1.txt ]
      3 then
      4     echo "/tmp/1.txt nnot exist."
      5     exit
      6 fi
      7 n=`wc -l /tmp/1.txt`
      8 if [ -z "$n" ]
      9 then
     10     echo error
     11     exit
     12 elif [ $n -gt 100 ]
     13 then
     14     echo ok
     15 fi

[root@localhost shell]# sh -x 1.sh 
+ '[' '!' -f /tmp/1.txt ']'
+ echo '/tmp/1.txt nnot exist.'
/tmp/1.txt nnot exist.
+ exit

使用变量时建议用双引号标注sql

[root@localhost shell]# if grep -w 'user1' /etc/passwd; then echo "user1 exist"; fi
[root@localhost shell]# if ! grep -w 'user1' /etc/passwd; then echo "user1 exist"; fi
user1 exist

修改于 180207shell

相关文章
相关标签/搜索