1. Manually parse parameters, position parameters
(1) $#: Number of parameters
(2) $1...$9: first parameter ... 9th parameter
2. Built-in command parsing, getopts, long parameter format not supported
Command format: getopts option_string variable
The first argument is a string, including the character and ":", each character is a valid option if the character is followed by ":", indicating that the character has its own arguments. If there is no ":" Symbol after the character, it does not have its own argument
Getopts takes these parameters from the command and deletes "-" and assigns them to the second argument, and if it has its own argument, the parameter is assigned in "Optarg"
test.sh
#!/bin/sh
While getopts "B:V:" opt
Do
Case $opt in
b
echo "b parameter value: $OPTARG"
;;
V
echo "v parameter value: $OPTARG"
;;
Esac
Done
Use:
./test.sh or./test.sh-b value or./test.sh-v 0.1 or./test.sh-b value-v 0.1
Shell command-line argument parsing