1.shell的运算符有哪些
算术运算符:+,-,*,/,%,*
赋值运算符:=,+=,-=,/=,*=,%=
比较运算符:>,>=,<,<=, !=,=
逻辑运算符: &&,|,!
位运算符:&, l , ~, ^,>>,<<
自增自减:++i,i++,--i,i--
2.数学计算的命令有哪些,并举例使用
1、(())
[root@servera bash]# echo $(( 3+4 ))
72、expr
示例
[root@servera bash]# expr 3 '+' 4
73、let
[root@servera bash]# let a=3+4;echo $a
74、bc
方式一:[root@servera bash]# echo '3+4'| bc
7
方式二:[root@servera bash]# bc
3+4
7
^C
(interrupt) Exiting bc.
[root@servera bash]#5、$[]
[root@servera bash]# echo $[ 3+4 ]
76、awk
[root@servera bash]# echo '' | awk '{ print 4 + 3 }'
77、declare
[root@servera bash]# declare -i a=4+3 | echo $a
7
3.${}中的使用,并举例包含(#, ##,%,%%,:, /, //),并举例使用
先定义变量
[root@servera bash]# data=`cat /etc/passwd | grep syc`
[root@servera bash]# echo $data
syc:x:1001:1001::/home/syc:/bin/bash
示例
1、${} #返回变量的内容
[root@servera bash]# echo ${data}
syc:x:1001:1001::/home/syc:/bin/bash2、${#} #返回变量的字符个数
[root@servera bash]# echo ${#data}
363、${data:offset:length} # 截取字符串
[root@servera bash]# echo ${data:5:10}
:1001:1001
Tips:顺着取得时候,下标从0开始4、${#}
[root@servera bash]# echo ${data#*:} # 从前往后,最短匹配,删除第一个冒号之前的字符
x:1001:1001::/home/syc:/bin/bash5、${##}
[root@servera bash]# echo ${data##*:} # 从后往前,最长匹配,删除第一个冒号之前的所有字符
/bin/bash6、${:}
[root@servera bash]# echo ${data:5} # 从下标为5的位置截取至结尾
:1001:1001::/home/syc:/bin/bash
[root@servera bash]# echo ${data:5:10} # 从下标5的位置开始往后截取10个
:1001:1001
[root@servera bash]# echo ${data::-4} # 从后往前删除4个字符,保留其余
syc:x:1001:1001::/home/syc:/bin/
[root@servera bash]# echo ${data: -1} # 从后往前删除1个字符,保留其余
h7、${%}
[root@servera bash]# echo ${data%:*} # 从后往前,最短匹配,删除第一个冒号之后的
syc:x:1001:1001::/home/syc8、${%%}
[root@servera bash]# echo ${data%%:*} # 从前往后,最长匹配,删除第一个冒号之后的所有字符
syc9、${/}
[root@servera bash]# echo ${data/h/H} # 只替换第一个匹配到的
syc:x:1001:1001::/Home/syc:/bin/bash10、${//}
[root@servera bash]# echo ${data//h/H} # 替换所有的h为大写H
syc:x:1001:1001::/Home/syc:/bin/basH
4.条件测试的语法 (()), [[]],[],test,功能以及语法格式
1、((<算术表达式>))
功能:整数运算,针对于整数的比较运算符
[root@servera bash]# echo $(( 1 > 3 ))
02、[[ <测试表达式> ]]
格式:
功能:条件测试,可以使用逻辑运算符&,||
[root@servera bash]# [[ -f year.sh && 1 < 0 ]];echo $?
13、[]
格式:[ <测试表达式> ]
功能:条件测试,逻辑运算符&&,||不能使用,用-a和-o分别代替&&和||
[root@servera bash]# [ -f year.sh ];echo $?
04、test
功能:条件测试
格式: test <测试表达式>
[root@servera bash]# test -f year.sh ; echo $?
0
5.通过read读入两个整数,并比较他们的大小
#!/bin/bash
read -p "Please input int_number,num_one and num_two:" num_one num_two
if [ -z $num_one -o -z $num_two ];then
echo "Please input two number"
exit 1
fiexpr $num_one + 10 &> /dev/null # +10判断它是否是数字
return_num_one=$?
expr $num_two + 10 &> /dev/null
return_num_two=$?if [ "$return_num_one" -ne 0 -a "$return_num_two" -ne 0 ];then
echo "Please input two number"
exit 2
fiif [ $num_one -lt $num_two ];then
echo "$num_one < $num_two"
exit 0
elif [ $num_one -gt $num_two ];then
echo "$num_one > $num_two"
exit 0
elif [ $num_one -eq $num_two ];then
echo "$num_one == $num_two"
exit 0
fi测试
[root@servera bash]# bash compare_size_test.sh
Please input int_number,num_one and num_two:
Please input two number
[root@servera bash]# bash compare_size_test.sh
Please input int_number,num_one and num_two:6 6
6 == 6
[root@servera bash]# bash compare_size_test.sh
Please input int_number,num_one and num_two:9 3
9 > 3
[root@servera bash]# bash compare_size_test.sh
Please input int_number,num_one and num_two:3 9
3 < 9
6.假设执行一个可以携带参数的script,执行该脚本后屏幕会显示如下的数据
程序的文件名;共有几个参数;若参数的个数小于2个则告知用户参数数量太少;全部的参数内容;第一个参数;第二个参数。
#!/bin/bash
echo "================================"
echo "the script filename is $0"
echo "================================"
if [ $# -lt 2 ];then
echo "parameter number is too less"
exit 2
fiecho "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@servera bash]# bash parameter_test.sh 2 3
================================
the script filename is parameter_test.sh
================================
The total number of parameters is 2
your whole parameter is '2 3'
================================
your whole parameter is '2 3'
the 1st parameter is 2
the 2nd parameter is 3
不带参数
[root@servera bash]# bash parameter_test.sh
================================
the script filename is parameter_test.sh
================================
parameter number is too less