Getopts in the shell.

Source: Internet
Author: User
Tags creative commons attribution

by Francis_hao Jul 5,2017

Getopts is a built-in command for the shell.

Overview

Getopts optstringname [args]
Optind,optarg,opterr

Describe

Getopts is used by the shell program to analyze positional parameters,optstring contains the option characters that need to be recognized, if the character followed by a colon, indicating that the word check requires a parameter, the parameters need to be separated by a space. Colons and question marks cannot be used as option characters.

Getopts each time it is called, it places the next option character on the variable name and initializes it if the name does not exist.

The variable optind is the index of the next positional parameter to be processed, with an initial value of 1. The shell does not automatically reset the Optind, and if you want to analyze the position parameters again, you need to reset the optind manually.

When a parameter is required for an election, getopts places the argument in the variable optarg,

When all positional parameter parsing is complete, getopts exits and returns a value greater than 0, Optind becomes the first index of the position parameter that is not the option, and name is set to '? '.

By default, getopts parses the incoming positional parameters, but if args has a value, only the parameters in args are parsed, and args only supports forms such as-ABC.

If an error is encountered, getopts prints out an error message, but if the optstring first character is a colon, or the variable opterr is set to 0 (this item simply does not print an error message and does not have a value of Optarg after an error), it enters silent mode. Error messages are not printed. There are usually two types of errors encountered:
1. Invalid option, name will be set to '? ' at this time. In silent mode, this invalid option is placed in Optarg. Non-silent mode, prints error messages, and unset optarg.
2, the option required parameters are not provided, in silent mode, name will be set to ': ', invalid option will be placed in Optarg. Non-silent mode, name is set to '? ', unset optarg and print error message.

Getopts returns True (0) If an option is encountered (whether or not valid), returns False if it encounters an end or generates an error.

Example

Example from reference "2"

#!/bin/sh
whilegetopts:Ab:C:OPTION; Do
Case$OPTIONinch
A)Echo "Get Option A"
;;
B)Echo "Get option B and parameter is $OPTARG"
;;
C)echo"get option C and parameter is $OPTARG"
;;
?) Echo "Get a non option $OPTARG and option is $OPTION"
;;
Esac
Done

A running instance

Where-A and-B are the correct options, and the-P and-C respectively correspond to the two cases of the error option.


This article was authored by Liu Yinghao and licensed under the Creative Commons Attribution-NonCommercial use-Share 3.0 Chinese mainland license agreement in the same way. Welcome reprint, please specify the source:
Reprinted from: http://www.cnblogs.com/yinghao1991/p/7123550.html

Reference

"1" Man bash

"2" D. tes. Linux and Unix shell Programming Guide. Mechanical industry press. 2000-1

Getopts in the shell.

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.