shell-2

文件目录属性判断

  • 在shell中通常会和文件、目录打交道,对文件或者目录的判断就非常重要了。

  • if判断文件、目录属性
    (1)[ -f file ] 判断是否是普通文件,且存在。
    2层含义:判断该文件是否是普通文件,判断是否存在该文件。

[root@zcy-1 tmp]# vi file1.sh
[root@zcy-1 tmp]# sh -x !$
sh -x file1.sh
+ f=/tmp/123.txt
+ '[' -f /tmp/123.txt ']'
+ touch /tmp/123.txt
[root@zcy-1 tmp]# sh -x file1.sh
+ f=/tmp/123.txt
+ '[' -f /tmp/123.txt ']'
+ echo yaoming
[root@zcy-1 tmp]# cat /tmp/123.txt
yaoming
[root@zcy-1 tmp]# 

(2)[ -d file ] 判断是否是目录,且存在

[root@zcy-1 tmp]# cat file2.sh
#!/bin/bash 
f="/tmp/nba" 
if [ -d $f ]
then 
    touch  $f/11.txt 
else 
   mkdir $f  
fi
[root@zcy-1 tmp]# sh -x !$
sh -x file2.sh
+ f=/tmp/nba
+ '[' -d /tmp/nba ']'
+ mkdir /tmp/nba
[root@zcy-1 tmp]# sh -x file2.sh
+ f=/tmp/nba
+ '[' -d /tmp/nba ']'
+ touch /tmp/nba/11.txt
[root@zcy-1 tmp]# ls /tmp/nba/11.txt 
/tmp/nba/11.txt

(3)[ -e file ] 判断文件或者目录是否存在,如果文件不存在都可以使用touch创建的,如果文件或者目录存在会修改它的3个time

f=/tmp/1.txt
if [ -e $f ] 
then 
  echo ok 
else  
  touch $f 
fi 

(4)[ -r file ] 判断文件是否可写,针对的是当前执行脚本的用户

这里写代码片

(5)[ -w file ] 判断文件是否可读

(6)[ -x file ] 判断文件是否可执行

(7)shell脚本中可以使用&&代替then

[ -f $f ] && rm -f $f 


if [ -f $f ]
then 
  rm -f $f
fi

(8) | | 代替 else

f=/tmp/1.txt
[ -f $f ] || touch $f 
//当/tmp/1.txt 文件不存在时,才会去创建



f=/tmp/1.txt
if [ -f $f ] 
then 
  rm -f $f 
else 
  touch $f 
fi 

  //当文件存在时,就删除文件,当文件不存在时就创建文件

(9) 反向判断

f=/tmp/1.txt
if 
[ ! -f $f ] 
then 
  touch $f 
fi 

使用!就是取反之意


if特殊用法

  • if [ -z “$a” ] 表示当变量a的值为空时会怎么样
[root@zcy-1 tmp]# cat if4.sh 
#!/bin/bash
if  [ ! -f /tmp/41233 ]
then 
    echo "/tmp/41233 not exist."
    exit 
fi 
n='wc -l /tmp/41233'
if [ -z "$n" ]
then 
     echo error 
     exit
elif [ $n -gt 100 ] 
then 
    echo ok 
fi 

[root@zcy-1 tmp]# sh -x if4.sh 
+ '[' '!' -f /tmp/41233 ']'
+ echo '/tmp/41233 not exist.'
/tmp/41233 not exist.
+ exit
[root@zcy-1 tmp]# 

这里判断文件不存在时,退出脚本。也就不用再执行后面的命令了。

  • if [ -n ” $a” ] 表示文件或者变量a不为空时会怎么样
[root@zcy-1 tmp]# cat 123.txt
yaoming
[root@zcy-1 tmp]# if [ -n "123.txt" ] ; then cat "123.txt" ;fi
yaoming
[root@zcy-1 tmp]# echo $b

[root@zcy-1 tmp]# if [ -n "$b" ] ; then echo $b ; else echo " b is null" ; fi 
 b is null
[root@zcy-1 tmp]# 

如果是变量需要用双引号引起来,如果是文件则不用双引号

(3)在逻辑判断中使用一个命令的结果作为判断条件

grep -w "mysql" /etc/passwd  //-w表示精准匹配,


