Shell脚本中的变量, Shell中的逻辑判断, if判断目录属性, if判断的特殊用法, case判断

shell脚本中的变量

  • 当脚本中使用某个字符串较频繁并且字符串长度很长时就应该使用变量代替
    (变量就是一串字符的名字,使用变量等于使用字符串)
  • 使用条件语句时,常使用变量 if [ $a -gt 1 ]; then … ; fi
  • 引用某个命令的结果时,用变量替代
[root@draft ~]# n=`date +%F`   #跟 n=$(date +%F)一样;
[root@draft ~]# echo $n
2019-11-19
[root@draft ~]# m=$(date +%T)
[root@draft ~]# n=$(date +%F)
[root@draft ~]# echo $n#$m
2019-11-19#10:30:07
  • 写和用户交互的脚本时,变量也是必不可少的
read -p "Input a number: " n; echo $n   如果没写这个n,可以直接使用$REPLY

[root@second ~]# read -p "please input. it will save in a variables:" variables1   
#read 内部命令被用来从标准输入读取单行数据, -p后面跟提示信息;
please input. it will save in a variables:content1   #填定变量内容;
[root@second ~]# echo $variables1   #读取变量;
content1
[root@second ~]# read -p "please input. it will save in a variables:"    #这次没有指定变量;
please input. it will save in a variables:contect2			#填写内容;
[root@second ~]# echo $REPLY		#内置变量存储了内容;
contect2
  • 内置变量 $0, $1, $2… $0表示脚本本身,$1 第一个参数,$2 第二个,如此类推, $#表示参数个数
[root@second ~]# vi bl.sh			#脚本;

#! /bin/bash
echo "first variable is $1."	
echo "second variable is $2."
echo "third variable is $3."
echo "total variable is $#."
echo "the shell name is $0."

[root@second ~]# bash bl.sh a b c d e f g h  #运行脚本并输入参数;
first variable is a.
second variable is b.
third variable is c.
total variable is 8.
the shell name is bl.sh.			#$0代表运行的脚本的名字;
[root@second ~]# chmod 755 bl.sh 
[root@second ~]# ./bl.sh a b c d e f g h
first variable is a.
second variable is b.
third variable is c.
total variable is 8.
the shell name is ./bl.sh.      #名字变化了;
[root@second ~]# /root/bl.sh a b c d e f g h
first variable is a.
second variable is b.
third variable is c.
total variable is 8.
the shell name is /root/bl.sh.	#名字根据命令变化了;

输入参数的一个例子

[root@second ~]# service mysqld star   #star是一个变量;
Usage: mysqld  {start|stop|restart|reload|force-reload|status}  [ MySQL server options ]   #命令错误给出提示信息;

mode=$1			#启动脚本里的语句;mode也等于是star;
...
case "$mode" in	#case语句里,没有star;
  'start')
  ...
*)				#star属于*;
      basename=`basename "$0"`	 
      echo "Usage: $basename  {start|stop|restart|reload|force-reload|status}  [ MySQL server options ]"   #给出提示信息;
      exit 1
    ;;
  • 数学运算
[root@second ~]# a=1
[root@second ~]# b=9
[root@second ~]# c=$(($a+$b))
[root@second ~]# echo $c
10
[root@second ~]# c=$[$a+$b]
[root@second ~]# echo $c
10

shell 中的逻辑判断

  • 实现例子,如果a大于60就OK
[root@second ~]# vi if.sh    #脚本尝试if语名;

#! /bin/bash
read -p "please input 1-100:" a
if [ $a -gt 60 ];
then
    echo "OK.";
fi

[root@second ~]# bash if.sh 
please input 1-100:80
OK.

[root@second ~]# a=80
[root@second ~]# if [ $a -gt 60 ];		#界面逐行输入;
> then
> echo "OK.";
> fi
OK.
[root@second ~]# if [ $a -gt 60 ]; then echo "OK."; fi  #一行输入;
OK.
  • 如果a大于60就OK,否则就不OK
#! /bin/bash
read -p "please input 1-100:" a
if [ $a -gt 60 ];
then
    echo "OK.";
else 
    echo "Not OK.";
fi

[root@second ~]# bash if.sh
please input 1-100:30
Not OK.
  • elif可以实现阶梯式的判断,if条件最高,elif条件低一点,elif2条件再低一点,再到最后一级else
