#示例1.精密算术操作高级运算工具:bc计算命令 #可以对于0.00123456789进行计算(前面的都不能进行浮点运算): echo"1.212*3" | bc #scale设定小数精度(数值范围),"scale保存的小数位数;表达式"|bc #有效数字 echo"scale=3;4/8"| bc echo"sqrt(10)" | bc
#示例4.数组的提取 array_name=( [0]=one [1]=two [2]=three [3]=four ) ${array[@]:0}#显示所有元素 ${array[@]:1}#除去第一个元素后的显示所有元素 echo${array[@]:0:2}#从下标[0]开始依次显示2个数组值 one two echo${array[@]:1:2}#从下标[1]开始依次显示2个数组值 two three
#示例5.数组子串替换 echo${array[@]/o/m}#将o替换成m mne twm three fmur #如果没有指定替换子串,则删除匹配到的子符 echo${array[@]//o/}#ne tw three fur #替换字符串前端(#)子串 echo${array[@]/#o/k}#kne two three four #替换字符串后端(%)子串 echo${array[@]/%o/k}#ne twk three four
#示例5.子串删除、剔除 #左边开始最短的匹配:"t*e",这将匹配到"thre"则显示e echo${array[@]#t*e}# 可采用通配符 one two e four #左边开始最长的匹配,这将匹配到"three",将删除three并输出到屏幕 echo${array[@]##t*e}
# 示例6.子串删除、剔除 #从字符串的结尾开始最短的匹配o #右边开始最短的匹配 echo${array[@]%o}#one tw three four #右边开始最长的匹配 echo${array[@]%%o}#one tw three four #从字符串的结尾开始最长的匹配
#示例7.关联数组的使用,关联数组的打印方法跟普通数组用法一样 array_var=( [one]=one-1 [two]=two-2 [three]=three-3 [four]=four-4 [five]=five-5 [six]=six-6 ) #列出数组索引值(值得学习) echo${!array_var[*]}#four one five six two three (注意感叹号)
#示例1.输入出数组中各个参数,采用$array[*]与$array[@]都差不多; echo"#示例1.案例输出" count=0 for i in${number[@]};do flag=$((count++)) echo"number[${flag}] = $i" done count=0 for i in${string[*]};do flag=$((count++)) echo"string[${flag}] = $i" done
#示例3:提取数组 提取数组元素:3 4 , baby ! 提取从下标[0]开始依次显示2个数组:1 2 , I Love
#示例4:替换删除 将string数组中o变成大写O:I LOve yOu baby ! 将string数组中o删除:I Lve yu baby ! 前匹配替换: I Love You baby ! 删除:I Love ou baby ! 后匹配替换: I Love You baby ! 删除:I Love ou baby !
#示例5:通配符匹配 左边最短匹配:I ve u baby ! ,最长匹配:I Love baby ! 右边最短匹配:I Love you baby ! ,最长匹配:I Love baby !
#!/bin/bash #变量替换测试代码 var=1024 echo"1 - Value of var is ${var}"${var:-"Variable is not set"}# 1 - Value of var is 1024 1024 echo"2 - Value of var is ${var}"${var:+"Variable is not set"}# 2 - Value of var is 1024 Variable is not set echo"3 - Value of var is ${var}"${var:="Variable is not set"}# 2 - Value of var is 1024 1024 echo"4 - Value of var is ${var}"${var:?"Variable is not set"}# 2 - Value of var is 1024 1024
echo"1 - Value of var is $var"${var-"This is default value"}# 1 - Value of var is 1024 1024 echo"2 - Value of var is $var"${var+"This is default value"}# 2 - Value of var is 1024 This is default value echo"3 - Value of var is $var"${var?"This is default value"}# 2 - Value of var is 1024 1024 echo"4 - Value of var is $var"${var="This is default value"}# 4 - Value of var is 1024 1024
unset var echo"4 - Value of var is $var"${var:+"【This is default value】"}# var未定义,将不会返回 This is default value 字符串,定义将返回字符串 # 4 - Value of var is echo"5 - Value of var is "${var:?"【Print this message】"}# var未定义,会返回 Print this message 字符串,定义将返回其变量 # -bash: var: Value of var is 【Print this message】
0x06 shell编程变量测试
描述:变量测试时shell编程中最重要的一个环节,可以更据条件进行执行相应的代码;Shell中的 test 命令 或者 [[ ]] 用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试。
#示例4.str检测字符串是否为空,不为空返回 true。 read a if [ $a ];then echo"$a : string is not empty" else echo"$a : string is empty" fi #abc : string is not empty
#示例5.判断软硬链接 [ /root/student.txt -ef /tmp/su.txt ] && echo"true" || echo"false"#false #[[]] 必须有空格 = test #是否是文件,文件是否存在 $[[ -f 1.txt ]]&& echo"1.txt is file" || echo"1.txt is notfile" 1.txt is file
#是否是可执行文件 [[ -x 1.txt ]]&& echo"1.txt can be execute" || echo"1.txt can't be execute" #1.txt can't be execute $chmod +x 1.txt [[ -x 1.txt ]]&& echo"1.txt can be execute" || echo"1.txt can be execute" #1.txt can be execute
#是否是目录 $[[ -d 1.txt ]] && echo"1.txt is dir" || echo"1.txt is't dir" #1.txt is't dir $[[ -d /bin ]] && echo"1.txt is dir" || echo"1.txt is't dir" #1.txt is dir
方式1.请访问本博主的B站【WeiyiGeek】首页关注UP主, 将自动随机获取解锁验证码。
Method 2.Please visit 【My Twitter】. There is an article verification code in the homepage.
方式3.扫一扫下方二维码,关注本站官方公众号
回复:验证码
将获取解锁(有效期7天)本站所有技术文章哟!