Chinaunix首页 | 论坛 | 博客
  • 博客访问: 440529
  • 博文数量: 101
  • 博客积分: 1547
  • 博客等级: 上尉
  • 技术积分: 1072
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-12 23:46
个人简介

music,code,dialog,rest

文章分类

全部博文(101)

文章存档

2023年(8)

2022年(25)

2021年(6)

2020年(2)

2019年(6)

2018年(4)

2017年(5)

2016年(20)

2015年(4)

2014年(2)

2013年(1)

2012年(1)

2011年(1)

2010年(1)

2009年(2)

2007年(10)

2006年(3)

分类: LINUX

2014-07-15 08:17:28

最近无意中再次查阅abs文档时,发现了bash 4 4.1 4.2新特性,清除了困扰多年不愿使用bash的阻碍。
在这里我只列出了自己关注的特性。

2009年2月发布的bash4:
1. 关联数组/Hash表

abs中的样例:
#创建

点击(此处)折叠或打开

  1. declare -A address
  2. address[Charles]="414 W. 10th Ave., Baltimore, MD 21236"
  3. address[John]="202 E. 3rd St., New York, NY 10009"
  4. address[Wilma]="1854 Vermont Ave, Los Angeles, CA 90023"

  5. #通过键提取值
  6. echo "Charles's address is ${address[Charles]}."
  7. # Charles
2. ;;& ;&  用于case语句中的连续匹配
;;& 继续匹配下一个模式条件,并根据结果执行相关语句。
;&  直接执行下一条语句,(我的理解是不进行模式匹配)。

点击(此处)折叠或打开

  1. 例子:

  2. test_char ()
  3. {
  4. case "$1" in
  5. [[:print:]] ) echo "$1 is a printable character.";;& # |
  6. # The ;;& terminator continues to the next pattern test. |
  7. [[:alnum:]] ) echo "$1 is an alpha/numeric character.";;& # v
  8. [[:alpha:]] ) echo "$1 is an alphabetic character.";;& # v
  9. [[:lower:]] ) echo "$1 is a lowercase alphabetic character.";;&
  10. [[:digit:]] ) echo "$1 is an numeric character.";& # |
  11. # The ;& terminator executes the next statement ... # |
  12. %%%@@@@@ ) echo "********************************";; # v
  13. # ^^^^^^^^ ... even with a dummy pattern.
  14. esac
  15. }
3. bash新的协程语句 coproc
coproc, 协程可以命名,可以让主程获取协程的输入输出。由于协程是异步执行的,所以有可能会比主程和它交互之前结束运行。

点击(此处)折叠或打开

  1. coproc { cat mx_data.txt; sleep 2; }
  2. # ^^^^^^^
  3. # Try running this without "sleep 2" and see what happens.
  4. while read -u ${COPROC[0]} line # ${COPROC[0]} is the
  5. do #+ file descriptor of the coprocess.
  6. echo "$line" | sed -e 's/line/NOT-ORIGINAL-TEXT/'
  7. done
  8. kill $COPROC_PID
4. 新的内置mapfile功能,使文件调入数组避免使用循环和替换语句。

点击(此处)折叠或打开

  1. mapfile Arr1 < $0
  2. # Same result as Arr1=( $(cat $0) )
  3. echo "${Arr1[@]}" # Copies this entire script out to stdout.
5. 参数变量描述符,使用大小写描述符可以指定变量内容的大小写情况。

点击(此处)折叠或打开

  1. #!/bin/bash4
  2. var=veryMixedUpVariable
  3. echo ${var} # veryMixedUpVariable
  4. echo ${var^} # VeryMixedUpVariable
  5. # * First char --> uppercase.
  6. echo ${var^^} # VERYMIXEDUPVARIABLE
  7. # ** All chars --> uppercase.
  8. echo ${var,} # veryMixedUpVariable
  9. # * First char --> lowercase.
  10. echo ${var,,} # verymixedupvariable
  11. # ** All chars --> lowercase.
6. declare 语句支持 -l 全部小写,  -c  第一字符大写其他小写。

点击(此处)折叠或打开

  1. #!/bin/bash4
  2. declare -l var1 # Will change to lowercase
  3. var1=MixedCaseVARIABLE
  4. echo "$var1" # mixedcasevariable
  5. # Same effect as echo $var1 | tr A-Z a-z
  6. declare -c var2 # Changes only initial char to uppercase.
  7. var2=originally_lowercase
  8. echo "$var2" # Originally_lowercase
  9. # NOT the same effect as echo $var2 | tr a-z A-Z
7. 大括号扩展指令

点击(此处)折叠或打开

  1. #!/bin/bash4
  2. echo {40..60..2}
  3. # 40 42 44 46 48 50 52 54 56 58 60
  4. # All the even numbers, between 40 and 60.
  5. echo {60..40..2}
  6. # 60 58 56 54 52 50 48 46 44 42 40
  7. # All the even numbers, between 40 and 60, counting backwards.
  8. # In effect, a decrement.
  9. echo {60..40..-2}
  10. # The same output. The minus sign is not necessary.
  11. # But, what about letters and symbols?
  12. echo {X..d}
  13. # X Y Z [ ] ^ _ ` a b c d
  14. # Does not echo the \ which escapes a space.
补零指令,在大括号中第一个位置,表示补零状态的初始值,

8. 位置参数的字符串提取,纠正了bash3的索引号未和位置参数索引对齐问题。