#! /bin/bash
read -p "please input 1-100:" a
if [ $a -gt 90 ];
then
    echo "优";					#完成其中一个then就退出;elif的各个等级应该要把所有范围都包括;
elif [ $a -gt 80 ];
        then
        echo "良";
elif [ $a -gt 70 ];
        then
        echo "中";
elif [ $a -gt 60 ];
        then
        echo "及格";
else
    echo "差";
fi

[root@second ~]# bash if.sh
please input 1-100:92
优
[root@second ~]# bash if.sh
please input 1-100:82
良
[root@second ~]# bash if.sh
please input 1-100:72
中
[root@second ~]# bash if.sh
please input 1-100:62
及格
[root@second ~]# bash if.sh
please input 1-100:52
差
  • if的嵌套,先把高于60和其他分开,高于60的可以逐层递增判断条件,每一个等级做不同的事情
#! /bin/bash   #脚本;
read -p "please input 1-100:" a
if [ $a -gt 60 ];    #if嵌套,所有then语名都会执行;
then
    echo "now is 4";
    if [ $a -gt 70 ];
        then echo "now is 3";
             if [ $a -gt 80 ];
                   then echo "now is 2";
                     if [ $a -gt 90 ];
                        then echo "now is 1";fi fi  fi
else
    echo "now is 5"
fi

please input 1-100:50
now is 5
[root@second ~]# bash if2.sh
please input 1-100:66
now is 4
[root@second ~]# bash if2.sh
please input 1-100:77
now is 4
now is 3
please input 1-100:88
now is 4
now is 3
now is 2
[root@second ~]# bash if2.sh
please input 1-100:98
now is 4
now is 3
now is 2
now is 1
  • 逻辑判断表达式:
if里使用的表达式 对应符号
-gt >
-lt <
-ge >=
-le <=
-eq ==
-ne !=
  • 可以使用 && || 结合多个条件
#! /bin/bash
read -p "please input 1-100:" a
if [ $a -gt 60 ] && [ $a -lt 80 ];   #等于    if [ $a -gt 60 -a $a -lt 80 ];   ;
then
    echo "group 2";
else
    echo "group 3";
fi

[root@second ~]# bash if3.sh 
please input 1-100:70
group 2
[root@second ~]# bash if3.sh 
please input 1-100:50
group 3
#! /bin/bash
echo $1 $2
if [ $1 -gt 5 -o $2 -gt 5 ];  #等于 if [ $1 -gt 5 ] || [ $2 -gt 5 ];  ;
then
    echo "ok";
else
    echo "not ok";
fi

[root@second ~]# bash if4.sh 7 0
7 0
ok
[root@second ~]# bash if4.sh 0 0
0 0
not ok
[root@second ~]# bash if4.sh 0 8
0 8
ok

if判断文件目录属性

  • -e, -f, -d
[root@second ~]# ls dir1 bl.sh  #目录下有一个目录和文件;
bl.sh

dir1:
[root@second ~]# if [ -r dir1 ]; then echo "exist"; fi; #-r判断文件或目录是否存在;
exist
[root@second ~]# if [ -r bl.sh ]; then echo "exist"; fi; 
exist
[root@second ~]# if [ -f bl.sh ]; then echo "file and exist"; fi; #-f判断是否存在而且为文件;
file and exist
[root@second ~]# if [ -f dir1 ]; then echo "file and exist"; fi; 
[root@second ~]# if [ -d dir1 ]; then echo "directory and exist"; fi; #-d判断是否存在而且为目录;
directory and exist
[root@second ~]# if [ -d bl.sh ]; then echo "directory and exist"; fi; 
[root@second ~]# 
[root@second ~]# if [ -f 1.txt ]; then echo "file and exist";else echo "kkk" >> 1.txt; fi 			#文件不存在,创建文件;
[root@second ~]# cat 1.txt
kkk
  • -r, -r, -w
