• 【无标题】shell_43.Linux三种在 shell 脚本中处理选项的方法


    3种在 shell 脚本中处理选项的方法。
    1. 处理简单选项
    先前的 shiftparams.sh 脚本中介绍过如何使用 shift 命令来依次处理脚本的命令行参数。你
    也可以用同样的方法来处理命令行选项。
    在提取单个参数时,使用 "case" 语句来判断某个参数是否为选项:

    1. $ cat extractoptions.sh 
    2. #!/bin/bash 
    3. # Extract command-line options 
    4. echo 
    5. while [ -n "$1"
    6. do 
    7.     case "$1" in 
    8.     -a) echo "Found the -a option" ;; 
    9.     -b) echo "Found the -b option" ;; 
    10.     -c) echo "Found the -c option" ;; 
    11.     *) echo "$1 is not an option" ;; 
    12.     esac 
    13.     shift 
    14. done 
    15. echo 
    16. exit 
    17. $ ./extractoptions.sh -a -b -c -d 
    18. Found the -a option 
    19. Found the -b option 
    20. Found the -c option 
    21. -d is not an option 
    22. $

    "case" 语句会检查每个参数,确认是否为有效的选项。找到一个,处理一个。无论选项在命令行中以何种顺序出现,这种方法都能应对。


    2.分离参数和选项
    经常碰到需要同时使用选项和参数的情况。在 Linux 中,处理这个问题的标准做法是使用特殊字符将两者分开,该字符会告诉脚本选项何时结束,普通参数何时开始。
    Linux 中,这个特殊字符是双连字符(--)。shell 会用双连字符表明选项部分结束。在双连字符之后,脚本就可以放心地将剩下的部分作为参数处理了。
    要检查双连字符,只需在 "case" 语句中加一项即可:

    1. $ cat extractoptionsparams.sh 
    2. #!/bin/bash 
    3. # Extract command-line options and parameters 
    4. echo 
    5. while [ -n "$1"
    6. do 
    7.     case "$1" in 
    8.         -a) echo "Found the -a option" ;; 
    9.         -b) echo "Found the -b option" ;; 
    10.         -c) echo "Found the -c option" ;; 
    11.         --) shift 
    12.             break;; 
    13.         *) echo "$1 is not an option" ;; 
    14.     esac 
    15.         shift 
    16. done 
    17. echo 
    18. count=1 
    19. for param in $@ 
    20. do 
    21.     echo "Parameter #$count: $param" 
    22.     count=$[ $count + 1 ] 
    23. done 
    24. echo 
    25. exit 
    26. $
    27. $ ./extractoptionsparams.sh -a -b -c test1 test2 test3 
    28. Found the -a option 
    29. Found the -b option 
    30. Found the -c option 
    31. test1 is not an option 
    32. test2 is not an option 
    33. test3 is not an option 
    34. $

    进行同样的测试,只是这次会用双连字符将命令行中的选项和参数分开:

    1. $ ./extractoptionsparams.sh -a -b -c -- test1 test2 test3 
    2. Found the -a option 
    3. Found the -b option 
    4. Found the -c option 
    5. Parameter #1: test1 
    6. Parameter #2: test2 
    7. Parameter #3: test3 
    8. $


    3. 处理含值的选项
    (1)有些选项需要一个额外的参数值。在这种情况下,命令行看起来像下面这样:

    $ ./testing.sh -a test1 -b -c -d test2 


    (2)当命令行选项要求额外的参数时,脚本必须能够检测到并正确地加以处理。
    来看下面的处理方法:

    1. $ cat extractoptionsvalues.sh 
    2. #!/bin/bash 
    3. # Extract command-line options and values 
    4. echo 
    5. while [ -n "$1"
    6. do 
    7.     case "$1" in 
    8.         -a) echo "Found the -a option" ;; 
    9.         -b) param=$2 
    10.             echo "Found the -b option with parameter value $param" 
    11.             shift;; 
    12.         -c) echo "Found the -c option" ;; 
    13.         --) shift 
    14.             break;; 
    15.         *) echo "$1 is not an option" ;; 
    16.     esac 
    17.     shift 
    18. done 
    19. echo 
    20. count=1 
    21. for param in $@ 
    22. do 
    23.     echo "Parameter #$count: $param" 
    24.     count=$[ $count + 1 ] 
    25. done 
    26. exit
    27. $ ./extractoptionsvalues.sh -a -b BValue -d 
    28. Found the -a option 
    29. Found the -b option with parameter value BValue 
    30. -d is not an option 
    31. $


    在这个例子中,"case" 语句定义了 3 个要处理的选项。-b 选项还需要一个额外的参数值。
    由于要处理的选项位于$1,因此额外的参数值就应该位于$2(因为所有的参数在处理完之后都会被移出)。
    只要将参数值从$2 变量中提取出来就可以了。当然,因为这个选项占用了两个位置,所以还需要使用 shift 命令多移动一次。
    只需这些基本的功能,整个过程就能正常工作,不管按什么顺序放置选项(只要记住放置好相应的选项参数):

    1. $ ./extractoptionsvalues.sh -c -d -b BValue -a 
    2. Found the -c option 
    3. -d is not an option 
    4. Found the -b option with parameter value BValue 
    5. Found the -a option 
    6. $

    现在 shell 脚本已经拥有了处理命令行选项的基本能力,但还有一些局限。例如,当你想合并多个选项时,脚本就不管用了:

    1. $ ./extractoptionsvalues.sh -ac 
    2. -ac is not an option 
    3. $

  • 相关阅读:
    阿里云linux centos系统jdk版本始终换不了OpenJDK害人
    在Espressif-IDE中使用Wokwi仿真ESP32
    windows server批量创建用户
    PPT文字很多的排版,PPT图片很多的排版,PPT图文排版
    迅为itop-3568开发板qt学习手册上新
    java毕业设计白天鹅造型网mybatis+源码+调试部署+系统+数据库+lw
    初识Spring Boot:构建项目结构与组件解析
    联盟链学习笔记-网络的创建
    【配置】【IDE】使用vscode调试typescript应用
    python爬虫之BeautifulSoup4使用
  • 原文地址:https://blog.csdn.net/mmmmm168m/article/details/134000386