Linux运维:Shell脚本(2)

系列笔记传送门:

Shell变量数值计算

shell中的算术运算符
《Linux运维:Shell脚本(2)》

shell中的运算命令
算术运算符常用于运算命令,shell中运算命令有以下几种:
《Linux运维:Shell脚本(2)》

示例1,(())命令实践
双小括号的作用是进行数值运算与数值的比较,常用。

[root@moli_linux1 shell_test] echo $((1+1)) #计算1+1后输出
2
[root@moli_linux1 shell_test] echo $((9-3))
6
[root@moli_linux1 shell_test] ((i=5))
[root@moli_linux1 shell_test] echo $((i+=1)) #获取i值,计算i+1,把i+1重新赋给i,输出i
6
[root@moli_linux1 shell_test] echo $i
6

[root@moli_linux1 shell_test] ((a=1+2**3-4%3))
#<==表达式运算后将结果赋值给变量a,先乘除后加减
[root@moli_linux1 shell_test] echo $((1+2**3-4%3))
#<==这里直接将运算表达式进行运算并将结果输出,输出要加上$符号
8
[root@moli_linux1 shell_test] b=$((1+2**3-4%3))
#<==这里直接将运算表达式进行运算并将结果输出,输出结果赋值给变量b
[root@moli_linux1 shell_test]# echo $b
8
[root@moli_linux1 shell_test] a=$((100*(100+1)/2))
#<==利用公式计算1+2+3....100
[root@moli_linux1 shell_test] echo $a
5050

示例2,用(())命令进行比较

[root@moli_linux1 shell_test] echo $((3>6)) 
#<==3>6是不成立的,因此输出0。表示比较时,1为真,0为假
0
[root@moli_linux1 shell_test] echo $((3>1))
#<==3>1是成立的,因此输出1.
1
[root@moli_linux1 shell_test] echo $((3==3))
#<==3=3是成立的,因此输出1
1

注意:上面提到的数字和变量必须是整型的,不能是浮点型或字符串。下面的bc和awk命令可以用于进行浮点数运算。

[root@moli_linux1 ~] echo $((a=1.0+2.5))
-bash: a=1.0+2.5: 语法错误: 无效的算术运算符 (错误符号是 ".0+2.5")
[root@moli_linux1 ~]# 

示例3,有关++,–的运算

[root@moli_linux1 shell_test] a=10
[root@moli_linux1 shell_test] echo $((a++))
变量a在运算符++之前,输出整个表达式时会输出a的值,再进行表达式的自增
10
[root@moli_linux1 shell_test] echo $a
此时输出a变量的值,是自增后的值,即为11
11
[root@moli_linux1 shell_test] echo $((a--))
11
[root@moli_linux1 shell_test] echo $a
10
[root@moli_linux1 shell_test] echo $((++a))
变量a在运算符++之后,输出整个表达式时会先输出整个表达式自增的值,即11
11
[root@moli_linux1 shell_test] echo $a
11
[root@moli_linux1 shell_test] echo $((--a))
10
[root@moli_linux1 shell_test] echo $a
10

变量在++.–运算符之前,输出表达式的值为变量自身,再进行表达式的自增或自减;变量在++.–运算符之后,会先进行表达式的自增或自减,再输出表达式的值。

let运算命令
let运算命令的语法格式为:let 赋值表达式
let赋值表达式相当于”((赋值表达式))“

[root@moli_linux1 shell_test] i=5
[root@moli_linux1 shell_test] let i=i+1
[root@moli_linux1 shell_test] echo $i
6

expr运算命令
语法:expr 表达式

[root@moli_linux1 shell_test] expr 2 + 2
4
[root@moli_linux1 shell_test] expr 2 - 2
0
[root@moli_linux1 shell_test] expr 2 * 2
expr: 语法错误
[root@moli_linux1 shell_test] expr 2 \* 2
4
[root@moli_linux1 shell_test] expr 2 / 2
1

注意:使用expr时:

  • 运算符及用于计算的数字左右至少有一个空格,否则会报错
  • 使用乘号时,必须使用反斜线屏蔽其特定含义,因为shell可能会误解星号的含义

expr在Shell中可配合变量进行计算,但需要用反引号将计算表达式括起来

[root@moli_linux1 shell_test] i=5
[root@moli_linux1 shell_test] i=` expr $i + 6 `
此处反引号括起来的表达式,变量和数字符号两边都要有空格
[root@moli_linux1 shell_test] echo $i
11

Shell脚本中常见的需求

