最近无意中再次查阅abs文档时,发现了bash 4 4.1 4.2新特性,清除了困扰多年不愿使用bash的阻碍。
在这里我只列出了自己关注的特性。
2009年2月发布的bash4:
1. 关联数组/Hash表
abs中的样例:
#创建
-
declare -A address
-
address[Charles]="414 W. 10th Ave., Baltimore, MD 21236"
-
address[John]="202 E. 3rd St., New York, NY 10009"
-
address[Wilma]="1854 Vermont Ave, Los Angeles, CA 90023"
-
-
#通过键提取值
-
echo "Charles's address is ${address[Charles]}."
-
# Charles
2. ;;& ;& 用于case语句中的连续匹配
;;& 继续匹配下一个模式条件,并根据结果执行相关语句。
;& 直接执行下一条语句,(我的理解是不进行模式匹配)。
-
例子:
-
-
test_char ()
-
{
-
case "$1" in
-
[[:print:]] ) echo "$1 is a printable character.";;& # |
-
# The ;;& terminator continues to the next pattern test. |
-
[[:alnum:]] ) echo "$1 is an alpha/numeric character.";;& # v
-
[[:alpha:]] ) echo "$1 is an alphabetic character.";;& # v
-
[[:lower:]] ) echo "$1 is a lowercase alphabetic character.";;&
-
[[:digit:]] ) echo "$1 is an numeric character.";& # |
-
# The ;& terminator executes the next statement ... # |
-
%%%@@@@@ ) echo "********************************";; # v
-
# ^^^^^^^^ ... even with a dummy pattern.
-
esac
-
}
3. bash新的协程语句 coproc
coproc,
协程可以命名,可以让主程获取协程的输入输出。由于协程是异步执行的,所以有可能会比主程和它交互之前结束运行。
-
coproc { cat mx_data.txt; sleep 2; }
-
# ^^^^^^^
-
# Try running this without "sleep 2" and see what happens.
-
while read -u ${COPROC[0]} line # ${COPROC[0]} is the
-
do #+ file descriptor of the coprocess.
-
echo "$line" | sed -e 's/line/NOT-ORIGINAL-TEXT/'
-
done
-
kill $COPROC_PID
4. 新的内置mapfile功能,使文件调入数组避免使用循环和替换语句。
-
mapfile Arr1 < $0
-
# Same result as Arr1=( $(cat $0) )
-
echo "${Arr1[@]}" # Copies this entire script out to stdout.
5. 参数变量描述符,使用大小写描述符可以指定变量内容的大小写情况。
-
#!/bin/bash4
-
var=veryMixedUpVariable
-
echo ${var} # veryMixedUpVariable
-
echo ${var^} # VeryMixedUpVariable
-
# * First char --> uppercase.
-
echo ${var^^} # VERYMIXEDUPVARIABLE
-
# ** All chars --> uppercase.
-
echo ${var,} # veryMixedUpVariable
-
# * First char --> lowercase.
-
echo ${var,,} # verymixedupvariable
-
# ** All chars --> lowercase.
6. declare 语句支持 -l 全部小写, -c 第一字符大写其他小写。
-
#!/bin/bash4
-
declare -l var1 # Will change to lowercase
-
var1=MixedCaseVARIABLE
-
echo "$var1" # mixedcasevariable
-
# Same effect as echo $var1 | tr A-Z a-z
-
declare -c var2 # Changes only initial char to uppercase.
-
var2=originally_lowercase
-
echo "$var2" # Originally_lowercase
-
# NOT the same effect as echo $var2 | tr a-z A-Z
7. 大括号扩展指令
-
#!/bin/bash4
-
echo {40..60..2}
-
# 40 42 44 46 48 50 52 54 56 58 60
-
# All the even numbers, between 40 and 60.
-
echo {60..40..2}
-
# 60 58 56 54 52 50 48 46 44 42 40
-
# All the even numbers, between 40 and 60, counting backwards.
-
# In effect, a decrement.
-
echo {60..40..-2}
-
# The same output. The minus sign is not necessary.
-
# But, what about letters and symbols?
-
echo {X..d}
-
# X Y Z [ ] ^ _ ` a b c d
-
# Does not echo the \ which escapes a space.
补零指令,在大括号中第一个位置,表示补零状态的初始值,
8. 位置参数的字符串提取,纠正了bash3的索引号未和位置参数索引对齐问题。
-
#!/bin/bash
-
# show-params.bash
-
# Requires version 4+ of Bash.
-
# Invoke this scripts with at least one positional parameter.
-
-
E_BADPARAMS=99
-
if [ -z "$1" ]
-
then
-
echo "Usage $0 param1 ..."
-
exit $E_BADPARAMS
-
fi
-
echo ${@:0}
-
# bash3 show-params.bash4 one two three
-
# one two three
-
# bash4 show-params.bash4 one two three
-
# show-params.bash4 one two three
-
# $0 $1 $2 $3
9. 新的通配符操作,可以对文件名或目录名,进行递归匹配。
-
#!/bin/bash4
-
# filelist.bash4
-
shopt -s globstar # Must enable globstar, otherwise ** doesn
10. 新的$BASHPID全局内部变量
11. 新的出错处理插入函数
-
#!/bin/bash4
-
command_not_found_handle ()
-
{ # Accepts implicit parameters.
-
echo "The following command is not valid: \""$1\"""
-
echo "With the following argument(s): \""$2\"" \""$3\""" # $4, $5 ...
-
} # $1, $2, etc. are not explicitly passed to the function.
-
bad_command arg1 arg2
-
# The following command is not valid: "bad_command"
-
# With the following argument(s): "arg1" "arg2"
Bash 4.1 2010 5月
1. printf 函数开始支持,-v 参数,支持设置数据的索引值
2. 在双方括号中,<>号开始支持地方字符集的比较,由于地方字符集的不同,可以引起字符序号的大小变化。
3. 内置 read 函数,开始支持 -N选项,指定读入多少字符后结束。
4. 内嵌文档标识功能,可以用简单的)符号进行终结。
例如:
-
#!/bin/bash
-
# here-commsub.sh
-
# Requires Bash version -ge 4.1 ...
-
multi_line_var=$( cat <<ENDxxx
-
------------------------------
-
This is line 1 of the variable
-
This is line 2 of the variable
-
This is line 3 of the variable
-
------------------------------
-
ENDxxx)
-
# Rather than what Bash 4.0 requires:
-
#+ that the terminating limit string and
-
#+ the terminating close-parenthesis be on separate lines.
-
# ENDxxx
-
# )
-
echo "$multi_line_var"
-
# Bash still emits a warning, though.
-
# warning: here-document at line 10 delimited
-
#+ by end-of-file (wanted `ENDxxx
Bash 4.2
2011年2月发布
1. 开始支持 unicode 字符表达
例如:
-
echo -e '\u2630' # Horizontal triple bar character.
-
# Equivalent to the more roundabout:
-
echo -e "\xE2\x98\xB0"
-
# Recognized by earlier Bash versions.
-
echo -e '\u220F' # PI (Greek letter and mathematical symbol)
-
echo -e '\u0416' # Capital "ZHE" (Cyrillic letter)
-
echo -e '\u2708' # Airplane (Dingbat font) symbol
-
echo -e '\u2622' # Radioactivity trefoil
-
echo -e "The amplifier circuit requires a 100 \u2126 pull-up resistor."
2. lastpipe 功能。有了lastpipe, 终于bash可以像 korn shell 一样进行工作了。korn shell之所以可以优雅的工作,这个功能占的比例很大。当然,还有其他一些细节目前bash还是不具备。但有这个功能,bash已经可以干很多了。
-
#!/bin/bash
-
# lastpipe-option.sh
-
line='' # Null value.
-
echo "\$line = "$line"" # $line =
-
echo
-
shopt -s lastpipe # Error on Bash version -lt 4.2.
-
echo "Exit status of attempting to set \"lastpipe\" option is $?"
-
# 1 if Bash version -lt 4.2, 0 otherwise.
-
echo
-
-
head -1 $0 | read line # Pipe the first line of the script to read.
-
# ^^^^^^^^^ Not in a
-
echo "\$line = "$line""
-
# Older Bash releases $line =
-
# Bash version 4.2 $line = #!/bin/bash
3. 数组下标负值,允许从数组末尾开始进行标注
4. 子串提取,可以长度采用负值,表示从目标字串的末尾开始算起。
点击(此处)折叠或打开
-
# Important: It changes the interpretation of this
-
stringZ=abcABC123ABCabc
-
echo ${stringZ} # abcABC123ABCabc
-
# Position within string: 0123456789.....
-
echo ${stringZ:2:3} # cAB
-
# Count 2 chars forward from string beginning, and extract 3 chars.
-
# ${string:position:length}
-
# So far, nothing new, but now ...
-
# abcABC123ABCabc
-
# Position within string: 0123....6543210
-
echo ${stringZ:3:-6} # ABC123
-
# ^
-
# Index 3 chars forward from beginning and 6 chars backward from end,
-
#+ and extract everything in between.
-
# ${string:offset-from-front:offset-from-end}
-
# When the "length" parameter is negative,
-
#+ it serves as an offset-from-end parameter.
-
# See also neg-array.sh.
-
echo ${stringZ: -4} #Cabc
-
echo ${stringZ:(-4)} #Cabc
阅读(1384) | 评论(0) | 转发(0) |