#! /Bin/bash # Extracting command text_text_line options as parameters Help_info (){ Echo "NAME" Echo "\ t $0" Echo "SYNOPSIS" Echo "\ t $0 is a shell test about process options" Echo "DESCRIPTION" Echo "\ toption like-a-B param1-c param2-d" } If [$ #-lt 0] Then Help_info Fi Nomal_opts_act () { Echo-e "\ n ### nomal_opts_act ###\ n" While [-n "$1"] Do Case "$1" in -) Echo "Found the-a option" ;; -B) Echo "Found the-B option" Echo "The parameter follow-B is $2" Shift ;; -C) Echo "Found the-c option" Echo "The parameter follow-c is $2" Shift ;; -D) Echo "Found the-d option" ;; *) Echo "$1 is not an option" ;; Esac Shift Done } # Using shell commands to build options for parsing can be implemented according to your own ideas # Advantages: self-built, not impossible, unexpected # Disadvantages: Trouble Getopt_act () { Echo-e "\ n ### getopt_act ###\ n" GETOPTOUT = 'getopt AB: c: d "$ @"' Set -- $ GETOPTOUT While [-n "$1"] Do Case $1 in -) Echo "Found the-a option" ;; -B) Echo "Found the-B option" Echo "The parameter follow-B is" $2 "" Shift ;; -C) Echo "Found the-c option" Echo "The parameter follow-c is" $2 "" Shift ;; -D) Echo "Found the-d option" ;; --) Shift Break ;; *) Echo "Unknow option:" $1 "" ;; Esac Shift Done Param_index = 1 For param in "$ @" Do Echo "Parameter $ param_index: $ param" Param_index = $ [$ param_index + 1] Done } # Use the getopt command to parse options and Parameters # Advantage: Compared with getopts, It is semi-automated parsing. Options and parameters are automatically organized and separated by the -- symbol. # Disadvantages: disadvantages of getopts #1. You need to work with the set -- command. It is not required. You need to manually shift #2. Space is not supported in option parameters, such as-a-B dog-c "earth moon"-d-f param1 param2, parsing errors will occur. Getopts_act () { Echo-e "\ n ### getopts_act ###\ n" While getopts: AB: c: d ARGS Do Case $ ARGS in A) Echo "Found the-a option" ;; B) Echo "Found the-B option" Echo "The parameter follow-B is $ OPTARG" ;; C) Echo "Found the-c option" Echo "The parameter follow-c is $ OPTARG" ;; D) Echo "Found the-d option" ;; *) Echo "Unknow option: $ ARGS" ;; Esac Done Shift $ [$ OPTIND-1] Param_index = 1 For param in "$ @" Do Echo "Parameter $ param_index: $ param" Param_index = $ [$ param_index + 1] Done } # Getopts Command Parsing options and Parameters # Advantage: the parameter can contain spaces such as-c "earth moon" # There can be no space between option letters and parameter values, for example,-bdog # Undefined options can be bound? Output # Unknow option :? Nomal_opts_act-a-B dog-c earth-d-f param1 param2 Getopts_act-a-B dog-c "earth moon"-d-f param1 param2 Getopt_act-a-B dog-c earth-d-f param1 param2 |