Detailed explanation of linux shell command line options and parameter usage

Keywords: Linux shell

Detailed explanation of linux shell command line options and parameter usage


In bash, there are three ways to handle command line arguments

- direct processing: use $1, ,...,$n for parsing

- getopts: the case of a single character option (for example: - n 10 -f file.txt Etc.)

- getopt: can handle single character options or long option (such as: - prefix=/home, etc.)

difference:

- small script can be processed directly

- getopts can handle most cases

- getopt is more complex and powerful.

1. Direct processing

Use the following variables for processing:

 

$0 is the command itself, equivalent to argv[0] in c/c + +
$1 first parameter
 $2, $3, $4... (parameters 2, 3, 4, and so on)
The number of $ාාparameters, excluding the command itself
 List of $@ ා parameters themselves, excluding the command itself
 $* ාis the same as $@, but "$*" and "$@" (quoted) are not the same,
     #"$*" interprets all parameters into a string, while "$@" is an array of parameters

 

2,getopts

  • -getopts is bash's internal command
  • getopts has two parameters. The first parameter is a string, including characters and ":
  • Each character is a valid option. If the character is followed by ":", it means that this option has its own argument, which is saved in the built-in variable optarget
  • $opt ind always stores the next element location to be processed in the original $*
  • For while getopts ":a:bc" opt, the first colon means ignore the error

for example getopts.sh :

#!/bin/bash

echo original parameters=[$*]
echo original OPTIND=[$OPTIND]
while getopts ":a:bc" opt
do
    case $opt in
        a)
            echo "this is -a option. OPTARG=[$OPTARG] OPTIND=[$OPTIND]"
            ;;
        b)
            echo "this is -b option. OPTARG=[$OPTARG] OPTIND=[$OPTIND]"
            ;;
        c)
            echo "this is -c option. OPTARG=[$OPTARG] OPTIND=[$OPTIND]"
            ;;
        ?)
            echo "there is unrecognized parameter."
            exit 1
            ;;
    esac
done
#adopt shift $(($OPTIND - 1))Treatment of, $*Only the parameters to remove the contents of the options are retained in,
#It can be processed in later shell programs
shift $(($OPTIND - 1))

echo remaining parameters=[$*]
echo \$1=[$1]
echo \$2=[$2]

 

 
Test it:
 
# ./getopts.sh -a 12 -b -c file1 file2
original parameters=[-a 12 -b -c file1 file2]
original OPTIND=[1]
this is -a option. OPTARG=[12] OPTIND=[3]
this is -b option. OPTARG=[] OPTIND=[4]
this is -c option. OPTARG=[] OPTIND=[5]
remaining parameters=[file1 file2]
$1=[file1]
$2=[file2]

 

3,getopt

explain:

  • getopt is an external command, not a bash built-in command. Linux distribution usually comes with it
  • getopt supports short and long options
  • There are many problems with the old version of getopt. The enhanced version of getopt is easy to use. Execute the command getopt -T; echo $?. if output 4, it means the enhanced version
  • If the short option has an argument and the argument is optional, the argument must adhere to the option, such as - c arg, but not - c arg
  • If the long option has argument and the parameter is optional, the "=" is used between the argument and the option, such as -- clone = Arg instead of -- clone ARG

for example getopt.sh :


#!/bin/bash

echo original parameters=[$@]

#-o or--options Options are followed by acceptable short options, such as ab:c::,Indicates that the acceptable short options are-a -b -c,
#among-a Options do not take arguments,-b Option must be followed by a parameter,-c Option is optional
#-l or--long Options are followed by acceptable long options separated by commas. Colons mean the same as short options.
#-n Option followed by script name prompted when option parsing error
ARGS=`getopt -o ab:c:: --long along,blong:,clong:: -n "$0" -- "$@"`
if [ $? != 0 ]; then
    echo "Terminating..."
    exit 1
fi

echo ARGS=[$ARGS]
#Assign normalized command line parameters to position parameters( $1,$2,...)
eval set -- "${ARGS}"
echo formatted parameters=[$@]

while true
do
    case "$1" in
        -a|--along) 
            echo "Option a";
            shift
            ;;
        -b|--blong)
            echo "Option b, argument $2";
            shift 2
            ;;
        -c|--clong)
            case "$2" in
                "")
                    echo "Option c, no argument";
                    shift 2  
                    ;;
                *)
                    echo "Option c, argument $2";
                    shift 2;
                    ;;
            esac
            ;;
        --)
            shift
            break
            ;;
        *)
            echo "Internal error!"
            exit 1
            ;;
    esac
done

#Process remaining parameters
echo remaining parameters=[$@]
echo \$1=[$1]
echo \$2=[$2]

 


 
Test it:
 
#Short options
# ./getopt.sh -a -b1 -c2 file1 file2
original parameters=[-a -b1 -c2 file1 file2]
ARGS=[ -a -b '1' -c '2' -- 'file1' 'file2']
formatted parameters=[-a -b 1 -c 2 -- file1 file2]
Option a
Option b, argument 1
Option c, argument 2
remaining parameters=[file1 file2]
$1=[file1]
$2=[file2]

#Long options
./getopt.sh --along --blong=1 --clong=2 file1 file2
original parameters=[--along --blong=1 --clong=2 file1 file2]
ARGS=[ --along --blong '1' --clong '2' -- 'file1' 'file2']
formatted parameters=[--along --blong 1 --clong 2 -- file1 file2]
Option a
Option b, argument 1
Option c, argument 2
remaining parameters=[file1 file2]
$1=[file1]
$2=[file2]

#Long short mix
# ./getopt.sh -a -b1 --clong=2 file1 file2
original parameters=[-a -b1 --clong=2 file1 file2]
ARGS=[ -a -b '1' --clong '2' -- 'file1' 'file2']
formatted parameters=[-a -b 1 --clong 2 -- file1 file2]
Option a
Option b, argument 1
Option c, argument 2
remaining parameters=[file1 file2]
$1=[file1]
$2=[file2]

 

 
For the case of an optional parameter error:
#Space between short option and argument
# ./getopt.sh -a -b 1 -c 2 file1 file2
original parameters=[-a -b 1 -c 2 file1 file2]
ARGS=[ -a -b '1' -c '' -- '2' 'file1' 'file2']
formatted parameters=[-a -b 1 -c -- 2 file1 file2]
Option a
Option b, argument 1
Option c, no argument
remaining parameters=[2 file1 file2]
$1=[2]
$2=[file1]

#Space between long option and argument
# ./getopt.sh --along --blong 1 --clong 2 file1 file2
original parameters=[--along --blong 1 --clong 2 file1 file2]
ARGS=[ --along --blong '1' --clong '' -- '2' 'file1' 'file2']
formatted parameters=[--along --blong 1 --clong -- 2 file1 file2]
Option a
Option b, argument 1
Option c, no argument
remaining parameters=[2 file1 file2]
$1=[2]
$2=[file1]

 




Posted by b-ware on Mon, 08 Jun 2020 21:20:15 -0700