变量数值计算
(())命令
i=$((i+1)) #运算后赋值
echo $((i=i+1)) #输出最终的变量值
b=$((1+2**3-4%3)) b=8
自加:
[root@sf106232 ~] # echo $((a+=1)) 1 [root@sf106232 ~] # echo $((a+=1)) 2 [root@sf106232 ~] # echo $((a+=1)) 3 |
自减:
[root@sf106232 ~]#
echo
$((a+=1)) 4 [root@sf106232 ~]#
echo
$((a-=1)) 3 [root@sf106232 ~]#
echo
$((a-=1)) 2 |
判断:
[root@sf106232 ~]#
echo
$((10<1)) #错误输出0 0 [root@sf106232 ~]#
echo
$((10>1)) #正确输出1 1 [root@sf106232 ~]#
if
((8>7&&5==5)) ;then echo
yes ; fi yes [root@sf106232 ~]#
if
((8>7&&5==6)) ;then echo
yes ; else
echo no ; fi no |
a++
[root@sf106232 ~]# a=10 [root@sf106232 ~]#
echo
$((a++)) ##输出当前的值,然后自加 10 [root@sf106232 ~]#
echo
$((a++)) 11 |
a--
[root@sf106232 ~]#
echo
$((a++)) 13 [root@sf106232 ~]#
echo
$((a)) 14 [root@sf106232 ~]#
echo
$((a--)) 14 [root@sf106232 ~]#
echo
$((a--)) 13 |
传参的使用
[root@sf106232 ~]# sh test.sh 2 8 a+b=10 [root@sf106232 ~]# cat test.sh
#!/bin/bash
a= $1 b= $2 echo
"a+b=$(($a +$b))" |
计算器功能
[root@sf106232 ~]# sh test.sh 1+2 3 [root@sf106232 ~]# sh test.sh 1/2 0 [root@sf106232 ~]# sh test.sh 1%2 1 [root@sf106232 ~]# sh test.sh 3**2 9 [root@sf106232 ~]# cat test.sh
#!/bin/bash
echo
$(( $1 )) |
方法二:
#!/bin/bash
# echo
$(( $1 )) print_usage() { printf $ "USAGE: sh $0 NUM1 {+|-|*|/} NUM2\n" exit
1 } if
[ $# -ne 3 ] ## 传参个数为三为正确 then
print_usage fi firstnum= $1 secondnum= $3 op= $2 if
[ ! -n "`echo $firstnum|sed 's/[0-9]//g'`" ] ## 判断是否为整数, -n 是判断是否为空值。不为空值说明不是整数。 then print_usage fi if
[ "$op"
!= "+"
] && [ "$op"
!= "-"
]&& [ "$op"
!= "*"
] && [ "$op"
!= "/"
];then print_usage fi if
[ ! -n "`echo $secondnum|sed 's/[0-9]//g'`" ];then ## 判断是否为整数, -n 是判断是否为空值。不为空值说明不是整数。 print_usage fi echo
"${firstnum}${op}${secondnum}=$((${firstnum}${op}${secondnum}))" |
执行结果:
[root@sf106232 ~]# sh test.sh 2 \* 4 2*4=8 [root@sf106232 ~]# sh test.sh 2 + 4 2+4=6 [root@sf106232 ~]# sh test.sh 2 - 4 2-4=-2 [root@sf106232 ~]# sh test.sh 2 / 4 2/4=0 |
let的用法
[root@sf106232 ~]# i=2 [root@sf106232 ~]# i=i+8 [root@sf106232 ~]#
echo
$i i+8 [root@sf106232 ~]# unset i [root@sf106232 ~]# i=2 [root@sf106232 ~]# let i=i+8 [root@sf106232 ~]#
echo
$i 10 |
expr用法
expr 14 % 9 # 整数运算 SUM=`expr 2 \* 3` # 乘后结果赋值给变量 LOOP=`expr
$LOOP
+ 1` # 增量计数(加循环即可) LOOP=0 expr length
"bkeep zbb"
# 计算字串长度 expr
substr
"bkeep zbb" 4 9 # 抓取字串 expr index
"bkeep zbb"
e # 抓取第一个字符数字串出现的位置 expr 30 / 3 / 2 # 运算符号有空格 expr bkeep.doc :
'.*'
# 模式匹配(可以使用expr通过指定冒号选项计算字符串中字符数) expr bkeep.doc :
'\(.*\).doc'
# 在expr中可以使用字符串匹配操作,这里使用模式抽取.doc文件附属名 |
例子:
expr $i + 6 &>/dev/null
[root@sf106232 ~]# cat 1111.sh
#!/bin/bash
expr
$1
+ 1 >/dev/null 2>&1 [ $? -eq 0 ] &&
echo
int|| echo
chars #返回值为0输出int 否则输出chars [root@sf106232 ~]# sh 1111.sh 3 int [root@sf106232 ~]# sh 1111.sh hh chars [root@sf106232 ~]# sh 1111.sh test
chars |
例子2:
持续判断:
[root@sf106232 ~]# cat 11111.sh
#!/bin/bash while
true do read -p
"please input:"
a expr
$a
+ 0 >/dev/null 2>&1 [ $? -eq 0 ] &&
echo
int || echo
chars done |
结果:
[root@sf106232 ~]# sh 11111.sh
please input:12 int please input:32 int please input:test chars please input:34 int please input:34.4 chars |
例子3
判断文件扩展名是否符合要求
#!/bin/bash
if expr "$1" : ".*\.pub" &>/dev/null
then
echo "you are using $1"
else
echo "please use *.pub file"
fi
测试:
root@sf106232 ~]# sh j.sh id_dsa
please use *.pub file
[root@sf106232 ~]# sh j.sh id_dsa.pub
you are using id_dsa.pub
[root@sf106232 ~]# sh j.sh .pub
you are using .pub
利用expr计算字符串的长度
[root@sf106232 ~]# expr length "$char"
7
[root@sf106232 ~]# echo ${#char}
7
[root@sf106232 ~]# echo ${char}|wc -L
7
打印语句中字符数不大于6的单词。
i like cool36o.org this is test hellohello
[root@sf106232 ~]# cat j1.sh
for n in i like cool36o.org this is test hellohello
do
if [ `expr length "$n"` -le 6 ]
then
echo $n
fi
done
结果:
[root@sf106232 ~]# sh j1.sh
i
like
this
is
test
方法2:
for n in i like cool36o.org this is test hellohello
do
if [ `echo ${n} |awk '{print length($0)}'` -le 6 ] ##awk中的length函数计算字符串的长度。
then
echo $n
# printf $n "\n"
fi
结果是一样的。
read
read:shell变量输入read命令
-p :设置提示信息
-t : 设置输入等待时间,默认为s
例子:
[root@sf106232 ~]# read -t 10 -p
"this is a test:"
a1 a2 ##超过10s时退出 this is a test:11 22 [root@sf106232 ~]#
echo
$a1 11 [root@sf106232 ~]#
echo
$a2 22 [root@sf106232 ~]#
|
例子2:加减乘除
这个例子思路还是不错的。
[root@sf106232 script]# cat read_size02.sh
#!/bin/bash a= $1 b= $2 Usage() { echo
$ "USAGE:sh $0 num1 num2" exit
1 } if
[ $# -ne 2 ] ; then Usage
fi expr
$a
+ 1 >/dev/null 2>&1 [ $? -ne 0 ] && Usage expr
$b
+ 1 >/dev/null 2>&1 [ $? -ne 0 ] && Usage echo
"a-b=$(($a-$b))" echo
"a+b=$(($a+$b))" echo
"a*b=$(($a*$b))" echo
"a**b=$(($a**$b))" if
[ $b
-eq 0 ] then echo
"$b is not allow to run" echo
"a/b=error" echo
"a%b=error" else echo
"a/b=$(($a/$b))" echo
"%b=$(($a%$b))" fi |
结果:
[root@sf106232 script]# sh read_size02.sh 1 2 a-b=-1 a+b=3 a*b=2 a**b=1 a/b=0 %b=1 [root@sf106232 script]# sh read_size02.sh 1 0 a-b=1 a+b=1 a*b=0 a**b=1 0 is not allow to run a/b=error a%b=error [root@sf106232 script]# sh read_size02.sh
USAGE:sh read_size02.sh num1 num2 |
shell脚本条件测试
test条件测试
-c<文件>:如果文件为一个字符特殊文件,则为真;
-d<文件>:如果文件为一个目录,则为真;
-e<文件>:如果文件存在,则为真;
-f<文件>:如果文件为一个普通文件,则为真;
-g<文件>:如果设置了文件的SGID位,则为真;
-G<文件>:如果文件存在且归该组所有,则为真;
-k<文件>:如果设置了文件的粘着位,则为真;
-O<文件>:如果文件存在并且归该用户所有,则为真;
-p<文件>:如果文件为一个命名管道,则为真;
-r<文件>:如果文件可读,则为真;
-s<文件>:如果文件的长度不为零,则为真;
-S<文件>:如果文件为一个套接字特殊文件,则为真;
-u<文件>:如果设置了文件的SUID位,则为真;
-w<文件>:如果文件可写,则为真;
-x<文件>:如果文件可执行,则为真。
[root@sf106232 script]# test -f test.sh1 && echo
true || echo
false #判断文件是否存在,存在输出true,不存在输出false. false [root@sf106232 script]# test -f test.sh && echo
true || echo
false true [root@sf106232 script]# test -f test.sh &&
echo
1 1 [root@sf106232 script]# test -f test.sh1 ||
echo
0 0 字符串测试 [root@sf106232 script]# test -z
""
&& echo
1 || echo
0 #被测试的字符串为空,成立,返回值为1. 1 [root@sf106232 script]# test -z
"test"
&& echo
1 || echo
0 0 |
[[]]
[root@sf106232 script]# [[
exec .sh -ot play.sh ]] && echo
1 || echo
0 ## exec .sh比play.sh旧,输出1 sb,sdd [root@sf106232 script]# [[
exec .sh -nt play.sh ]] && echo
1 || echo
0 ## exec .sh比play.sh新,输出1,否则输出0 shauige |
特殊条件测试表达式
[ 条件1 ]&& {
命令1
命令2
....
}
[[ 条件1 ]]&& {
命令1
命令2
....
}
test 条件1 && {
命令1
命令2
....
}
例子:
[root@sf106232 script]# cat 6_29.sh
[ -f /etc ] || { echo
1 echo
2 echo
3 } [root@sf106232 script]#
[root@sf106232 script]# sh 6_29.sh
1 2 3 |
字符串测试表达式
-z "字符串" ###字符串长度为0 , 为真。
"串1"="串2" 为真。or "串1"=="串2" 为真。
"串1"!="串2" 为真。
注释:字符串的比较需加"".
[root@sf106232 script]# [ -n
"abc"
] && echo
1 || echo
0 1 [root@sf106232 script]# [ -n
""
] && echo
1 || echo
0 0 [root@sf106232 script]# [ -z
"abc"
] && echo
1 || echo
0 0 [root@sf106232 script]# [ -z
""
] && echo
1 || echo
0 1 [root@sf106232 script]# [
"abc"
= "abc"
] || echo
0 && echo
1 1 [root@sf106232 script]# [
"abc"
= "abcd"
] && echo
1 || echo
0 0 [root@sf106232 script]# [
"abc"
== "abc"
] && echo
1 || echo
0 1 [root@sf106232 script]# [
"abc"
!= "abc"
] && echo
1 || echo
0 0 [root@sf106232 script]# [
"abc"
!= "abcd"
] && echo
1 || echo
0 1 |
逻辑操作符
-a and 两端都为真,结果为真。 ## [[]] &&
-o or 两端有一个为真 ,结果为真 ## [[]] ||
! not 两端相反,结果为真 ## [[]] !
例子:
[root@sf106232 script]# [ -f /etc/hosts -a -f /etc/services ] &&
echo
1 || echo
0 1 [root@sf106232 script]# [ -f /etc/hosts && -f /etc/services ] &&
echo
1 || echo
0 -bash: [: missing `]' 0 [root@sf106232 script]# [[ -f /etc/hosts && -f /etc/services ]] &&
echo
1 || echo
0 1 [root@sf106232 script]# [ 5 -eq 6 -o 5 -gt 3 ] &&
echo
1 || echo
0 1 [root@sf106232 script]# [ 5 -eq 6 -o 5 -gt 6 ] &&
echo
1 || echo
0 0 [root@sf106232 script]# (( 5==6 || 5>3 )) &&
echo
1 || echo
0 1 [root@sf106232 script]# (( 5==6 || 5<3 )) &&
echo
1 || echo
0 0 |
例子2:
- [root@sf106232 script]# for n in `ls /etc/init.d/*`;do egrep -wn "\[\[ " $n && echo $n ; done
- 274: if [[ "$OS_VER" < "10.5.0" ]]
- /etc/init.d/elasticsearch
- 560: if [[ "$x" =~ "^/dev/" ]] ; then
- /etc/init.d/functions
- 144: if [[ "$dst" =~ "^/dev/mapper" ]] \
- /etc/init.d/halt
- 41: if [[ "$rootopts" =~ "_netdev" ]] ; then
- /etc/init.d/iscsi
- 51: if [[ "$rootopts" =~ "_netdev" ]] ; then
- /etc/init.d/iscsid
- 78: if ! [[ "$SYSLOGADDR" =~ "^([0-9]{1,3}\.){3}[0-9]{1,3}$" ]]; then
- /etc/init.d/netconsole
- 185: if [[ "$rootfs" =~ "^nfs" ]] || [[ "$rootopts" =~ "_netdev|_rnetdev" ]] ; then
- /etc/init.d/network
例子3:
输入一个数,等于1就打印1 ,等于2就打印2 ,否则就提示错误,退出。
[root@sf106232 script]# sh 6_29.sh
please input a char:1 1 [root@sf106232 script]# sh 6_29.sh
please input a char:2 2 [root@sf106232 script]# sh 6_29.sh
please input a char:3 error [root@sf106232 script]# cat 6_29.sh
#!/bin/bash echo
-n "please input a char:" read
var [
"$var"
== "1"
] && { echo
1 exit
0 } [
"$var"
== "2"
] && { echo
2 exit
0 } [
"$var"
!= "1"
-a "$var"
!= "2"
] && { echo
error exit
1 } |
[root@sf106232 script]# sh 6_29.sh
error [root@sf106232 script]# sh 6_29.sh 1 1 [root@sf106232 script]# sh 6_29.sh 2 2 [root@sf106232 script]# cat 6_29.sh
#!/bin/bash # echo
-n "please input a char:" #read
var var = $1 [
"$var"
== "1"
] && { echo
1 exit
0 } [
"$var"
== "2"
] && { echo
2 exit
0 } [
"$var"
!= "1"
-a "$var"
!= "2"
] && { echo
error exit
1 } |
例子4:
[root@sf106232 script]# cat 6_29_1.sh #!/bin/bash read -p
"please input two num:"
a b #1 [ -z
"$a"
] || [ -z "$b"
] && { echo
" please input two num again." exit
1 } #2 expr
$a
+ 10 >/dev/null 2>&1 RETVAL1=$? expr
$b
+ 10 >/dev/null 2>&1 RETVAL2=$? test
$RETVAL1
-eq 0 -a $RETVAL2
-eq 0 || { echo
"pls two " num " again." exit } #3 [
$a
-eq $b
] && { echo
"$a = $b" exit
0 } [
$a
-gt $b
] && { echo
"$a > $b " exit
0 } [
$a
-lt $b
] && { echo
"$a < $b " exit
0 } 结果: [root@sf106232 script]# sh 6_29_1.sh
please input two num:23.2 23. 3 pls two num again. [root@sf106232 script]# sh 6_29_1.sh
please input two num:23 .^H^H^H^H^H^H pls two num again. [root@sf106232 script]# sh 6_29_1.sh
please input two num:23.1 23 pls two num again. [root@sf106232 script]# sh 6_29_1.sh
please input two num:23 12 23 > 12
|
写法2:
[root@sf106232 script]# cat 6_29_1.sh
#!/bin/bash #read -p
"please input two num:"
a b #1 a= $1 b= $2 [ $# -ne 2 ] && { echo
"USAGE:$0 NUM1 NUM2" exit
1 } #2 expr
$a
+ 10 >/dev/null 2>&1 RETVAL1=$? expr
$b
+ 10 >/dev/null 2>&1 RETVAL2=$? test
$RETVAL1
-eq 0 -a $RETVAL2
-eq 0 || { echo
"pls two " num " again." exit } #3 [
$a
-eq $b
] && { echo
"$a = $b" exit
0 } [
$a
-gt $b
] && { echo
"$a > $b " exit
0 } [
$a
-lt $b
] && { echo
"$a < $b " exit
0 } 结果: [root@sf106232 script]# sh 6_29_1.sh 12 USAGE:6_29_1.sh NUM1 NUM2 [root@sf106232 script]# sh 6_29_1.sh 12 13 12 < 13
[root@sf106232 script]# sh 6_29_1.sh 12 13.1 pls two num again. [root@sf106232 script]# sh 6_29_1.sh 13 12 13 > 12
|
利用判断表达式展示菜单操作
[root@sf106232 script]# cat 6_29_2.sh
#!/bin/bash #***************************************************** # Author: suixiaofeng # blog:http: //blog.cool360.org # Email: 258818040@qq.com
# Last modified: 2017-06-29 20:43 # Filename: 6_29_2.sh # Description:
#**************************************************** cat <<
END 1.panxiaoting 2.gongli 3.fanbinbing END read -p
"Which do you like? input the num:"
a [
"$a"
= "1"
] && { echo
"I guess ,you like panxiaoting" exit
0 } [
"$a"
= "2"
] && { echo
"I guess ,you like gongli " exit
0 } [
"$a"
== "3"
] && { echo
" I guess , you like fangbinbing" exit
0 } [[ !
"$a"
=~ [1-3] ]] && { echo
" I guess,you are not man." exit
0 } [root@sf106232 script]# sh 6_29_2.sh
1.panxiaoting 2.gongli 3.fanbinbing Which
do
you like? input the num:3 I guess , you like fangbinbing [root@sf106232 script]# sh 6_29_2.sh
1.panxiaoting 2.gongli 3.fanbinbing Which
do
you like? input the num:4 I guess,you are not man. |
模拟安装lnmp环境
[root@sf106232 script]# cat 6_29_4.sh
#!/bin/bash #***************************************************** # Author: suixiaofeng # blog:https: //blog.cool360.org # Email: 258818040@qq.com
# Last modified: 2017-06-29 21:08 # Filename: 6_29_4.sh # Description:
#**************************************************** path=/u02/script [ ! -d
"$path"
] && { mkdir
-p "$path" } cat <<
END 1.[install lamp] 2.[install lnmp] 3.[ exit ] pls input the num you chiose: 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
"start installing lamp." sleep 2; [ -x
"$path/lamp.sh"
] || { echo
"$path/lamp.sh does not exist or can not be exec." exit
1 } $path /lamp.sh exit
$? } [
$num
-eq 2 ] && { echo
"start installing lnmp." sleep 2; [ -x
"$path/lnmp.sh"
] || { echo
"$path/lnmp.sh does not exist or can not be exec." exit
2 } $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 } 结果: [root@sf106232 script]# sh 6_29_4.sh
1.[install lamp] 2.[install lnmp] 3.[ exit ] pls input the num you chiose: 2 start installing lnmp. start install lAmp.. [root@sf106232 script]# sh 6_29_4.sh
1.[install lamp] 2.[install lnmp] 3.[ exit ] pls input the num you chiose: 3 bye. [root@sf106232 script]# sh 6_29_4.sh
1.[install lamp] 2.[install lnmp] 3.[ exit ] pls input the num you chiose: 4 the num you input must be {1|2|3} Input ERROR !
|
echo " I guess,you are not man."
exit 0
}