1.shell的运算符有哪些?
shell的运算符:
+、- 求和、差
*、/、% 求乘积、商、余数
** 幂运算,例如3**3是求3的立方。
+=、-=、 例如a+=1相当于 a=a+1
*=、/=、%=
++x、 --x 先将变量x的值加1,再赋值给x;先将变量x的值减1.然后再赋值给x
x++、x-- 先使用x的值,再将该变量的值加1;先使用x的值,然后再将该变量减1
位运算符>>、 位运算符通常出现在整数间,但是针对的也不是整个整数,而是二进制表示形式
<< 中的某些位(bit)。例如 2>>1是将二进制形式的2(10) 右移一位 变为 1
4<<2 即就是:100向左边移2位 结果为16
&、|、~、^ 二进制内(与运算&: (同一取一) 或运算|:(有1取一))
取反(按位取反):~ 直接取相反数 异或:^ (相同取1,不同取0)
>>=、<<= 将变量的值左移指定位数之后重新赋给该变量,x<<=3,将x的值左移3位,重新 赋给变量x;将变量的值右移指定位数之后重新赋给该变量,x>>=4,将变量x的 值右移4位后重新赋给变量x。
&= 、|=、^= 将变量的值与指定的数值按位与之后重新赋给该变量,x&=8,将变量x的值与8 按 位与运算之后重新赋给变量x; 将变量的值与指定的数值按位或之后重新赋给 该变量,x|=7,将变量x的值与7执行按位或运算之后重新赋给变量x; 将变量的 值与指定的数值按位异或之后重新赋给该变量,x^=9,将变量x的值与9执行按 位 异或运算之后重新赋给变量x。
运算操作符和运算命令:
作用 说明
(()): 用于整数运算的常用运算符。 在(())中使用变量时可以去掉变量前的$符号
let: 用于整数运算 使用let命令可以执行一个或多个算术表达式,其中 的变量名不用$符号。
expr: 可用于整数运算,但还有其他功能。 使用expr时,运算符及用于计算的数字左右都至少 有一个空格,否则 会报错;使用乘号时,必须使用 反斜线屏蔽其特定含义;使用expr做 计算,将一个 未知的变量和一个已知的整数相加,看返回码是否 为 0,如果为0就认为做加法的变量为整数,否则就 不是整数。
[root@localhost test]# char="we are young"
[root@localhost test]# expr length "$char"
12
bc:linux下的一个计算器程序(适合整数和小数运算)
例如: [root@localhost test]# echo "15+5"|bc
20
$[] : 这个也可以计算整数; 例如: [root@localhost test]# r=$[8+19]
[root@localhost test]# echo $r
27
awk: 也可以用来计算整数 例如:[root@localhost test]# awk 'BEGIN { print 9+2*8}'
25
这个是可以计算小数的:
[root@localhost test]# echo "6.88882 3.88882" |awk '{print ($1-$2)}'
3
declare :定义变量值和属性
- i参数可以用于定义整形变量,
做运算 例如:[root@localhost test]# declare -i n=23*2
[root@localhost test]# echo $n
46
2.数学计算的命令有哪些,并举例使用
使用expr计算字符:
[root@localhost test]# char="we are young"
[root@localhost test]# expr length "$char"
12
使用bc进行算术运算:
[root@localhost test]# echo "15+5"|bc
20
使用(())进行运算:
[root@localhost test]# r=$((1+4*2))
[root@localhost test]# echo $r
9
使用let命令进行运算:
[root@localhost test]# let q=12*23+2
[root@localhost test]# echo $q
278
使用declare命令进行运算:
例如:[root@localhost test]# declare -i n=23*2
[root@localhost test]# echo $n
46
使用awk进行算术运算:(包含小数)
第一个整数:
[root@localhost test]# awk 'BEGIN { print 9+2*8}'
25
第二个小数:
[root@localhost test]# echo "6.88882 3.88882" |awk '{print ($1-$2)}'
3
使用$[]进行整数运算:
[root@localhost test]# r=$[8+19]
[root@localhost test]# echo $r
27
3.${}中的使用,并举例包含(#, ##,%,%%,:, /, //),并举例使用
[root@localhost test]# str2="we are young"
#量化变量长度
[root@localhost test]# echo ${#str2}
12
#变量截取
#指定起始位置,一直到结束
${data:offset:length} # 截取字符串
[root@localhost test]# echo ${str2:1} 在变量${str2}中,从位置offset之后开始提取子串到结 尾
e are young
[root@localhost test]# echo ${str2::1} #指定长度,不指定起始位置默认从开头开始
w
[root@localhost test]# echo ${str2:2:5} #指定起始位置和长度
are
[root@localhost test]# echo ${str2:1-4:3} #从右边第几个字符开始,及字符的个数
ung
[root@localhost test]# echo ${str2:0-5} #输出右边的几个字符
young
[root@localhost test]# echo ${str2} #输出完整字符串
we are young
删除字符串:
[root@localhost test]# echo ${str2#we} 从变量${str2}开头开始删除最短匹配的word子串
are young
[root@localhost test]# str2="filename.tar.gz"
[root@localhost test]# echo ${str2##*:} # 从后往前,最长匹配,删除第一个点之前的所有字符
gz
${ } #返回变量的内容
[root@localhost test]# echo ${str2}
filename.tar.gz
[root@localhost test]# echo ${str2%.*} 从后往前,最短匹配,删除第一个点之后的
filename.tar
[root@localhost test]# echo ${str2%%.*} 从前往后,最长匹配,删除第一个点之后的所有字符
filename
[root@localhost test]# echo ${str2/a/A} 只替换第一个匹配到的
filenAme.tar.gz
[root@localhost test]# echo ${str2//a/A} 替换所有的a为大写A
filenAme.tAr.gz
4.条件测试的语法
(()), [[]],[],test,功能以及语法格式
1、((<算术表达式>))
功能:整数运算,针对于整数的比较运算符
[root@localhost test]# echo $(( 2 > 5 ))
0
2、[[ <测试表达式> ]]
格式:
功能:条件测试,可以使用逻辑运算符&,||
[root@localhost test]# [[ -f years.sh && 5 < 0 ]];echo $?
1
3、[]
格式:[ <测试表达式> ]
功能:条件测试,逻辑运算符&&,||不能使用,用-a和-o分别代替&&和||
[root@localhost test]# [ -f years.sh ];echo $?
0
4、test
功能:条件测试
格式: test <测试表达式>
[root@localhost test]# test -f years.sh ; echo $?
0
5.通过read读入两个整数,并比较他们的大小
[root@localhost test]# cat test_equal.sh
#!/bin/bash
#########################
#File name:test_equal.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-17 05:17:23
#Description:
#########################
read -p "please input two number:" x y
[ -z "$x" -o -z "$y" ] && {
echo "please input "TWO" number"
exit 2
}
expr $x + 10 &>/dev/null
return_x=$?
expr $y + 10 &>/dev/null
return_y=$?
[ "$return_x" -eq 0 -a "$return_y" -eq 0 ] || {
echo "please input two 'number'"
exit 3
}
[ "$x" -lt "$y" ] && {
echo "$x < $y"
exit 0
}
[ "$x" -eq "$y "] && {
echo "$x ==$y"
exit 5
}
[ "$x " -gt "$y" ] && {
echo "$x > $y"
echo 6
}
测试:
[root@localhost test]# bash test_equal.sh
please input two number:12 14
12 < 14
6.假设执行一个可以携带参数的script,执行该脚本后屏幕会显示如下的数据
#!/bin/bash
#########################
#File name:speak_test.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2022-08-17 06:08:16
#Description:
#########################
echo "**********************"
echo "the script indexname is $0"
echo "**********************"
if [ $# -lt 2 ];then
echo "parameter number is too less"
exit 4
fi
echo "the total number of parameters is $#"
echo "your whole parameter is '$@'"
echo "**********************"
echo "your whole parameter is '$@'"
echo "the 1st parameter is $1"
echo "the 2nd parameter is $2"
测试:
[root@localhost test]# bash speak_test.sh 1 3
**********************
the script indexname is speak_test.sh
**********************
the total number of parameters is 2
your whole parameter is '1 3'
**********************
your whole parameter is '1 3'
the 1st parameter is 1
the 2nd parameter is 3
不带参数:
[root@localhost test]# bash speak_test.sh
**********************
the script indexname is speak_test.sh
**********************
parameter number is too less
[root@localhost test]#