Shell command-line argument parsing

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.