bash使用 变量定义与使用、预定义变量、数组变量、变量计算、掐头去尾与内容替换、数字比较大小、字符串比较、判断文件夹是否存在、逻辑控制if/for/while/
1、变量定义与使用
定义变量 a=1
b=helloworld
c="hello 'zhangsan'"
使用变量:echo ${var}
使⽤$var 或 ${var}来访问变量。后者更为严谨。$var_x ${var}_x 是不同的。
[root@VM_0_14_centos ~]# a=1 [root@VM_0_14_centos ~]# echo $a 1 [root@VM_0_14_centos ~]# echo "$a" 1 [root@VM_0_14_centos ~]# b=helloworld [root@VM_0_14_centos ~]# echo $b helloworld [root@VM_0_14_centos ~]# echo "$b" helloworld [root@VM_0_14_centos ~]# echo "nihao: $b" nihao: helloworld [root@VM_0_14_centos ~]# echo nihao: $b nihao: helloworld [root@VM_0_14_centos ~]# c="hello 'zhangsan'" [root@VM_0_14_centos ~]# echo $c hello 'zhangsan' [root@VM_0_14_centos ~]# echo ${a} 1 [root@VM_0_14_centos ~]# echo ${c} hello 'zhangsan' [root@VM_0_14_centos ~]# echo c is : ${c} c is : hello 'zhangsan'
[root@VM_0_14_centos ~]# echo abc is : ${c}_${b}_${a}
abc is : hello 'zhangsan'_helloworld_1
2、预定义变量
当前目录
echo $PWD
用户名
echo $USER
家目录
echo $HOME
环境变量
echo $PATH
3、数组变量
数字
[root@VM_0_14_centos /]# a=(1 2 3 4 5 6) [root@VM_0_14_centos /]# echo ${a[0]}_${a[1]}_${a[5]} 1_2_6
字符加单引号
[root@VM_0_14_centos /]# c=('ls' 'ww' 'zs') [root@VM_0_14_centos /]# echo ${c[2]} zs
加双引号
[root@VM_0_14_centos /]# e=("a1" "a2" "a3" "b1") [root@VM_0_14_centos /]# echo ${e[0]}_${e[3]} a1_b1
不加引号
[root@VM_0_14_centos /]# d=(ls ww zs) [root@VM_0_14_centos /]# echo ${d[1]} ww
变量计算
数字
[root@VM_0_14_centos /]# a2=1
[root@VM_0_14_centos /]# ((a2=a2+3))
[root@VM_0_14_centos /]# echo $a2
4
[root@VM_0_14_centos /]# ai=1 [root@VM_0_14_centos /]# ((ai=ai+1));echo $ai 2
浮点数
begin不能写成小写
[root@VM_0_14_centos /]# awk 'BEGIN{print 1/3}' 0.333333
浮点数格式化
[root@VM_0_14_centos /]# awk 'BEGIN{printf("%0.2f\n",1/3)}' 0.33
取值
${value:offset}
从字符串value中取值,从第offset为开始取,下标从0开始
${value:offset:length}
从字符串value中取值,从第offset为开始取,取length个
${#value}
字符串长度
[root@VM_0_14_centos /]# str="abcdef123456" [root@VM_0_14_centos /]# echo ${str:5} f123456 [root@VM_0_14_centos /]# echo ${str:5:1} f [root@VM_0_14_centos /]# echo ${#str} 12
${#array[*]} ${#array[@]}
表⽰数组中元素的个数
[root@VM_0_14_centos /]# e=("a1" "a2" "a3" "b1") [root@VM_0_14_centos /]# echo ${#e[*]} 4 [root@VM_0_14_centos /]# echo ${#e[@]} 4
掐头去尾与内容替换
${value#pattern} ${value##pattern} #表⽰掐头
[root@VM_0_14_centos /]# echo ${str} abcdef123456 [root@VM_0_14_centos /]# echo ${str##*ef} 123456 [root@VM_0_14_centos /]# echo ${str} abcdef123456 [root@VM_0_14_centos /]# echo ${str#*ef} 123456
${value%pattern} ${value%%pattern} %表⽰去尾
[root@VM_0_14_centos /]# echo ${str} abcdef123456 [root@VM_0_14_centos /]# echo ${str%2*} abcdef1 [root@VM_0_14_centos /]# echo ${str%%2*} abcdef1
${value/pattern/string} ${value//pattern/string} /表⽰替换
[root@VM_0_14_centos /]# echo ${str}
abcdef123456
[root@VM_0_14_centos /]# echo ${str/*f/ooo} ooo123456 [root@VM_0_14_centos /]# echo ${str/f*/ooo} abcdeooo
[root@VM_0_14_centos /]# echo ${str/f/oo}
abcdeoo123456
awk截取字段--substr()
[root@VM_0_14_centos /]# xx="1234567" [root@VM_0_14_centos /]# echo $xx|awk '{print substr($0,2,3)}' 234 [root@VM_0_14_centos /]# echo $xx|sed 's#3#c#g' 12c4567
数字比较大小
❖ [ 2 –eq 2 ] 相等
❖ [ 2 –ne 2 ] 不等
❖ [ 3 –gt 1 ] ⼤于
❖ [ 3 –ge 3 ]⼤于等于
❖ [ 3 –lt 4 ] ⼩于
❖ [ 3 –le 3 ] ⼩于等于
0 真
1 假
[root@VM_0_14_centos ~]# [ 2 -eq 2 ];echo $? 0 [root@VM_0_14_centos ~]# [ 2 -eq 12 ];echo $? 1 [root@VM_0_14_centos ~]# [ 2 -ne 2 ];echo $? 1 [root@VM_0_14_centos ~]# [ 2 -ne 12 ];echo $? 0 [root@VM_0_14_centos ~]# [ 12 -gt 2 ];echo $? 0 [root@VM_0_14_centos ~]# [ 12 -ge 12 ];echo $? 0 [root@VM_0_14_centos ~]# [ 12 -lt 22 ];echo $? 0 [root@VM_0_14_centos ~]# [ 12 -le 12 ];echo $? 0
字符串比较
[ string1 = string2 ] 如果两字符串相同,则结果为真
[ string1 != string2 ] 如果两字符串不相同,则结果为真
[ -n "$var" ] 如果字符串不是空,则结果为真
[ -z "$var" ] 如果字符串是空,则结果为真
[[ "xxxx" == x* ]] 在表达式中表⽰0或者多个字符
[[ xxx == x?? ]] 在表达式中表⽰单个字符
[root@VM_0_14_centos ~]# [ abcdef = abcdef ];echo $? 0 [root@VM_0_14_centos ~]# [ abcdef = 123456 ];echo $? 1 [root@VM_0_14_centos ~]# [ abcdef != 123456 ];echo $? 0 [root@VM_0_14_centos ~]# str=abcdef [root@VM_0_14_centos ~]# [ -n "$str" ] [root@VM_0_14_centos ~]# [ -n "$str" ];echo $? 0 [root@VM_0_14_centos ~]# [ -n "$arr" ];echo $? 1 [root@VM_0_14_centos ~]# [ -z "$arr" ];echo $? 0
[root@VM_0_14_centos ~]# [[ abcd == a* ]] ;echo $? 0 [root@VM_0_14_centos ~]# [[ abcd == *c* ]] ;echo $? 0 [root@VM_0_14_centos ~]# [[ abcd == *d ]] ;echo $? 0 [root@VM_0_14_centos ~]# [[ abcd == a??d ]] ;echo $? 0 [root@VM_0_14_centos ~]# [[ abcd == a?? ]] ;echo $? 1 [root@VM_0_14_centos ~]# [[ abcd == a??? ]] ;echo $? 0
判断文件夹是否存在
-e file 如果⽂件存在,则结果为真
❖ -d file 如果⽂件是⼀个⼦⽬录,则结果为真
❖ -f file 如果⽂件是⼀个普通⽂件,则结果为真
❖ -r file 如果⽂件可读,则结果为真
❖ -s file 如果⽂件的长度不为0,则结果为真
❖ -w file 如果⽂件可写,则结果为真
❖ -x file 如果⽂件可执⾏,则结果为真
[root@VM_0_14_centos ~]# [ -e jitsi-videobridge ];echo $? 0 [root@VM_0_14_centos ~]# [ -d jitsi-videobridge ];echo $? 0 [root@VM_0_14_centos ~]# [ -f jitsi-videobridge ];echo $? 1 [root@VM_0_14_centos ~]# [ -r jitsi-videobridge ];echo $? 0 [root@VM_0_14_centos ~]# [ -s jitsi-videobridge ];echo $? 0 [root@VM_0_14_centos ~]# [ -w jitsi-videobridge ];echo $? 0 [root@VM_0_14_centos ~]# [ -x jitsi-videobridge ];echo $? 0
&&用法
如果前面判断为0,就执行&&后面语句
如果前面判断为1,就执行 || 后面语句
逻辑控制
if 条件
if [ condition ] ; then …;fi
[root@VM_0_14_centos ~]# if [ 2 -gt 1 ];then echo '2>1';fi 2>1
if [ condition ] ; then …;else …;fi
[root@VM_0_14_centos ~]# if [ 2 -eq 1 ];then echo '2=1';else echo '2!=1';fi 2!=1
if [ condition ] ; then …;elif …;fi
[root@VM_0_14_centos ~]# if [ 2 -eq 1 ];then echo '2=1';elif [ 2 -eq 2 ];then echo '2=2';else echo 'nonono';fi 2=2
分支 case
[root@VM_0_14_centos ~]# num=3 [root@VM_0_14_centos ~]# case $num in > 1) echo 'num==1';; > 2) echo 'num==2';; > 3) echo 'num==3';; > esac num==3
for 循环
root@VM_0_14_centos ~]# arr=(1 2 3 4)
[root@VM_0_14_centos ~]# for ((i=0;i<${#arr[*]};i++)); do echo ${arr[i]};done 1 2 3 4
For 遍历循环
[root@VM_0_14_centos ~]# for x in `ls` ;do echo $x ;done jitsi-videobridge jitsi-videobridge.git nginx-1.9.9.tar.gz [root@VM_0_14_centos ~]# ss="aa bb cc dd";for x in $ss;do echo $x ;done aa bb cc dd [root@VM_0_14_centos ~]# ss=(aa bb cc "sss dd");for x in "${ss[@]}";do echo $x ;done aa bb cc sss dd
While 循环
[root@VM_0_14_centos ~]# i=0;while [ $i -lt 3 ] ;do echo $i;((i=i+1));done 0 1 2
读取文件
[root@VM_0_14_centos test]# cat t01 1aaa 2s 0000 3ddd 4fff afff bfff cff df [root@VM_0_14_centos test]# while read rows;do echo $rows;done < t01 1aaa 2s 0000 3ddd 4fff afff bfff cff df
输出当前文件夹下的文件
[root@VM_0_14_centos jitsi-videobridge]# for f in * > do echo '' > if [ -f $f ] > then echo $f is file > else continue;fi;done