1、判断一个未知的变量是不是整数
原理: 利用expr做计算时变量或字符串必须是整数的规则,把一个变量和一个整数(非零)相加。看命令的返回值是否为0。如果为0,就认为与整数相加的变量是整数,如果不是0,就不是整数

[root@moli_linux1 shell_test] i=5
#此时的i是整数,数字5
[root@moli_linux1 shell_test] expr $i + 6 &>/dev/null 
#把i与一个整数相加,&>/dev/null 表示不保留任何输出
[root@moli_linux1 shell_test] echo $?
0
#返回0说明i是整数
[root@moli_linux1 shell_test] i=moli
[root@moli_linux1 shell_test] expr $i + 6 &>/dev/null 
[root@moli_linux1 shell_test] echo $?
2
#返回1说明i不是是整数

2、判断传参是不是整数

[root@moli_linux1 shell_test] cat t3.sh 
#!/bin/bash
expr $1 + 1 >/dev/null 2>&1
[ $? -eq 0 ] &&echo int||echo chars
#这是一个条件表达式语法,返回值为0则输出int,否则输出chars
[root@moli_linux1 shell_test] sh t3.sh 1
int
[root@moli_linux1 shell_test] sh t3.sh a
chars

3、编写shell脚本,查找一段语句中字符数小于6的单词
思路,需要知道字符串长度的计算方法,利用for循环。
字符串长度可以利用expr命令得出,如
几种计算字符串的方法

