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
相关推荐
开挖掘机上班1 天前
shell批量添加新用户
linux·服务器·shell
L_Xian1 天前
Android混合Flutter项目打包aar细说
flutter·shell·dart
꧁༺朝花夕逝༻꧂1 天前
docker详细操作--未完待续
linux·nginx·docker·shell
Johny_Zhao1 天前
基于CentOS Stream 8的物联网数据采集与展示方案
linux·网络·python·mqtt·mysql·网络安全·信息安全·云计算·shell·yum源·系统运维
码农秋3 天前
快刀集(1): 一刀斩断视频片头广告
shell·快刀集·一闪
vortex53 天前
探索 Shell:选择适合你的命令行利器 bash, zsh, fish, dash, sh...
linux·开发语言·bash·shell·dash
itachi-uchiha4 天前
awk处理xml文件&&封装集合变量和调用
xml·shell·awk
粉红色回忆5 天前
在bash中进行基本数值计算
shell
粉红色回忆6 天前
linux 如何自定义文件描述符
shell
薛定谔的猫_C8T67 天前
程序人生-Hello’s P2P
c语言·汇编·程序人生·shell·二进制·计算机系统·hello