Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1171852
  • 博文数量: 115
  • 博客积分: 950
  • 博客等级: 准尉
  • 技术积分: 1734
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-08 20:46
文章分类

全部博文(115)

文章存档

2015年(5)

2014年(28)

2013年(42)

2012年(40)

分类: LINUX

2012-09-03 21:10:32

    阅读了高级bash脚本编程一书,贴上有关$#, $*, $@的相关内容已经脚本实例。

位置参数:

$1, $2.. :
      位置参数从参数 0 开始。参数 0 引用启动 bash 的程序的名称,如果函数在 shell 脚本中运行,则引用
shell 脚本的名称。有关该参数的其他信息,比如 bash 由 -c 参数启动,请参阅 bash 手册页面。由单引号或
双引号包围的字符串被作为一个参数进行传递,传递时会去掉引号。 如果是双引号,则在调用函数之前将对
 $HOME 之类的 shell 变量进行扩展。对于包含嵌入空白或其他字符(这些空白或字符可能对 shell 有特殊意
义)的参数,需要使用单引号或双引号进行传递。
$* :
      位置参数从参数 1 开始。如果在双引号中进行扩展,则扩展就是一个词,由 IFS 特殊变量的第一个字符
将参数分开,如果 IFS 为空,则没有间隔空格。IFS 的默认值是空白、制表符和换行符。如果没有设置 IFS,
则使用空白作为分隔符(仅对默认 IFS 而言)。
$@ :
      位置参数从参数 1 开始。如果在双引号中进行扩展,则每个参数都会成为一个词,因此 “$@” 与
“$1” “$2” 等效。如果参数有可能包含嵌入空白,那么您将需要使用这种形式。
$# :
      参数数量(不包含参数 0)。

脚本实例:
      程序实例是最能说明问题的了

点击(此处)折叠或打开

  1. #!/bin/bash

  2. # "$*"和"$@"的古怪行为,
  3. #+ 依赖于它们是否被""引用.
  4. # 单词拆分和换行的不一致处理.

  5. set -- "First one" "second" "third:one" "" "Fifth: :one"
  6. # 设置这个脚本参数,$1,$2,等等.
  7. echo $#

  8. #################################
  9. echo
  10. echo "eg: 1-1"
  11. echo 'IFS unchanged, using "$*"'
  12. c=0
  13. for i in "$*" # 引用
  14. do echo "$((c+=1)): [$i]" # 这行在下边的每个例子中都一样.
  15. # Echo 参数.
  16. done
  17. echo ---
  18. #-------------------------------
  19. echo "eg: 1-2"
  20. echo 'IFS unchanged, using $*'
  21. c=0
  22. for i in $* # 未引用
  23. do echo "$((c+=1)): [$i]"
  24. done
  25. echo ---
  26. #################################

  27. #################################
  28. echo
  29. echo "eg: 2-1"
  30. echo 'IFS unchanged, using "$@"'
  31. c=0
  32. for i in "$@"
  33. do echo "$((c+=1)): [$i]"
  34. done
  35. echo ---
  36. #-------------------------------
  37. echo "eg: 2-2"
  38. echo 'IFS unchanged, using $@'
  39. c=0
  40. for i in $@
  41. do echo "$((c+=1)): [$i]"
  42. done
  43. echo ---
  44. #################################

  45. #################################
  46. echo
  47. echo "eg: 3-1"
  48. IFS=:
  49. echo 'IFS=":", using "$*"'
  50. c=0
  51. for i in "$*"
  52. do echo "$((c+=1)): [$i]"
  53. done
  54. echo ---
  55. #-------------------------------
  56. echo "eg: 3-2"
  57. echo 'IFS=":", using $*'
  58. c=0
  59. for i in $*
  60. do echo "$((c+=1)): [$i]"
  61. done
  62. echo ---
  63. #################################

  64. #################################
  65. echo
  66. echo "eg: 4-1"
  67. var=$*
  68. echo 'IFS=":", using "$var" (var=$*)'
  69. c=0
  70. for i in "$var"
  71. do echo "$((c+=1)): [$i]"
  72. done
  73. echo ---
  74. #-------------------------------
  75. echo "eg: 4-2"
  76. echo 'IFS=":", using $var (var=$*)'
  77. c=0
  78. for i in $var
  79. do echo "$((c+=1)): [$i]"
  80. done
  81. echo ---
  82. #################################

  83. #################################
  84. echo
  85. echo "eg: 5-1"
  86. var="$*"
  87. echo 'IFS=":", using "$var" (var="$*")'
  88. c=0
  89. for i in "$var"
  90. do echo "$((c+=1)): [$i]"
  91. done
  92. echo ---
  93. #-------------------------------
  94. echo "eg: 5-2"
  95. echo 'IFS=":", using $var (var="$*")'
  96. c=0
  97. for i in $var
  98. do echo "$((c+=1)): [$i]"
  99. done
  100. echo ---
  101. #################################

  102. #################################
  103. echo
  104. echo "eg: 6-1"
  105. echo 'IFS=":", using "$@"'
  106. c=0
  107. for i in "$@"
  108. do echo "$((c+=1)): [$i]"
  109. done
  110. echo ---
  111. #-------------------------------
  112. echo "eg: 6-2"
  113. echo 'IFS=":", using $@'
  114. c=0
  115. for i in $@
  116. do echo "$((c+=1)): [$i]"
  117. done
  118. echo ---
  119. #################################

  120. #################################
  121. echo
  122. echo "eg: 7-1"
  123. var=$@
  124. echo 'IFS=":", using $var (var=$@)'
  125. c=0
  126. for i in $var
  127. do echo "$((c+=1)): [$i]"
  128. done
  129. echo ---
  130. #-------------------------------
  131. echo "eg: 7-2"
  132. echo 'IFS=":", using "$var" (var=$@)'
  133. c=0
  134. for i in "$var"
  135. do echo "$((c+=1)): [$i]"
  136. done
  137. echo ---
  138. #################################

  139. #################################
  140. echo
  141. echo "eg: 8-1"
  142. var=$@
  143. var="$@"
  144. echo 'IFS=":", using "$var" (var="$@")'
  145. c=0
  146. for i in "$var"
  147. do echo "$((c+=1)): [$i]"
  148. done
  149. echo ---
  150. #-------------------------------
  151. echo "eg: 8-2"
  152. echo 'IFS=":", using $var (var="$@")'
  153. c=0
  154. for i in $var
  155. do echo "$((c+=1)): [$i]"
  156. done
  157. #################################

  158. echo

  159. # 用 ksh 或者 zsh -y 来试试这个脚本.

  160. exit 0

  161. # This example script by Stephane Chazelas,
  162. # and slightly modified by the document author.
  163. ################################End
