Shell编程中Shift的用法

Shell编程中Shift的用法

  • 位置参数可以用shift命令左移。比如shift 3表示原来的4现在变成1,原来的5现在变成2等等,原来的1,2, 3 , 3, 3,-丢弃,$ 0 不移动。不带参数的shift命令相当于shift 1。

  • 非常有用的 Unix 命令:shift。我们知道,对于位置变量或命令行参数,其个数必须是确定的,或者当 Shell 程序不知道其个数时,可以把所有参数一起赋值给变量\*。若用户要求 Shell 在不知道位置变量个数的情况下,还能逐个的把参数一一处理,也就是在 1 后为 2,在 2 后面为 3 等。在 shift 命令执行前变量 1 的值在 shift 命令执行后就不可用了。

    复制代码
      #测试 shift 命令(x_shift.sh)
      until [ $# -eq 0 ]
      do
      echo "第一个参数为: $1 参数个数为: $#"
      shift
      done
      执行以上程序x_shift.sh:
      $./x_shift.sh 1 2 3 4
      
      结果显示如下:
      
      第一个参数为: 1 参数个数为: 4
      第一个参数为: 2 参数个数为: 3
      第一个参数为: 3 参数个数为: 2
      第一个参数为: 4 参数个数为: 1
    • 从上可知 shift 命令每执行一次,变量的个数($#)减一,而变量值提前一位,下面代码用 until 和 shift 命令计算所有命令行参数的和。

      复制代码
      shift 上档命令的应用(x_shift2.sh)
      if [ $# -eq 0 ]
      then
      echo "Usage:x_shift2.sh 参数"
      exit 1
      fi
      sum=0
      until [ $# -eq 0 ]
      do
      sum=`expr $sum + $1`
      shift
      done
      echo "sum is: $sum"			
      执行上述程序:			
      $x_shift2.sh 10 20 15			
      其显示结果为:45
  • Shift 命令还有另外一个重要用途, Bsh 定义了9个位置变量,从 1 到 9,这并不意味着用户在命令行只能使用9个参数,借助 shift 命令可以访问多于9个的参数。Shift 命令一次移动参数的个数由其所带的参数指定。例如当 shell 程序处理完前九个命令行参数后,可以使用 shift 9 命令把 10 移到 1。

  • 实际应用:

bash 复制代码
while :
do
    [ -z "$1" ] && break;
    case "$1" in
        -a ) NEED_NFS='y' && NEED_HTTP='y' && YUM_ONLINE_REPO='y';shift;;
        -c ) ONLY_UPGRADE_CTL='y' && UPGRADE='y';shift;;
        # shift 2 代表后面的参数前移2位, 比如a,b,c前移之后变成 c
        -C ) check_myarg $1 $2;CONSOLE_PROXY_ADDRESS=$2;shift 2;;
        -d ) DEBUG='y';shift;;
        -D ) NEED_DROP_DB='y';shift;;
        -E ) INSTALL_ENTERPRISE='y';shift;;
        -H ) check_myarg $1 $2;NEED_HTTP='y' && HTTP_FOLDER=$2;shift 2;;
        -f ) check_myarg $1 $2;ZSTACK_ALL_IN_ONE=$2;shift 2;;
        -F ) FORCE='y';shift;;
        -i ) ONLY_INSTALL_ZSTACK='y' && NEED_NFS='' && NEED_HTTP='' ;shift;;
        -I ) check_myarg $1 $2;MANAGEMENT_INTERFACE=$2 && NEED_SET_MN_IP='y';shift 2;;
        -k ) NEED_KEEP_DB='y';shift;;
        -l ) ONLY_INSTALL_LIBS='y';shift;;
        -L ) check_myarg $1 $2;LICENSE_PATH=$2;shift 2;;
        -m ) INSTALL_MONITOR='y';shift;;
        -M ) UPGRADE_MONITOR='y';shift;;
        -n ) check_myarg $1 $2;NEED_NFS='y' && NFS_FOLDER=$2;shift 2;;
        # -o: do not use yum online repo
        -o ) YUM_ONLINE_REPO='' && ZSTACK_OFFLINE_INSTALL='y' &&  [ "zstack.org" = "$WEBSITE" ] && WEBSITE='localhost';shift;;
        # -O: use yum online repo
        -O ) if [ x"${CHECK_REPO_VERSION}" != x"True" ]; then
                YUM_ONLINE_REPO='y'
                ZSTACK_OFFLINE_INSTALL=''
             else
                fail2 "$PRODUCT_NAME don't support '-O' option! Please remove '-O' and try again."
             fi;
             shift;;
        -P ) check_myarg $1 $2;MYSQL_ROOT_PASSWORD=$2 && MYSQL_NEW_ROOT_PASSWORD=$2;shift 2;;
        -p ) check_myarg $1 $2;MYSQL_USER_PASSWORD=$2;shift 2;;
        -q ) QUIET_INSTALLATION='y';shift;;
        -r ) check_myarg $1 $2;ZSTACK_INSTALL_ROOT=$2;shift 2;;
        # -R: use yum third party repo
        -R ) check_myarg $1 $2;
             if [ x"${CHECK_REPO_VERSION}" != x"True" ]; then
                ZSTACK_PKG_MIRROR=$2
                YUM_ONLINE_REPO='y'
                ZSTACK_OFFLINE_INSTALL=''
             else
                fail2 "$PRODUCT_NAME don't support '-R' option! Please remove '-R' and try again."
             fi;
             shift 2;;
        # -s: skip syncing from repo.zstack.io
        -s ) SKIP_SYNC='y';shift;;
        -t ) check_myarg $1 $2;ZSTACK_START_TIMEOUT=$2;shift 2;;
        -T ) check_myarg $1 $2;MYSQL_PORT=$2;shift 2;;
        -u ) UPGRADE='y';shift;;
        -y ) check_myarg $1 $2;HTTP_PROXY=$2;shift 2;;
        -z ) NOT_START_ZSTACK='y';shift;;
             --mini) MINI_INSTALL='y';shift;;
             --SY) SANYUAN_INSTALL='y';shift;;
             --sds) SDS_INSTALL='y';shift;;
        --) shift;;
        * ) usage;;
    esac
done
相关推荐
pr_note2 天前
legality检查
shell·tcl
啥都不懂的小小白3 天前
Shell脚本编程入门:从零基础到实战掌握
前端·shell
dingdingfish7 天前
GNU Parallel 学习 - 第1章:How to read this book
bash·shell·gnu·parallel
似霰10 天前
Linux Shell 脚本编程——核心基础语法
linux·shell
似霰10 天前
Linux Shell 脚本编程——脚本自动化基础
linux·自动化·shell
偷学技术的梁胖胖yo11 天前
Shell脚本中连接数据库查询数据报错 “No such file or directory“以及函数传参数组
linux·mysql·shell
纵有疾風起20 天前
【Linux 系统开发】基础开发工具详解:软件包管理器、编辑器。编译器开发实战
linux·服务器·开发语言·经验分享·bash·shell
gis分享者22 天前
Shell 脚本中如何使用 here document 实现多行文本输入? (中等)
shell·脚本·document·多行·文本输入·here
柏木乃一22 天前
基础IO(上)
linux·服务器·c语言·c++·shell
angushine23 天前
CPU脚本并远程部署
shell