发新话题
打印

bash 4.0 比较有用的功能

bash 4.0 比较有用的功能

bash 4.0 已经正式 release, 4.0.0 的时候有好多 bug, 然后 patch file 迅速就出了 10 个, 现在版本为 4.0.10 基本比较稳定了.

4.0 的新 feature 很多, 不过大部分大家可能都用不到, 大概浏览了一下, 找出几个感觉比较有用的 feature 跟大家 share 一下.

[ 本帖最后由 dearvoid 于 2009-3-18 13:29 编辑 ]      
论坛的短消息功能有问题, 请发邮件: d e a r v o i d @ g m a i l . c o m

TOP

内置的哈希 (hash) 支持: 关联数组 (associative array)

这个 feature 应当是大家期盼已久的了,perl、tcl、awk 等都支持 hash,用起来相当方便。

declare 命令有一个新的选项 -A 来声明关联数组,示例如下:
引用:
-(dearvoid@LinuxEden:Forum)-(~/void/bash/bash4)-
[$$=32353 $?=0]
; cat associative-array.sh
#!/bin/bash
# vi:set ts=8 sw=4 et sta:
#
#--------------------------------------------------------------------#

declare -A hash
hash[aaa]=111
hash[bb]=222
hash[c]=333

echo "All keys in hash: ${!hash[@]}"

echo "All values in hash: ${hash[@]}"

echo "Key-value pairs:"
for key in "${!hash[@]}"; do
    echo "    hash[$key] = ${hash[$key]}"
done
-(dearvoid@LinuxEden:Forum)-(~/void/bash/bash4)-
[$$=32353 $?=0]
; bash4 associative-array.sh
All keys in hash: c bb aaa
All values in hash: 333 222 111
Key-value pairs:
    hash[c] = 333
    hash[bb] = 222
    hash[aaa] = 111
-(dearvoid@LinuxEden:Forum)-(~/void/bash/bash4)-
[$$=32353 $?=0]
; bye
      
论坛的短消息功能有问题, 请发邮件: d e a r v o i d @ g m a i l . c o m

TOP

新的 brace expansion 用法

引用:
-(dearvoid@LinuxEden:Forum)-(~/void/bash/bash4)-
[$$=32353 $?=0]
; cat brace_expand.sh
#!/bin/bash
# vi:set ts=8 sw=4 et sta:
#
#--------------------------------------------------------------------#

echo {1..10}
echo {001..10}
-(dearvoid@LinuxEden:Forum)-(~/void/bash/bash4)-
[$$=32353 $?=0]
; bash3 brace_expand.sh
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
-(dearvoid@LinuxEden:Forum)-(~/void/bash/bash4)-
[$$=32353 $?=0]
; bash4 brace_expand.sh
1 2 3 4 5 6 7 8 9 10
001 002 003 004 005 006 007 008 009 010
-(dearvoid@LinuxEden:Forum)-(~/void/bash/bash4)-
[$$=32353 $?=0]
; bye
      
论坛的短消息功能有问题, 请发邮件: d e a r v o i d @ g m a i l . c o m

TOP

built-in 的大小写转换机制

declare -l var 可以声明一个小写的变量 var,当赋值给 var 的时候,bash 会强制把其中的大写字母转成小写;相应的,declare -u var 则可以声明一个大写的变量。有了这个 feature,大小写转换就太简单了,再也不用调用什么 tr 之类
引用:
-(dearvoid@LinuxEden:Forum)-(~/void/bash/bash4)-
[$$=32353 $?=0]
; cat declare-l-u.sh
#!/bin/bash
# vi:set ts=8 sw=4 et sta:
#
#--------------------------------------------------------------------#

declare -l lc
declare -u uc

lc=HELLO
echo $lc
uc=world
echo $uc
-(dearvoid@LinuxEden:Forum)-(~/void/bash/bash4)-
[$$=32353 $?=0]
; bash4 declare-l-u.sh
hello
WORLD
-(dearvoid@LinuxEden:Forum)-(~/void/bash/bash4)-
[$$=32353 $?=0]
; bye
      


论坛的短消息功能有问题, 请发邮件: d e a r v o i d @ g m a i l . c o m

TOP

把文本文件直接读入一个数组

bash4 新增了个 mapfile 命令(也叫 readarray),可以方便的把文本文件一次性读入一个数组中,mapfile 有好多灵活的选项,详情请 help mapfile。示例:
引用:
-(dearvoid@LinuxEden:Forum)-(~/void/bash/bash4)-
[$$=32353 $?=0]
; cat file.txt
line 1
line 2
line 3
line 4
-(dearvoid@LinuxEden:Forum)-(~/void/bash/bash4)-
[$$=32353 $?=0]
; cat mapfile.sh
#!/bin/bash
# vi:set ts=8 sw=4 et sta:
#
#--------------------------------------------------------------------#

mapfile -t arr < file.txt
for i in "${arr[@]}"; do
    echo "$i"
done
-(dearvoid@LinuxEden:Forum)-(~/void/bash/bash4)-
[$$=32353 $?=0]
; bash4 mapfile.sh
line 1
line 2
line 3
line 4
-(dearvoid@LinuxEden:Forum)-(~/void/bash/bash4)-
[$$=32353 $?=0]
; bye
      
论坛的短消息功能有问题, 请发邮件: d e a r v o i d @ g m a i l . c o m

TOP

新的重定向符号

bash4 新增两个 redirection 的符号:&>> 和 |&

cmd ... &>> file 等价于 cmd >> file 2>&1
cmd ... |& cmd2 ... 等价于 cmd ... 2>&1 | cmd2 ...      
论坛的短消息功能有问题, 请发邮件: d e a r v o i d @ g m a i l . c o m

TOP

新的 help 输出格式

bash4 以前的 help 命令的输出很难看,不容易找到某个具体选项的说明,bash4 对此有极大改善。比如 bash3 中 help help 输出如下:
复制内容到剪贴板
代码:
help: help [-s] [pattern ...]
    Display helpful information about builtin commands.  If PATTERN is
    specified, gives detailed help on all commands matching PATTERN,
    otherwise a list of the builtins is printed.  The -s option
    restricts the output for each builtin command matching PATTERN to
    a short usage synopsis.
而bash4 中 help help 输出如下:
复制内容到剪贴板
代码:
help: help [-ds] [pattern ...]
    Display information about builtin commands.

    Displays brief summaries of builtin commands.  If PATTERN is
    specified, gives detailed help on all commands matching PATTERN,
    otherwise the list of help topics is printed.

    Options:
      -d        output short description for each topic
      -m        display usage in pseudo-manpage format
      -s        output only a short usage synopsis for each topic matching
        PATTERN

    Arguments:
      PATTERN   Pattern specifiying a help topic

    Exit Status:
    Returns success unless PATTERN is not found or an invalid option is given.
      
论坛的短消息功能有问题, 请发邮件: d e a r v o i d @ g m a i l . c o m

TOP

当上了小白?哈

这些特性都很不错!       
面包会有的 女人也会有的
_______________________

TOP

其中三个很有用:关联数组,brace expantion ,把文件读入数组      
本帖最近评分记录
  • admin 论坛活跃 +5 指定楼层奖励  2009-3-19 08:42

TOP

发新话题