[root@second ~]# ll if.sh if2.sh
-rwxr-xr-x 1 root  root  272 11月 19 12:22 if2.sh
-rw------- 1 user1 user1 259 11月 19 12:10 if.sh
[root@second ~]# if [ -r if2.sh ]; then echo "readable"; fi		#判断文件是否可读;
readable
[root@second ~]# if [ -w if2.sh ]; then echo "writable"; fi    #判断文件是否可写;
writable
[root@second ~]# if [ -x if2.sh ]; then echo "runable"; fi    #判断文件是否可运行;运行必须要有运行权限;
[root@second ~]# chmod +x if2.sh
[root@second ~]# if [ -x if2.sh ]; then echo "runable"; fi    
runable
[root@second ~]# if [ -r if.sh ]; then echo "readable"; fi    #设置无权限,root还是可以读;
readable
[root@second ~]# if [ -w if.sh ]; then echo "writable"; fi   #设置无权限,root还是可以写;
writable
[root@second ~]# if [ -x if.sh ]; then echo "runable"; fi    #运行必须要有运行权限;

if判断的一些特殊用法

  • -z, -n
[root@second ~]# if [ -z "$a" ]; then echo "null"; fi  #-z判断变量是否为空,空为1;
null
[root@second ~]# a=90
[root@second ~]# if [ -z "$a" ]; then echo "null"; fi   #不空为0;
[root@second ~]# if [ -n "$a" ]; then echo "not empty"; fi   #-n判断变量不为空,不为空为1;
not empty
[root@second ~]# a=
[root@second ~]# if [ -n "$a" ]; then echo "not empty"; fi  #空为0;
[root@second ~]# a=
[root@second ~]# if [ -n "$a" ]; then echo "not empty"; fi   #$a为空,结果为空;
[root@second ~]# if [ -n $a ]; then echo "not empty"; fi   #echo"", 说明$a不为空,因为没有""引起的错误;
not empty
  • 命令用作判断
[root@second ~]# ls 1.txt 
1.txt
[root@second ~]# if ls 1.txt; then echo "1"; fi     #命令运行正常为1, echo "1", 显示了命令结果;
1.txt
1
[root@second ~]# if ls 0.txt; then echo "1"; fi   ##命令运行错误为0, 显示了错误信息;
ls: 无法访问0.txt: 没有那个文件或目录
[root@second ~]# if ls 0.txt &> /dev/null ; then echo "1"; else echo 0 ; fi   #把错误信息输出到null里;
0
[root@second ~]# grep  root  /etc/passwd     #查找成功为1;
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@second ~]# grep  root  /etc/passwd -q   #-q不打印查找结果;
[root@second ~]# echo $?
0
[root@second ~]# grep  root123  /etc/passwd   #查找不成功为0;
[root@second ~]# echo $?
1
[root@second ~]# if grep root /etc/passwd -q; then echo "1"; fi    #grep作为判断;
1
[root@second ~]# if grep root123 /etc/passwd -q; then echo "1"; fi
[root@second ~]# 
  • !取反
[root@second ~]# if [ -e "1.txt" ]; then echo "not empty"; fi  
not empty
[root@second ~]# if [ ! -e "1.txt" ]; then echo "not empty"; fi  #判断加上取反;
[root@second ~]# 
  • 使用(()),可使用运算符
    [ ] 中不能使用<,>,==,!=,>=,<=这样的符号
[root@second ~]# if (($a>100)); then echo "good"; else echo "not good"; fi
not good
[root@second ~]# if (($a==90)); then echo "good"; else echo "not good"; fi
good

shell中的case判断

  • 格式
     case  变量名 in 
        value1)    
              command  
	      ;;
        value2)      
              command  
              ;; 
        *)      #其他情况;
              commond 
              ;;     
     esac

在case程序中,可以在条件中使用|,表示或的意思, 比如   
     2|3)     
	command
	;;
#!/bin/bash
read -p "Please input a number: " n    #输入值;
if [ -z "$n" ]				#判断值为空,中断;不为空继续;
then
    echo "Please input a number."
    exit 1
fi

n1=`echo $n|sed 's/[0-9]//g'`   #把输入值的数字去除;
if [ -n "$n1" ]				#值为空,继续;不为空要重新输入(代表输入值含其他字符,这个脚本只需要输入数字);
then
 echo "Please input a number."
 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    #case语句的使用;
    1)
	echo "not ok"
        ;;
    2)
        echo "ok"
        ;;
    3)
        echo "ook"
        ;;
    4)
        echo "oook"
        ;;
    *)
        echo "The number range is 0-100."
        ;; 
esac
发布了125 篇原创文章 · 获赞 5 · 访问量 4630

猜你喜欢

转载自blog.csdn.net/tanyyinyu/article/details/103135888