[root@zcy-1 tmp]# if grep -wq 'mysql' /etc/passwd ; then echo "mysql exist" ; fi 
mysql exist
//不加选项会打印出匹配的结果
if ! grep -wq "zcy" /ect/passwd ; then useradd zcy ;fi  
表示zcy用户不存在时就创建该用户

(4)if (($a<1)) ; then... 等同于 if [ $a -lt 1 ] ; then ...

(5)[ ] 中不能使用< > == != >= <=这样的符号


case判断

  • 在shell中还有一种逻辑判断就做case ,较if fi复杂一些。
  • 格式
case 变量名 in 
      value1)
         command
          ;;
      value2)
         command
           ;; 
        *) 
          command 
          ;; 
        esac                  

当变量为 value1执行哪些,当变量为 value2时又执行哪些,当变量既不是 value1又不是 value2的时候,使用*)里面的命令。在上面的例子中;; (双分号)表示value1判断结束。进入下一个判断

  • 在case程序中,可以在条件中使用||,表示或的意思,比如
2|3) 
   command 
    ;; 
  • 编写脚本测试csae的作用
#!/bin/bash
read -p "Please input a number; " n  //定义变量n的值
if [ -z "$n" ]  //判断变量是否为空
then
   echo "Please input a number. "
   exit 1  //当一条命令执行完成后,会返回的值
fi


n1=`echo $n|sed 's/[0-9]//g'`
if [ ! -z $n1 ]  //当变量不为空时,继续输入数字
then
  echo "Please input a number."
  exit 1
#elif [ $n -lt 0 ] || [ $n -gt 100 ]
#then
# echo "The number range is 0-100."
#  exit 1
fi
if [ $n -lt 60 ]  && [ $n -ge 0 ]
then
    tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
    tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
    tag=3
elif [ $n -ge 90  ] && [ $n -le 100 ]
then
    tag=4
else
    tag=0
fi

case $tag in
  1)
      echo  "not ok "
        ;;
  2)
      echo "ok"
        ;;

  3)
      echo "good"
        ;; 
  4echo "evry good"   
        ;;    
  *)
      echo "The number range is 0-100."
        ;;
esac

(1)read -p

[root@zcy-1 tmp]# read -p "Please input a number; " n
Please input a number; 5
[root@zcy-1 tmp]# echo $n
5
[root@zcy-1 tmp]# 

(2)定义返回的值

[root@zcy-1 tmp]# cat ce.sh
#!/bin/bash
read -p "Please input a number; " n
if [ -z "$n" ]
then
   echo "Please input a number. "
   exit 3
fi

[root@zcy-1 tmp]# sh ce.sh 
Please input a number; 
Please input a number. 
[root@zcy-1 tmp]# echo $?
3
[root@zcy-1 tmp]# 

测试

[root@zcy-1 tmp]# sh -x case.sh 
+ read -p 'Please input a number; ' n
Please input a number; 30
+ '[' -z 30 ']'
++ sed 's/[0-9]//g'
++ echo 30
+ n1=
+ '[' '!' -z ']'
+ '[' 30 -lt 60 ']'
+ '[' 30 -ge 0 ']'
+ tag=1
+ case $tag in
+ echo 'not ok '
not ok 
[root@zcy-1 tmp]# sh -x case.sh 
+ read -p 'Please input a number; ' n
Please input a number; 60
+ '[' -z 60 ']'
++ echo 60
++ sed 's/[0-9]//g'
+ n1=
+ '[' '!' -z ']'
+ '[' 60 -lt 60 ']'
+ '[' 60 -ge 60 ']'
+ '[' 60 -lt 80 ']'
+ tag=2
+ case $tag in
+ echo ok
ok
[root@zcy-1 tmp]# sh -x case.sh 
+ read -p 'Please input a number; ' n
Please input a number; 92
+ '[' -z 92 ']'
++ echo 92
++ sed 's/[0-9]//g'
+ n1=
+ '[' '!' -z ']'
+ '[' 92 -lt 60 ']'
+ '[' 92 -ge 60 ']'
+ '[' 92 -lt 80 ']'
+ '[' 92 -ge 80 ']'
+ '[' 92 -lt 90 ']'
+ '[' 92 -ge 90 ']'
+ '[' 92 -le 100 ']'
+ tag=4
+ case $tag in
+ echo 'evry good'
evry good
[root@zcy-1 tmp]# 

猜你喜欢

转载自blog.csdn.net/chunyang315/article/details/79999265