shell shift

在看uboot的源码时(开发板是EB-SAM9G45):

在顶层Makefile中,

at91sam9g45ekes_nandflash_config \
at91sam9g45ekes_dataflash_config \
at91sam9g45ekes_dataflash_cs0_config \
at91sam9g45ekes_config    :    unconfig
    @mkdir -p $(obj)include
    @if [ "$(findstring _nandflash,$@)" ] ; then \
        echo "#define CFG_USE_NANDFLASH 1"    >>$(obj)include/config.h ; \
        $(XECHO) "... with environment variable in NAND FLASH" ; \
        echo "#define CFG_USE_AT91SAM9G45EKES 1"    >>$(obj)include/config.h ; \
    else \
        echo "#define CFG_USE_DATAFLASH 1"    >>$(obj)include/config.h ; \
        $(XECHO) "... with environment variable in SPI DATAFLASH CS0" ; \
        echo "#define CFG_USE_AT91SAM9G45EKES 1"    >>$(obj)include/config.h ; \
    fi;
    @$(MKCONFIG) -a at91sam9m10g45ek arm arm926ejs at91sam9m10g45ek atmel at91sam9

在mkconfig中,

while [ $# -gt 0 ] ; do
    case "$1" in
    --) shift ; break ;;
    -a) shift ; APPEND=yes ;;
    -n) shift ; BOARD_NAME="${1%%_config}" ; shift ;;
    *)  break ;;
    esac
done

则句话执行之前,

$1 是 -a

$2 是at91sam9m10g45ek

$3 arm

$4 arm926ejs

$5 at91sam9m10g45ek

$6 atmel

$7 是at91sam9

在这句话执行后,$1、$2、$3等发生了变化,原因是 执行这句话之前$1是-a,所以执行了-a) shift ; APPEND=yes ;; 其中;;相等于break。关键是shift,执行shift后$1变成了$2,$2变成了$3,以此类推,$1的值丢失了。

所以执行完成后:

$1 是 at91sam9m10g45ek

$2 是arm

$3 arm926ejs 

$4 at91sam9m10g45ek 

$5 atmel 

$6 at91sam9

详细说明:

转载自:http://blog.csdn.net/xiangliangyu2008/article/details/8112568

shift命令会重新分配位置参数,其实就是把所有的位置参数都向左移动一个位置。

 

比如shift 3表示原来的$4现在变成$1,原来的$5现在变成$2等等,原来的$1、$2、$3丢弃,$0不移动。不带参数的shift命令相当于shift 1。
我们知道,对于位置变量或命令行参数,其个数必须是确定 的,或者当 Shell 程序不知道其个数时,可以把所有参数一起赋值给变量$*。若用户要求 Shell 在不知道位置变量个数的情况下,还能逐个的把参数一一处理,也就是在 $1 后为 $2,在 $2 后面为 $3 等。在 shift 命令执行前变量 $1 的值在 shift 命令执行后就不可用了。

 

 

#!/bin/bash
# 使用'shift'来逐步存取所有的位置参数. 

#  给脚本命个名, 比如shft,
#+ 然后给脚本传递一些位置参数, 比如: 
#          ./shft a b c def 23 skidoo

until [ -z "$1" ]  # 直到所有的位置参数都被存取完...
do
  echo "第一个参数为: $1 参数个数为: $#"
  shift
done

echo --------------

exit 0

然后执行

# sh ./shft a b c def 23 skidoo
第一个参数为: a 参数个数为: 6
第一个参数为: b 参数个数为: 5
第一个参数为: c 参数个数为: 4
第一个参数为: def 参数个数为: 3
第一个参数为: 23 参数个数为: 2
第一个参数为: skidoo 参数个数为: 1
--------------

从上可知 shift 命令每执行一次,变量的个数($#)减一,而变量值提前一位。

下面代码用 until 和 shift 命令计算所有命令行参数的和。

#!/bin/bash
#shift 上档命令的应用(shft2.sh)
if [ $# -eq 0 ]
then
echo "Usage:shift2.sh 参数"
exit 1
fi
sum=0
until [ $# -eq 0 ]
do
sum=`expr $sum + $1`
shift
done
echo "sum is: $sum"

然后执行

# sh shft2.sh 10 20 30
sum is: 60


Shift 命令还有另外一个重要用途, Bash 定义了9个位置变量,从 $1 到 $9,这并不意味着用户在命令行只能使用9个参数,借助 shift 命令可以访问多于9个的参数。Shift 命令一次移动参数的个数由其所带的参数指定。例如当 shell 程序处理完前九个命令行参数后,可以使用 shift 9 命令把 $10 移到 $1。

posted @ 2013-07-27 10:06  摩斯电码  阅读(1801)  评论(0编辑  收藏  举报