运行结果:

点击(此处)折叠或打开

  1. 5

  2. eg: 1-1
  3. IFS unchanged, using "$*"
  4. 1: [First one second third:one Fifth: :one]
  5. ---
  6. eg: 1-2
  7. IFS unchanged, using $*
  8. 1: [First]
  9. 2: [one]
  10. 3: [second]
  11. 4: [third:one]
  12. 5: [Fifth:]
  13. 6: [:one]
  14. ---

  15. eg: 2-1
  16. IFS unchanged, using "$@"
  17. 1: [First one]
  18. 2: [second]
  19. 3: [third:one]
  20. 4: []
  21. 5: [Fifth: :one]
  22. ---
  23. eg: 2-2
  24. IFS unchanged, using $@
  25. 1: [First]
  26. 2: [one]
  27. 3: [second]
  28. 4: [third:one]
  29. 5: [Fifth:]
  30. 6: [:one]
  31. ---

  32. eg: 3-1
  33. IFS=":", using "$*"
  34. 1: [First one:second:third:one::Fifth: :one]
  35. ---
  36. eg: 3-2
  37. IFS=":", using $*
  38. 1: [First one]
  39. 2: [second]
  40. 3: [third]
  41. 4: [one]
  42. 5: []
  43. 6: [Fifth]
  44. 7: [ ]
  45. 8: [one]
  46. ---

  47. eg: 4-1
  48. IFS=":", using "$var" (var=$*)
  49. 1: [First one:second:third:one::Fifth: :one]
  50. ---
  51. eg: 4-2
  52. IFS=":", using $var (var=$*)
  53. 1: [First one]
  54. 2: [second]
  55. 3: [third]
  56. 4: [one]
  57. 5: []
  58. 6: [Fifth]
  59. 7: [ ]
  60. 8: [one]
  61. ---

  62. eg: 5-1
  63. IFS=":", using "$var" (var="$*")
  64. 1: [First one:second:third:one::Fifth: :one]
  65. ---
  66. eg: 5-2
  67. IFS=":", using $var (var="$*")
  68. 1: [First one]
  69. 2: [second]
  70. 3: [third]
  71. 4: [one]
  72. 5: []
  73. 6: [Fifth]
  74. 7: [ ]
  75. 8: [one]
  76. ---

  77. eg: 6-1
  78. IFS=":", using "$@"
  79. 1: [First one]
  80. 2: [second]
  81. 3: [third:one]
  82. 4: []
  83. 5: [Fifth: :one]
  84. ---
  85. eg: 6-2
  86. IFS=":", using $@
  87. 1: [First one]
  88. 2: [second]
  89. 3: [third]
  90. 4: [one]
  91. 5: []
  92. 6: [Fifth]
  93. 7: [ ]
  94. 8: [one]
  95. ---

  96. eg: 7-1
  97. IFS=":", using $var (var=$@)
  98. 1: [First one second third one Fifth one]
  99. ---
  100. eg: 7-2
  101. IFS=":", using "$var" (var=$@)
  102. 1: [First one second third one Fifth one]
  103. ---

  104. eg: 8-1
  105. IFS=":", using "$var" (var="$@")
  106. 1: [First one second third:one Fifth: :one]
  107. ---
  108. eg: 8-2
  109. IFS=":", using $var (var="$@")
  110. 1: [First one second third]
  111. 2: [one Fifth]
  112. 3: [ ]
  113. 4: [one]
对于之前的1-6的实例,没有问题
但是对于实例7,为何作为$@直接传递给var后,var的值中却没有IFS的分割符显示,比如实例5-1?


阅读(6601) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~