现在的位置: 首页 > 综合 > 正文

脚本参数中shift的使用 (ex19)

2014年06月05日 ⁄ 综合 ⁄ 共 603字 ⁄ 字号 评论关闭
#!/bin/bash
# shft.sh: Using 'shift' to step through all the positional parameters.

#  Name this script something like shft.sh,
#+ and invoke it with some parameters.
#+ For example:
#             sh shft.sh a b c def 23 Skidoo

until [ -z "$1" ]  # Until all parameters used up . . .
do
  echo -n "$1  "
  echo "$2"
  shift
done

echo               # Extra linefeed.

# But, what happens to the "used-up" parameters?
echo "$2"
#  Nothing echoes!
#  When $2 shifts into $1 (and there is no $3 to shift into $2)
#+ then $2 remains empty.
#  So, it is not a parameter *copy*, but a *move*.

exit

#  See also the echo-params.sh script for a "shiftless"
#+ alternative method of stepping through the positional params.

执行:./ex19.sh a b c def 23 skido

结果:

a  b
b  c
c  def
def  23
23  skido
skido 

抱歉!评论已关闭.