Parameters of the AIX operating system shell

Source: Internet
Author: User
Tags command line variables variable
command options files Some variables related to the arguments passed to the shell: the number of $# command line arguments $? The return value of the calling command $$ the process number of the current process $! The process number of the last background command the first parameter of the command line, which is the command name $n the nth parameter of the command line, is equal to ("$ ...") if you want to take more than 9 parameters ${n}$* all command-line arguments. $@ "All command line arguments equal to (" $ "" $ "...) $_ the last command-line argument of the previous command there are generally two main ways to parse the options passed to the shell script. The first approach is to use the above shell variables and shift commands, with the case command to direct analysis, in the number of fewer parameters, this method is more commonly used. However, if the number of parameters is higher, you need to use the getopts command.

Command format:

Getopts option-string Variable

Getops the process of parsing the parameters passed by the command line is:

1. Getopts checks all command-line arguments to find parameters that start with the character "-".

2. When you find a parameter that starts with the character "-", compare the string after the "-" and the option-string.

3. If a matching match is found, the specified variable is set, otherwise the variable is formatted as a? character.

4. Repeat 1 through 3 until all the options are processed.

5. When the analysis is complete, GETOPTS returns a non 0 return value, which is often used to make a cyclic judgment condition. In addition, when getopts ends, it also sets the variable optind to the subscript of the last parameter.

When you need to follow an additional parameter (such as-I intputfile) after the item is selected, you need to add a colon (such as I:) to the corresponding option in option-string. In this case, when parsing to this option, additional parameters are assigned to the OPTARG variable.

Example:

The following shell list will be able to analyze:

-I specifies the filename entered

-o Specifies the file name of the output

-v Specifies to use verbose mode

Shell Script list:

#!/usr/bin/ksh

Usage= "USAGE: $" [-v] [-i] [filename] [-o] [filename] ' "

Verbose=false

While Getopts o:f:v OPTION; Todo

Case "$OPTION" in

o) outfile= "$OPTARG";;

i) infile= "$OPTARG";;

v) verbose=true;;

\?) echo "$USAGE";

Exit 1

;;

Esac

Done

echo Output file is $OUTFILE

echo Input file is $INFILE

echo in VERBOSE Mode: $VERBOSE



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.