Linux Command Line options and parameters, linux Command Line options

Source: Internet
Author: User
Tags linux command line options

Linux Command Line options and parameters, linux Command Line options

1. main function parameter form

Int main (int argc, char * argv [], char * env []); // The first argc parameter indicates the number of parameters in the command line // The second parameter points to each parameter in turn, for example, "ls-l", argv [0] points to "ls ", argv [1] points to "-l", argv [2] is NULL // The third parameter is the list of environment variables of the current process

2. Command Line Parameter Identification

When you enter a command in the command line, the options are randomly located. For example, the effects of ls-l-a and ls-a-l are the same.

2.1. getopt (int argc, char * argv [], char * optstring) introduces the header file "unistd. h"

The first and second parameters are the same as the first two parameters of the main function. The third parameter optstring defines the options and parameters.

(1) A single character indicates that this option has no parameters

(2) A single character is followed by a colon, indicating that there must be parameters after this option, and the options and parameters are separated by spaces.

(3) A single character is followed by a double colon, indicating that the option can be followed by a parameter or no parameter; if there is a parameter, the parameter must be followed by the option

For example:

AB: c ::

Option a has no parameter; Option B has a parameter separated by spaces; Option c can have a parameter or no parameter. If there is a parameter, the parameter must be followed by the option

There are also four global variables related to getopt.

Int optopt: Storage Options that do not meet optstring requirements

Char * optarg: pointer to the parameter of the current option

Int optind: After determining the options and corresponding parameters, point to the next argv parameter.

Int opterr: if it is set to 0, getopt does not want stderr to output error messages.

 

Call principle:

After each call, the corresponding option is returned, so that the optarg points to the parameter corresponding to the option. If the option does not meet the optstring rule '? And save this option to optopt. If the parsing is complete,-1 is returned.

After the resolution, getopt will rearrange the parameters in argv. All the non-conforming options and parameters will be placed at the end of argv (where the options are prior and parameters are ), and make optind point to the starting subscript of these non-conforming option parameters in argv.

1 # include <stdio. h> 2 # include <unistd. h> 3 4 int main (int argc, char ** argv, char ** env) {5 int res; 6 opterr = 0; // Do Not Display Error Message 7 while (res = getopt (argc, argv, "AB: c ::"))! =-1) {8 switch (res) {9 case 'A ': 10 printf ("option = a \ toptarg = % s \ toptopt = % c \ toptind = % d \ targv [optind]-> % s \ n", optarg, optopt, optind, argv [optind]); 11 break; 12 case 'B ': 13 printf ("option = B \ toptarg = % s \ toptopt = % c \ toptind = % d \ targv [optind]-> % s \ n", optarg, optopt, optind, argv [optind]); 14 break; 15 case 'C ': 16 printf ("option = c \ toptarg = % s \ toptopt = % c \ toptind = % d \ targv [optind]-> % s \ n", optarg, optopt, optind, argv [o Ptind]); 17 break; 18 case '? ': 19 printf ("option =? \ Toptarg = % s \ toptopt = % c \ toptind = % d \ targv [optind]-> % s \ n ", optarg, optopt, optind, argv [optind]); 20 break; 21} 22 23} 24 printf ("OVER \ noptarg = % s \ toptopt = % c \ toptind = % d \ targv [optind]-> % s \ n", optarg, optopt, optind, argv [optind]); // print the final value of the three global variables: 25 int I = 1; 26 printf ("************************************ * ***** \ n "); 27 for (; I <argc; I ++) {// print the re-ordered command line parameter 28 printf ("% s \ n", argv [I]); 29} 30}

Sudo./main-a-B bbb-c ccc-d ddd
Option = a optarg = (null) optopt = optind = 2 argv [optind]->-B
Option = B optarg = bbb optopt = optind = 4 argv [optind]->-c
Option = c optarg = (null) optopt = optind = 5 argv [optind]-> ccc
Option =? Optarg = (null) optopt = d optind = 7 argv [optind]-> ddd
OVER
Optarg = (null) optopt = d optind = 6 argv [optind]-> ccc
**************************************** **
-
-B
Bbb
-C
-D
Ccc
Ddd

 

2.2. getopt_long

It not only recognizes short options (command-short options), but also supports Long Options (command-long options ).

Int getopt_long (int argc, char ** argv, char * shortOpt, struct option * longOpt, int * index) Note: The struct option should introduce the header file "getopt. h"

The first three parameters are the same as the getopt parameter, short options defined by shortOpt, and the form of parameters.

The fourth parameter defines the struct option array,

The fifth parameter record length option is located in the subscript of the longOpt array. Generally, it is set to NULL.

Struct option {
Const char * name; // long option name
Int has_arg; // 0 indicates that this option does not have a parameter. 1 indicates that a parameter is required, and 2 indicates optional.
Int * flag; // if it is NULL, getopt_long returns val. Otherwise, the val value is assigned to the integer variable pointed to by the flag. The return value of the function is 0.
Int val; // It is generally set to the short option corresponding to the long option. If the flag is NULL, val is used as the return value of the function. Otherwise, val is assigned to the variable pointed to by flag.
};

If you do not want to support short options, set shortOpt to "" an empty string, but not NULL.

1 # include <stdio. h> 2 # include <unistd. h> 3 # include <getopt. h> 4 int main (int argc, char ** argv, char ** env) {5 int res; // save function return value 6 struct option longOption [4] = {// define long option array 7 {"help", 0, NULL, 'H '}, 8 {"version", 0, NULL, 'V'}, 9 {"output", 1, NULL, 'O'}, 10 {NULL, 0, NULL, 0} 11}; 12 opterr = 0; // No error message 13 int index is output; // The long option is located at the subscript of longOption 14 while (-1! = (Res = getopt_long (argc, argv, "hvo:", longOption, & index) {15 switch (res) {16 case 'H ': 17 printf ("option = help \ n"); 18 break; 19 case 'V': 20 printf ("option = version \ n"); 21 break; 22 case 'O': 23 printf ("option = output, output path = % s \ n", optarg); 24 break; 25 case '? ': 26 printf ("error \ n"); 27 break; 28} 29 printf ("index in longOption is % d \ n", index ); 30} 31 32 return 0; 33}

Output:

Sudo./main-h
Option = help
Index in longOption is-1218614280 // for short options, the fifth parameter of the function is invalid.

Sudo./main-o/home/out
Option = output, output path =/home/out
Index in longOption is-1219220488

Sudo./main -- version
Option = version
Index in longOption is 1

Sudo./main -- output/home/out
Option = output, output path =/home/out
Index in longOption is 2

 

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.