- 论坛徽章:
- 8
|
好了,唠叨了这么多,试着读解一下incompat.sh的结果吧
- #!/bin/bash
- # Erratic behavior of the "$*" and "$@" internal Bash variables,
- #+ depending on whether they are quoted or not.
- # Inconsistent handling of word splitting and linefeeds.
- set -- "First one" "second" "third:one" "" "Fifth: :one"
- # Setting the script arguments, $1, $2, etc.
- echo
- echo 'IFS unchanged, using "$*"'
- c=0
- for i in "$*" # quoted
- do echo "$((c+=1)): [$i]" # This line remains the same in every instance.
- # Echo args.
- done
- ###############
- #结果
- #1: [First one second third:one Fifth: :one]
- #$*被扩展为一个字`First one second third:one Fifth: :one',注意扩展时的\c为space
- #由于""的作用,不进行word splitting
- #变量i in 一个字
- ###############
- echo ---
- echo 'IFS unchanged, using $*'
- c=0
- for i in $* # unquoted
- do echo "$((c+=1)): [$i]"
- done
- ###############
- #结果
- #1: [First]
- #2: [one]
- #3: [second]
- #4: [third:one]
- #5: [Fifth:]
- #6: [:one]
- #$*被扩展为一个字`First one second third:one Fifth: :one',注意扩展时的\c为space
- #然后进行word splitting扩展为`First'、`one'、 `second'、`third:one'、`Fifth:'、`:one'六个字,
- 注意使用缺省IFS时中间连续的空格被解释为一个字分割符
- #变量i in 六个字`First'、`one'、 `second'、`third:one'、`Fifth:'、`:one'
- ###############
- echo ---
- echo 'IFS unchanged, using "$@"'
- c=0
- for i in "$@"
- do echo "$((c+=1)): [$i]"
- done
- #结果
- #1: [First one]
- #2: [second]
- #3: [third:one]
- #4: []
- #5: [Fifth: :one]
- #$@被扩展为五个字`First one'、`second'、`third:one'、`空字串'、`Fifth: :one'
- #由于""的作用,不进行word splitting
- #变量i in 五个字`First'、`one'、 `second'、`third:one'、`Fifth: :one'
- echo ---
- echo 'IFS unchanged, using $@'
- c=0
- for i in $@
- do echo "$((c+=1)): [$i]"
- done
- #结果
- #1: [First]
- #2: [one]
- #3: [second]
- #4: [third:one]
- #5: [Fifth:]
- #6: [:one]
- #$@被扩展为五个字`First one'、`second'、`third:one'、`空字串'、`Fifth: :one'
- #然后进行word splitting扩展为`First'、`one'、 `second'、`third:one'、`Fifth:'、`:one'六个字,注意由于没有""的保护上一步那个空
- 字串已经不见了
- #变量i in 六个字`First'、`one'、 `second'、`third:one'、`Fifth:'、`:one'
- echo ---
- IFS=:
- echo 'IFS=":", using "$*"'
- c=0
- for i in "$*"
- do echo "$((c+=1)): [$i]"
- done
- ###############
- #结果
- #1: [First one:second:third:one::Fifth: :one]
- #$*被扩展为一个字`First one:second:third:one::Fifth: :one',注意扩展时的\c为:
- #由于""的作用,不进行word splitting
- #变量i in 一个字
- ###############
- echo ---
- echo 'IFS=":", using $*'
- c=0
- for i in $*
- do echo "$((c+=1)): [$i]"
- done
- ###############
- #结果
- #1: [First one]
- #2: [second]
- #3: [third]
- #4: [one]
- #5: []
- #6: [Fifth]
- #7: [ ]
- #8: [one]
- #$*被扩展为一个字`First one:second:third:one::Fifth: :one',注意扩展时的\c为:
- #然后进行word splitting扩展为`First one'、`second'、`third'、`one'、`空字串'、`Fifth'、` '、`one'八个字,
- 注意使用指定IFS时每间个IFS都被认为是一个字分割符
- #变量i in 八个字`First one'、`second'、`third'、`one'、`空字串'、`Fifth'、` '、`one'
- ###############
- echo ---
- var=$*
- echo 'IFS=":", using "$var" (var=$*)'
- c=0
- for i in "$var"
- do echo "$((c+=1)): [$i]"
- done
- ###############
- #结果
- #1: [First one:second:third:one::Fifth: :one]
- #$*被扩展为一个字`First one:second:third:one::Fifth: :one'并赋值给var,注意扩展时的\c为:
- #由于""的作用,$var替代后不进行word splitting
- #变量i in 一个字`First one:second:third:one::Fifth: :one'
- ###############
- echo ---
- echo 'IFS=":", using $var (var=$*)'
- c=0
- for i in $var
- do echo "$((c+=1)): [$i]"
- done
- ###############
- #结果
- #IFS=":", using $var (var=$*)
- #1: [First one]
- #2: [second]
- #3: [third]
- #4: [one]
- #5: []
- #6: [Fifth]
- #7: [ ]
- #8: [one]
- #1: [First one:second:third:one::Fifth: :one]
- #$*被扩展为一个字`First one:second:third:one::Fifth: :one'并赋值给var,注意扩展时的\c为:
- #$var替代后进行word splitting扩展为`First one'、`second'、`third'、`one'、`空字串'、`Fifth'、` '、`one'八个字,
- 注意使用指定IFS时每间个IFS都被认为是一个字分割符
- #变量i in 八个字`First one'、`second'、`third'、`one'、`空字串'、`Fifth'、` '、`one'
- ###############
- echo ---
- var="$*"
- echo 'IFS=":", using $var (var="$*")'
- c=0
- for i in $var
- do echo "$((c+=1)): [$i]"
- done
- ###############
- #结果
- #IFS=":", using $var (var=$*)
- #1: [First one]
- #2: [second]
- #3: [third]
- #4: [one]
- #5: []
- #6: [Fifth]
- #7: [ ]
- #8: [one]
- #$*被扩展为一个字`First one:second:third:one::Fifth: :one'并赋值给var,注意扩展时的\c为:
- #$var替代后进行word splitting扩展为`First one'、`second'、`third'、`one'、`空字串'、`Fifth'、` '、`one'八个字,
- 注意使用指定IFS时每间个IFS都被认为是一个字分割符
- #变量i in 八个字`First one'、`second'、`third'、`one'、`空字串'、`Fifth'、` '、`one'
- ###############
- echo ---
- echo 'IFS=":", using "$var" (var="$*")'
- c=0
- for i in "$var"
- do echo "$((c+=1)): [$i]"
- done
- ###############
- #结果
- #1: [First one:second:third:one::Fifth: :one]
- #$*被扩展为一个字`First one:second:third:one::Fifth: :one'并赋值给var,注意扩展时的\c为:
- #由于""的作用,$var替代后不进行word splitting
- #变量i in 一个字`First one:second:third:one::Fifth: :one'
- ###############
- echo ---
- echo 'IFS=":", using "$@"'
- c=0
- for i in "$@"
- do echo "$((c+=1)): [$i]"
- done
- ###############
- #结果
- #1: [First one]
- #2: [second]
- #3: [third:one]
- #4: []
- #5: [Fifth: :one]
- #$@被扩展为五个字`First one'、`second'、`third:one'、`空字串'、`Fifth: :one'
- #由于""的作用,不进行word splitting
- #变量i in 五个字`First one'、`second'、`third:one'、`'、`Fifth: :one'
- ###############
- echo ---
- echo 'IFS=":", using $@'
- c=0
- for i in $@
- do echo "$((c+=1)): [$i]"
- done
- #结果
- 1: [First one]
- 2: [second]
- 3: [third]
- 4: [one]
- 5: []
- 6: [Fifth]
- 7: [ ]
- 8: [one]
- ###############
- #$@被扩展为五个字`First one'、`second'、`third:one'、`空字串'、`Fifth: :one'
- #这五个字再进行word splitting,扩展为`First one'、`second'、`third'、`one'、`空字串'、`Fifth'、` '、`one'八个字
- #注意这里的第3和4由上一上步的`third:one'扩展,第678由`Fifth: :one'扩展
- #变量i in 八个字`First one'、`second'、`third'、`one'、`空字串'、`Fifth'、` '、`one'
- ###############
- echo ---
- var=$@
- echo 'IFS=":", using $var (var=$@)'
- c=0
- for i in $var
- do echo "$((c+=1)): [$i]"
- done
- ###############
- #结果
- #1: 1: [First one second third one Fifth one]
- #$@被扩展为五个字`First one'、`second'、`third:one'、`空字串'、`Fifth: :one'并word splitting成为`First one'、`second'、#`third'、`one'、`空字串'、`Fifth'、` '、`one'再由space串成一串赋值给var
- #$var在替代后进行word splitting,由于不包含IFS,所以分不分都一样
- #变量i in 一个字`First one second third one Fifth one'
- ###############
- echo ---
- echo 'IFS=":", using "$var" (var=$@)'
- c=0
- for i in "$var"
- do echo "$((c+=1)): [$i]"
- done
- ###############
- #结果
- #1: 1: [First one second third one Fifth one]
- #$@被扩展为五个字`First one'、`second'、`third:one'、`空字串'、`Fifth: :one'并word splitting成为`First one'、`second'、#`third'、`one'、`空字串'、`Fifth'、` '、`one'再由space串成一串赋值给var
- #$var不进行word splitting
- #变量i in 一个字`First one second third one Fifth one'
- ###############
- echo ---
- var="$@"
- echo 'IFS=":", using "$var" (var="$@")'
- c=0
- for i in "$var"
- do echo "$((c+=1)): [$i]"
- done
- ###############
- #结果
- #1: 1: [First one second third:one Fifth: :one]
- #"$@"被扩展为五个字`First one'、`second'、`third:one'、`空字串'、`Fifth: :one'并由space串成一串赋值给var
- #$var不进行word splitting
- #变量i in 一个字`First one second third:one Fifth: :one'
- ###############
- echo ---
- echo 'IFS=":", using $var (var="$@")'
- c=0
- for i in $var
- do echo "$((c+=1)): [$i]"
- done
- ###############
- #结果
- #1: [First one second third]
- #2: [one Fifth]
- #3: [ ]
- #4: [one]
- #"$@"被扩展为五个字`First one'、`second'、`third:one'、`空字串'、`Fifth: :one'并由space串成一串赋值给var
- #$var在替代后进行word splitting,由一个字`First one second third:one Fifth: :one'分割为`First one second'
- `third'、`one Fifth'、` '、`one'四个字
- #变量i in 四个字`First one second'、`third'、`one Fifth'、` '、`one'
- ###############
- echo
- # Try this script with ksh or zsh -y.
- exit 0
- # This example script by Stephane Chazelas,
- # and slightly modified by the document author.
复制代码
[ 本帖最后由 waker 于 2006-2-24 13:25 编辑 ] |
|