目录
运算符
shell里的所有运算符
运算操作符
1. (())
意义:用于整数运算的常用运算符
说明:在(())中使用变量时可以去掉变量前的$符号
[root@localhost shell]# a=10
[root@localhost shell]# b=20
[root@localhost shell]# echo $((a+b))
30
[root@localhost shell]# echo $((a< b))
1
[root@localhost shell]# echo $((2+2*5))
12
2. let
意义:用于整数运算
说明:使用let命令可以执行一个或者多个算术表达式,其中的变量名毋需使用$符号
[root@localhost shell]# a=10
[root@localhost shell]# let r=a+1
[root@localhost shell]# echo $r
11
[root@localhost shell]# let r=++9
[root@localhost shell]# echo $r
9
3. expr
意义:可用于整数运算,但还有很多其他的额外功能说明:
说明:使用expr时,运算符及用于计算的数字左右都至少有一个空格,否则会报错;使用乘号时,必须使用反斜线屏蔽其特定含义;使用expr做计算,将一个未知的变量和一个已知的整数相加,看返回码是否为0,如果为0就认为做加法的变量为整数,否则就不是整数。
[root@localhost shell]# res=`expr 5+6`
[root@localhost shell]# echo $res
5+6 #使用expr运算命令时运算符两边得加空格
[root@localhost shell]# res=`expr 5 + 6`
[root@localhost shell]# echo $res
11
#使用expr计算字符的长度
[root@localhost test4]# char="i have a"
[root@localhost test4]# expr length "$char"
8
4.bc
意义:linux下的一 个计算器程序(适合整数及小数运算)
说明:通过标准输入得到数据,然后运算(管道或键盘输入等)
[root@localhost shell]# echo '1 + 1' | bc
2
[root@localhost shell]# bc
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
10^8
100000000
3.56+5.55
9.11
5. $[]
意义:用于整数运算
[root@localhost shell]# echo $[ 1 + 3 ]
4
6. declare
意义:定义变量值和属性,-i参数可以用于定义整形变量,做运算
[root@localhost shell]# declare -i r=6*6
[root@localhost shell]# echo $r
36
7. awk
意义:awk既可以用于整数运算,也可以用于小数运算
[root@localhost shell]# echo "12.56 5" | awk '{print ($1-$2)}'
7.56
总结:
1.shell里的运算操作符,除bc外通常不会直接输出结果,需要用echo等自己去调用
${}的使用
示例演示:
[root@localhost shell]# para=abcde
[root@localhost shell]# echo ${para}
abcde
[root@localhost shell]# echo ${#para}
5
[root@localhost shell]# echo ${para:2}
cde
[root@localhost shell]# echo ${para:d}
abcde
[root@localhost shell]# echo ${para:1:2}
bc
[root@localhost shell]# char=abc,abcd,abcde
[root@localhost shell]# echo ${char}
abc,abcd,abcde
[root@localhost shell]# echo ${char#abc*}
,abcd,abcde
[root@localhost shell]# echo ${char##abc*}
[root@localhost shell]# echo ${char%abc*}
abc,abcd,
[root@localhost shell]# echo ${char%%abc*}
[root@localhost shell]# echo ${char/ab/ss}
ssc,abcd,abcde
[root@localhost shell]# echo ${char//ab/ss}
ssc,sscd,sscde
条件测试
1. 条件测试的基本语法
在
shell
程序中,用户可以使用测试语句来测试指定的条件表达式的条件的真或假。当指定的条件为
真时,整个条件测试的返回值为
0
;反之,如果指定的条件为假,则条件测试语句的返回值为非
0
值。
示例应用
[root@localhost shell]# test 1 < 2 && echo 1 #借助echo输出判定结果
1
[root@localhost shell]# [ 1 < 2 ] && echo 1
1
[root@localhost shell]# [[ 1 < 2 ]] && echo 1
1
[root@localhost shell]# ((1 < 2)) && echo 1
1
2. 文件测试表达式
部分示例演示:
[root@localhost shell]# mkdir file.test
[root@localhost shell]# test -a file.test && echo 1 || echo 0
1
[root@localhost shell]# test -a file.te && echo 1 || echo 0
0
[root@localhost shell]# test -c file.test && echo 1 || echo 0
0
[root@localhost shell]# test -b file.test && echo 1 || echo 0
0
[root@localhost shell]# test -d file.test && echo 1 || echo 0
1
[root@localhost shell]# touch aaa
[root@localhost shell]# test -f file.test && echo 1 || echo 0
0
[root@localhost shell]# test -f aaa && echo 1 || echo 0
1
[root@localhost shell]# test -d aaa && echo 1 || echo 0
0
[root@localhost shell]# test -r aaa && echo 1 || echo 0
1
[root@localhost shell]# test file.test -nt aaa && echo 1 || echo 0
0
[root@localhost shell]# test file.test -ot aaa && echo 1 || echo 0
1
3. 字符串测试表达式
示例演示:
[root@localhost shell]# [[ -n abc ]] && echo 1 || echo 0 #字符串长度不为零输出1,为0输出0
1
[root@localhost shell]# [[ -z abc ]] && echo 1 || echo 0 #字符串长度为零输出1,不为0输出0
0
[root@localhost shell]# [[ abc = abc ]] && echo 1 || echo 0 #相同为1不同为0
1
[root@localhost shell]# [[ abc == abc ]] && echo 1 || echo 0
1
[root@localhost shell]# [[ abc == abd ]] && echo 1 || echo 0
0
[root@localhost shell]# [[ abc==abd ]] && echo 1 || echo 0 #符号两边要留空格,不然会错误输出
1
4. 整数测试表达式
注意:
=和
!=
也可在
[]
中作比较时使用,在
[]
中也可使用
>
和
<
符号,但需要使用反斜线转义,有时不转译虽然语 法不会报错,但是结果可能会不对; 在[[]]
中也可使用包含
-gt
和
-lt
的符号,不建议使用;比较符号两端也要有空格。
示例演示:
#使用test演示 注意,以下使用$?返回结果,所以正确为0,错误为1
[root@localhost shell]# test 5 -eq 3;echo $?
1
[root@localhost shell]# test 5 = 3;echo $?
1
[root@localhost shell]# test 5 != 3;echo $?
0
#使用[]演示
[root@localhost shell]# [ 6 -eq 5 ];echo $?
1
[root@localhost shell]# [ 6 < 5 ];echo $? #[]里不能使用< >符号
0
[root@localhost shell]# [ 6 -lt 5 ];echo $?
1
[root@localhost shell]# [ 6 = 5 ];echo $?
1
#使用[[]]演示 所以符号都能使用
[root@localhost shell]# [[ 5 -eq 6 ]];echo $?
1
[root@localhost shell]# [[ 5 -ne 6 ]];echo $?
0
[root@localhost shell]# [[ 5 != 6 ]];echo $?
0
[root@localhost shell]# [[ 5 -lt 6 ]];echo $?
0
[root@localhost shell]# [[ 5 -gt 6 ]];echo $?
1
#使用(()) 双括号里比较符可以不用空格
[root@localhost shell]# ((5>6));echo $?
1
[root@localhost shell]# ((5>4));echo $?
0
[root@localhost shell]# ((5!=5));echo $?
1
[root@localhost shell]# ((5==5));echo $?
0
[root@localhost shell]# ((5 == 5));echo $?
0
5. 逻辑操作符
注:命令
1 &&
命令
2
,如果命令
1
执行不成功,则命令
2
不执行。
命令
3 ||
命令
4
,如果命令
3
成功,不执行命令
4
;如果命令
3
不成功,则执行命令
4
示例演示:
[root@localhost shell]# [[ 6 > 5 && -e aa ]];echo $?
0
[root@localhost shell]# [[ 6 > 7 && -e aa ]];echo $?
1
[root@localhost shell]# [[ 6 > 5 || -e aa ]];echo $?
0
[root@localhost shell]# [[ 6 > 7 || -e aa ]];echo $?
0
[root@localhost shell]# [[ 6 > 7 || -e aahhhhhh ]];echo $?
1
[root@localhost shell]# ! [[ 5 > 4 ]];echo $?
1
[root@localhost shell]# ! [[ 5 > 7 ]];echo $?
0
小综合实验
1. 通过read读入两个整数,并比较他们的大小
1)用vim指令创建.sh脚本文件
[root@localhost shell]# vim compare_size.sh
#!/bin/bash
#########################
#File name:dd.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-17 16:27:37
#Description:
#########################
read -p "please input two integers:" num1 num2
[ -z "$num1" -o -z "$num2" ] && {
echo "please input 'two' integers"
exit 2
}
expr $num1 + 1 &>/dev/null
return_1=$?
expr $num2 + 1 &>/dev/null
return_2=$?
[ "$return_1" -eq 0 -a "$return_2" -eq 0 ] || {
echo "please input integers"
exit 1
}
echo "num1="$num1" : num2="$num2""
[ "$num1" -gt "$num2" ] && {
echo "num1 > num2"
exit 0
}
[ "$num1" -lt "$num2" ] && {
echo "num1 < num2"
exit 0
2)执行compare_size.sh脚本
[root@localhost shell]# bash compare_size.sh
3)输出结果
2. 假设执行一个可以携带参数的script,执行该脚本后屏幕会显示如下的数据
程序的文件名;共有几个参数;若参数的个数小于2个则告知用户参数数量太少;全部的参数内容;第一 个参数;第二个参数。
1)用vim指令创建.sh脚本文件
[root@localhost shell]# vim parameter.sh
#!/bin/bash
#########################
#File name:parameter.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-17 16:38:40
#Description:
#########################
echo "the script's filename: $0"
echo "the script parameter number is: $#"
[ "$#" -lt 2 ] && {
echo "the numbber of parameter is less than 2!!!!"
exit 0
}
echo "the script takes the following: $@ "
echo "first parameter is $1"
echo "the 2nd paraneter is $2"
2)执行parameter.sh脚本
bash parameter.sh 1 2 3 5
3)输出结果