点击(此处)折叠或打开

  1. #!/bin/bash
  2. # show-params.bash
  3. # Requires version 4+ of Bash.
  4. # Invoke this scripts with at least one positional parameter.

  5. E_BADPARAMS=99
  6. if [ -z "$1" ]
  7. then
  8. echo "Usage $0 param1 ..."
  9. exit $E_BADPARAMS
  10. fi
  11. echo ${@:0}
  12. # bash3 show-params.bash4 one two three
  13. # one two three
  14. # bash4 show-params.bash4 one two three
  15. # show-params.bash4 one two three
  16. # $0 $1 $2 $3
9. 新的通配符操作,可以对文件名或目录名,进行递归匹配。

点击(此处)折叠或打开

  1. #!/bin/bash4
  2. # filelist.bash4
  3. shopt -s globstar # Must enable globstar, otherwise ** doesn
10. 新的$BASHPID全局内部变量

11. 新的出错处理插入函数

点击(此处)折叠或打开

  1. #!/bin/bash4
  2. command_not_found_handle ()
  3. { # Accepts implicit parameters.
  4. echo "The following command is not valid: \""$1\"""
  5. echo "With the following argument(s): \""$2\"" \""$3\""" # $4, $5 ...
  6. } # $1, $2, etc. are not explicitly passed to the function.
  7. bad_command arg1 arg2
  8. # The following command is not valid: "bad_command"
  9. # With the following argument(s): "arg1" "arg2"
Bash 4.1  2010 5月
1. printf 函数开始支持,-v 参数,支持设置数据的索引值
2. 在双方括号中,<>号开始支持地方字符集的比较,由于地方字符集的不同,可以引起字符序号的大小变化。
3. 内置 read 函数,开始支持 -N选项,指定读入多少字符后结束。
4. 内嵌文档标识功能,可以用简单的)符号进行终结。
例如:

点击(此处)折叠或打开

  1. #!/bin/bash
  2. # here-commsub.sh
  3. # Requires Bash version -ge 4.1 ...
  4. multi_line_var=$( cat <<ENDxxx
  5. ------------------------------
  6. This is line 1 of the variable
  7. This is line 2 of the variable
  8. This is line 3 of the variable
  9. ------------------------------
  10. ENDxxx)
  11. # Rather than what Bash 4.0 requires:
  12. #+ that the terminating limit string and
  13. #+ the terminating close-parenthesis be on separate lines.
  14. # ENDxxx
  15. # )
  16. echo "$multi_line_var"
  17. # Bash still emits a warning, though.
  18. # warning: here-document at line 10 delimited
  19. #+ by end-of-file (wanted `ENDxxx
Bash 4.2
2011年2月发布
1. 开始支持 unicode 字符表达
例如:

点击(此处)折叠或打开

  1. echo -e '\u2630' # Horizontal triple bar character.
  2. # Equivalent to the more roundabout:
  3. echo -e "\xE2\x98\xB0"
  4. # Recognized by earlier Bash versions.
  5. echo -e '\u220F' # PI (Greek letter and mathematical symbol)
  6. echo -e '\u0416' # Capital "ZHE" (Cyrillic letter)
  7. echo -e '\u2708' # Airplane (Dingbat font) symbol
  8. echo -e '\u2622' # Radioactivity trefoil
  9. echo -e "The amplifier circuit requires a 100 \u2126 pull-up resistor."
2. lastpipe 功能。有了lastpipe, 终于bash可以像 korn shell 一样进行工作了。korn shell之所以可以优雅的工作,这个功能占的比例很大。当然,还有其他一些细节目前bash还是不具备。但有这个功能,bash已经可以干很多了。

点击(此处)折叠或打开

  1. #!/bin/bash
  2. # lastpipe-option.sh
  3. line='' # Null value.
  4. echo "\$line = "$line"" # $line =
  5. echo
  6. shopt -s lastpipe # Error on Bash version -lt 4.2.
  7. echo "Exit status of attempting to set \"lastpipe\" option is $?"
  8. # 1 if Bash version -lt 4.2, 0 otherwise.
  9. echo

  10. head -1 $0 | read line # Pipe the first line of the script to read.
  11. # ^^^^^^^^^ Not in a
  12. echo "\$line = "$line""
  13. # Older Bash releases $line =
  14. # Bash version 4.2 $line = #!/bin/bash
3. 数组下标负值,允许从数组末尾开始进行标注
4. 子串提取,可以长度采用负值,表示从目标字串的末尾开始算起。
点击(此处)折叠或打开
  1. # Important: It changes the interpretation of this
  2. stringZ=abcABC123ABCabc
  3. echo ${stringZ} # abcABC123ABCabc
  4. # Position within string: 0123456789.....
  5. echo ${stringZ:2:3} # cAB
  6. # Count 2 chars forward from string beginning, and extract 3 chars.
  7. # ${string:position:length}
  8. # So far, nothing new, but now ...
  9. # abcABC123ABCabc
  10. # Position within string: 0123....6543210
  11. echo ${stringZ:3:-6} # ABC123
  12. # ^
  13. # Index 3 chars forward from beginning and 6 chars backward from end,
  14. #+ and extract everything in between.
  15. # ${string:offset-from-front:offset-from-end}
  16. # When the "length" parameter is negative,
  17. #+ it serves as an offset-from-end parameter.
  18. # See also neg-array.sh.
  19. echo ${stringZ: -4}   #Cabc
  20. echo ${stringZ:(-4)}  #Cabc
阅读(1384) | 评论(0) | 转发(0) |
0

上一篇:Motor Theory

下一篇:bash ksh的经验日志

给主人留下些什么吧!~~