Bash脚本编程:变量、模式匹配、计算与流程控制
1. 变量赋值与模式匹配
1.1 变量赋值
当变量 BLAH 被重新定义但得到空值时,它仍然存在,只是此时没有值。例如,在代码 echo ${BLAH=value} 中,由于 BLAH 当时为空值,不会分配新值。而使用 echo ${BLAH:=value} 可以为 BLAH 分配新值。
1.2 模式匹配操作符
模式匹配操作符可用于在变量中搜索模式,并在找到模式时修改变量。以下是几种常见的模式匹配操作符及其用法:
- 从字符串开头查找最长匹配 :使用 ## ,例如:
#!/bin/bash
# script that extracts the file name from a filename that includes the complete path
# usage: stripit <complete file name>
filename=${1##*/}
echo "The name of the file is $filename"
执行该脚本:
sander@linux %> ./stripit /bin/bash
the name of the
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



