• shift移动变量


    1、移动变量

    脚本 sh05.sh 

    #!/bin/bash
    # Program
    #       Program shows the effect of shift function
    # History:
    # 2015/9/6      zengdp  First release
    PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
    export PATH
    
    
    echo "Total parameter number is     ==> $#"
    echo "You whole parameter is        ==> '$@'"
    shift
    echo "Total parameter number is     ==> $#"
    echo "You whole parameter is        ==> '$@'"
    shift 3
    echo "Total parameter number is     ==> $#"
    echo "You whole parameter is        ==> '$@'"

    运行 sh sh05.sh one two three four five six 

    得到

    Total parameter number is     ==> 6
    You whole parameter is        ==> 'one two three four five six'
    Total parameter number is     ==> 5
    You whole parameter is        ==> 'two three four five six'
    Total parameter number is     ==> 2
    You whole parameter is        ==> 'five six'

    光看结果你就可以知道啦,那个 shift 会移动变量,而且 shift 后面可以接数字,代表拿掉最前面的几个参数的意思。 上面的运行结果中,第一次进行 shift 后他的显示情况是『 one two three four five six』,所以就剩下五个啦!第二次直接拿掉三个,就变成『 two three four five six 』啦! 

    2、 当使用shift移动一个变量时,使用while输出第一个变量,从而输出在输入时所有的变量

    文件名为 test13.sh 

     1 #!/bin/bash
     2 # demostrating the shift command
     3 
     4 count=1
     5 while [ -n "$1" ]
     6 do
     7   echo "Parameter #$count=$1"
     8   count=$[$count + 1]
     9   shift
    10 done

    脚本执行一个while循环,测试第一个参数的长度,当第一个参数值长度为0时,结束循环,测试第一个参数后,使用shift命令将所有参数左移一个位置

    sh test13.sh rich barbara katie jessica

    输出为:

    Parameter #1=rich
    Parameter #2=barbara
    Parameter #3=katie
    Parameter #4=jessica

    3、从参数中分离选项

    执行shell脚本时经常会遇到既需要使用选项有需要使用参数的情况,在Linux中的标准方式是通过特殊字符吗将二者分开,这个特殊字符吗告诉脚本选项结束和普通参数开始的位置
    对于Linux,这个特殊字符吗就是双破折号(--),shell使用双破折号知识选项列表的结束,发现双破只好后,脚本就能够安全的将剩余的命令行参数作为参数而不是选项处理

    文件名为 test16.sh 

     1 #!/bin/bash
     2 #  extracting options and parameters
     3 
     4 while [ -n "$1" ]
     5 do
     6   case "$1" in
     7   -a) echo "Found the -a option" ;;
     8   -b) echo "Found the -b option";;
     9   -c) echo "Found the -c option" ;;
    10   --) shift
    11       break ;;
    12   *) echo "$1 is not an option";;
    13   esac
    14   shift
    15 done
    16 
    17 count=1
    18 for param in $@
    19 do
    20   echo "Parameter #$count: $param"
    21   count=$[ $count + 1 ]
    22 done

    这个脚本使用break命令使得遇到双破折号时从while循环中跳出,因为提前从循环中跳出,所以需要保证加入一个shift命令将双破折号从参数变量中丢弃

    第一个测试,尝试使用普通选项和参数集运行脚本

    sh test16.sh -a -b -c test1 test2 test3

    输出为:

    Found the -a option
    Found the -b option
    Found the -c option
    test1 is not an option
    test2 is not an option
    test3 is not an option

    第二次测试,使用双破折号将命令行中选项和参数分开

    sh test16.sh -a -b -c -- test1 test2 test3

    输出结果为

    Found the -a option
    Found the -b option
    Found the -c option
    Parameter #1: test1
    Parameter #2: test2
    Parameter #3: test3

    这时可以看到输出的结果中,因为识别到'--'符号,便跳出了while循环,然后使用下一个循环中输出剩下的变量

  • 相关阅读:
    006 date find
    005 输出重定向 > >>命令 echo命令 tail命令
    总结,一周,
    mokey 学习
    树状,
    定制,
    萌芽,
    到底为什么,
    会,
    “恋爱”,一路走来,
  • 原文地址:https://www.cnblogs.com/jacson/p/4786554.html
Copyright © 2020-2023  润新知