[root@moli_linux1 ~] char="i am a student"
[root@moli_linux1 ~] expr length "$char"
14
[root@moli_linux1 ~] echo ${#char}
14
[root@moli_linux1 ~] echo ${char} | wc -L
14
[root@moli_linux1 ~] echo ${char} | awk '{print length($0)}'
14

知道怎么计算字符串长度的方法,就可以利用计算出来的长度和整数做比较就可以得出结果了,例如判断下面这条语句。(有关for循环,if判断稍后再讲)
i am oldboy linux welcome to our training

[root@moli_linux1 shell_test] cat wordLength.sh 
#!/bin/bash
for n in i am oldboy welcome to our training
do
  if [ `expr length $n` -le 6 ]
    then
      echo $n
  fi
done
[root@moli_linux1 shell_test] sh wordLength.sh 
i
am
oldboy
to
our

4、基于Shell变量输出read命令的运算实践

Shell变量除了可以直接赋值或脚本传参外,还可以使用read命令从标准输入中获得。
语法格式:read [参数] [变量名]
常用参数:

  • -p prompt : 设置提示信息
  • -t timeout:设置输入等待的时间,单位默认为秒

实现read命令的基本读入功能

[root@moli_linux1 ~] read -t 10 -p "Please input a num:" num #作为接受read的变量,不该带$符号
#读入一个输入,赋值给num变量,注意,num变量前需要有空格
Please input a num:10 #输入10,把数字10赋值给num变量
[root@moli_linux1 ~] echo $num
10
[root@moli_linux1 ~] read -t 10 -p "Please input two num:" a1 a2
#读入两个输入,注意要以空格隔开,分别赋值给a1 a2变量,a1变量前后都要有空格
Please input two num:10 20
[root@moli_linux1 ~] echo $a1 $a2
10 20
[root@moli_linux1 ~] read a3 a4
30 40
[root@moli_linux1 ~] echo $a3
30
[root@moli_linux1 ~] echo $a4
40

read命令小实践

[root@moli_linux1 shell_test] cat t2_read_change.sh
#!/bin/bash
#no.1
read -t 10 -p "Pleasa input two int number:" a b
#通过read命令读入两个值,赋给变量a和变量b
[ ${#a} -le 0 ]&&{
#利用条件表达式,根据变量值的长度是否小于1,来确定第一个数是否为空
    echo "The first number is null."
    exit 1
}
[ ${#b} -le 0 ]&&{
#利用条件表达式,根据变量值的长度是否小于1,来确定第二个数是否为空
    echo "The second number is null."
    exit 2
}
#no.2 判断传参是否是整数
expr $a + 1 &>/dev/null
RETVAL_A=$? 
expr $b + 1 &>/dev/null
RETVAL_B=$?
#如果A和B中有一个不是整数,那么就打印提示并退出。
if [ $RETVAL_A -ne 0 -o $RETVAL_B -ne 0 ];then
    echo "one of the num is not num,please input again."
    exit 3
fi
#no.3
echo "a+b=$(($a+$b))"
echo "a-b=$(($a-$b))"
echo "a*b=$(($a*$b))"
echo "a/b=$(($a/$b))"

shell的条件测试与比较

在bash中各种条件结构和流程控制结构中都要进行各种测试,根据测试结果执行不同的操作
执行条件测试表达式后通常会返回真或假。
在bash编程里,条件测试常用的语法有四种,

条件测试语法说明
test <表达式>利用test命令进行条件测试表达式的方法。test命令和表达式之间至少有一个空格。
[ <表达式> ]这是最常用的条件测试表达式。通过[ ]来测试括号内的表达式的方法。[ ]左右边界至少有一个空格
[[ <表达式> ]]和[]类似,比较不常用,但是它可以使用通配符等进行模式匹配,通常[[ ]]左右边界至少有一个空格
((<表达式>))利用双小括号,两端不需要有空格,常用于if里的计算。

注意:&&,||,<,>等操作符可以应用在[[ ]]中,但不能应用在[ ]中,在[ ]中一般使用 -a,-o,-gt等。

文件测试表达式
《Linux运维:Shell脚本(2)》

test条件测试表达式实践
test命令的语法是:test <测试表达式>
对于下面的命令,

test -f file && echo true || echo false

该语句表示如果文件file存在,就输出true,否则输出false。-f参数是文件测试表达式之一,用于测试文件是否为普通文件。

test表达式也可以只用一半逻辑,&& 或者 ||,如:

test -f file && echo true #如果表达式成立就输出true
test -f file || echo false #如果表达式不成立就输出flase

test中 -选项用来测试字符串长度,如果为0则表达式成立,否则不成立

[root@moli_linux1 ~] test -z "study" && echo 1 || echo 0
0 #因为study字符串长度不是0,因此输出0

[ ]条件测试表达式实践
[ ]条件测试表达式的语法: [ <测试表达式> ]
对于下面语句:

[ -f /tmp/test.txt ] && echo 1 || echo 0

如果/tmp/下有test.txt文件存在,则输出1.否则输出0.

文件测试表达式示例
普通文件测试文件类型

[root@moli_linux1 shelltest] touch oldboy #创建普通文件oldboy
[root@moli_linux1 shelltest] ll oldboy 
-rw-r--r-- 1 root root 0 1月  23 18:32 oldboy
[root@moli_linux1 shelltest] [ -f oldboy ] && echo 1 || echo 0
#文件存在,条件为真,输出1
1

目录文件(测试文件类型)

[root@moli_linux1 shelltest] mkdir oldgirl #创建目录文件oldgirl
[root@moli_linux1 shelltest] [ -f oldgirl ] && echo 1 || echo 0
#输出为0,说明文件oldgirl不是普通文件,oldgirl是目录的确不是普通文件
0
[root@moli_linux1 shelltest] [ -e oldgirl ] && echo 1 || echo 0
1  #输出为1,证明文件oldgirl存在,不管oldfirl是目录还是普通文件
[root@moli_linux1 shelltest] [ -d oldgirl ] && echo 1 || echo 0
1 #输出为1,证明oldgirl是目录
[root@moli_linux1 shelltest] [ -d oldboy ] && echo 1 || echo 0
0#输出为0,证明oldboy不是目录

字符串测试表达式
字符串测试操作符的作用:比较两个字符串是否相同,测试字符串的长度是否为零,字符串是否为null等。
《Linux运维:Shell脚本(2)》
示例:

[root@moli_linux1 shell_test] [ -n "abc" ] && echo 1 || echo 0
1 #字符串abc的长度不为0,表达式为真,输出1

[root@moli_linux1 shell_test] [ -z "abc" ] && echo 1 || echo 0
0 #字符串abc的长度不为0,表达式为假,输出0

[root@moli_linux1 shell_test] test -n "abc" && echo 1
1 #使用test等同于[]

[root@moli_linux1 shell_test] char=abc
[root@moli_linux1 shell_test] test -n "$char" && echo 1
1 #使用变量时要注意使用双引号    
[root@moli_linux1 shell_test] [ -z "$char" ] && echo 1 || echo 0
0

[root@moli_linux1 shell_test] char1=abc
[root@moli_linux1 shell_test] char2=abc
[root@moli_linux1 shell_test] char3=moli
[root@moli_linux1 shell_test] [ "$char1" = "$char2" ] && echo 1 || echo 0
1  #变量char1等于char2,表达式为真,输出1

[root@moli_linux1 shell_test] [ "$char1" = "$char3" ] && echo 1 || echo 0
0 #变量char1不等于char2,表达式为假,输出0

[root@moli_linux1 shell_test] [ "$char1" != "$char3" ] && echo 1 || echo 0
1 #变量char1不等于char3,表达式为真,输出1

整数二元比较操作符
《Linux运维:Shell脚本(2)》
示例:

[root@moli_linux1 ~]# a1=98
[root@moli_linux1 ~]# a2=99
[root@moli_linux1 ~]# [ $a1 -eq $a2 ] && echo 1 || echo 0 #判断a1和a2是否相等
0
[root@moli_linux1 ~]# [ $a1 -gt $a2 ] && echo 1 || echo 0#判断a1是否大于a2
0
[root@moli_linux1 ~]# [ $a1 -lt $a2 ] && echo 1 || echo 0#判断a1是否小于a2
1
[root@moli_linux1 ~]# [ $a1 -ne $a2 ] && echo 1 || echo 0#判断a1和a2是否不相等
1

逻辑操作符
《Linux运维:Shell脚本(2)》
示例:

[root@moli_linux1 shelltest]# [ -f oldboy.txt -o -f oldgirl.txt ] && echo 1
1
[root@moli_linux1 shelltest]# [ -f oldboy.txt -a -f oldgirl.txt ] && echo 1
1
[root@moli_linux1 shelltest]# [ -f oldboy.txt -a -f younggirl.txt ] || echo 1
1
[root@moli_linux1 shelltest]# [ -f oldboy.txt -o -f younggirl.txt ] && echo 1 || echo 0
1

-o规则:
当左边为真,右边为真,结果为真
当左边为真,右边为假,结果为真
当左边为假,右边为真,结果为真
当左边为假,右边为假,结果为假
-a规则:
当左边为真,右边为真,结果为真
当左边为真,右边为假,结果为假
当左边为假,右边为真,结果为假
当左边为假,右边为假,结果为假

总结
《Linux运维:Shell脚本(2)》

条件表达式和测试表达式的实践脚本

示例1
通过命令行传入字符或数字,判断传入的字符或数字,执行相应的操作。

[root@moli_linux1 script]# cat test6.sh 
#!/bin/bash
read -p "pls input a char:" var #读取用户输入赋值给var变量
[ "$var" == "1" ] &&{ #条件判断,看变量值是否为1
    echo 1
    exit 0
}
[ "$var" == "2" ] &&{ #条件判断,看变量值是否为2
    echo 2    
    exit 0
}
[ "$var" != "2" -a "$var" != "1" ] &&{ #如果不等于1或者2,就打印错误提示并退出
    echo error
    exit 0
}

示例2
打印选择菜单,安装选择项一键安装不同的web服务。

[root@moli_linux1 script]# mkdir -p /root/server/script/lamp.sh
[root@moli_linux1 script]# mkdir -p /root/server/script/lnmp.sh
[root@moli_linux1 script]# echo "echo the lamp is installed" > lamp.sh
[root@moli_linux1 script]# echo "echo the lnmp is installed" > lnmp.sh
[root@moli_linux1 script]# vim menu.sh

#!/bin/bash
path=/root/server/script
[ ! -d "$path" ] && mkdir -p $path
#!/bin/bash
path=/root/server/script
[ ! -d "$path" ] && mkdir -p $path

#menu
cat <<END
    1.[install lamp]
    2.[install lnmp]
    3.[exit]
    pls input the num you want:
END
read num
expr $num + 1 &>/dev/null
[ $? -ne 0 ] && {
    echo "the num you input must be {1|2|3}"
    exit 1
}
[ $num -eq 1 ] &&{
    echo "starint installing lamp"
    sleep 2;
    [ -x "$path/lamp.sh" ]||{
        echo "$path/lamp.sh does not exit or can nor be exec"
        exit 1
    }
    $path/lamp.sh
    exit $?
}
[ $num -eq 2 ] &&{
    echo "starint installing lnmp"
    sleep 2;
    [ -x "$path/lnmp.sh" ]||{
        echo "$path/lnmp.sh does not exit or can nor be exec"
        exit 1
    }
    $path/lnmp.sh
    exit $?
}
[ $num -eq 3 ] &&{
    echo bye
    exit 3
}
[[ ! $num =~ [1-3] ]] &&{ #<==[[]]
    echo "the num you input must be {1|2|3}"
    echo "Input Error"
    exit 4
}
    原文作者:mori_d
    原文地址: https://segmentfault.com/a/1190000018034251
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