(031) The shell location parameters of Linux

Source: Internet
Author: User

Fundamentals of the ten-year OPS series-Linux

Zeng Lin

Contact: [Email protected]

Website: www.jplatformx.com

Copyright: Please do not reprint the article without permission

First, Introduction

We have not had the ability to receive and process command-line options and parameters before. This section describes shell features that allow programs to access the command-line content.

Second, access the command line

The shell provides a set of variables called positional parameters that are used to store the keywords in the command line, each of which is ordered as 0~9. These variables can be shown in the following ways.

1#!/bin/Bash2 3# posit-param:script to view command line parameters4 5 Echo "6\$0= $07\$1= $18\$2= $29\$3= $3Ten\$4= $4 One\$5= $5 A\$6= $6 -\$7= $7 -\$8= $8 the\$9= $9 - "

This simple script shows the value from the variable to the $9. The result of executing this script without any command-line arguments is as follows:

Even if no arguments are provided, the variable $ $ always stores the first item of data that is displayed on the command line, which is the path name of the executing program. Now let's take a look at the results of the program execution in the case of providing the arguments.

Note that with the parameter extension technique, the user can actually get more than 9 parameters. To mark a number greater than 9, enclose the number in curly braces, such as ${10}, ${11}, ${888}, and so on.

Iii. determining the number of actual parameters

The shell also provides the variable $ #以给出命令行参数的数目. As shown in the following code:

#!/bin/bash# posit-param:script to view command line parametersEcho "Number of arguments: $#"Echo "\$0= $0\$1= $1\$2= $2\$3= $3\$4= $4\$5= $5\$6= $6\$7= $7\$8= $8\$9= $9"

The results of the above program operation are as follows:

Iv. shift--Processing a large number of arguments

But what happens if the program provides a lot of real attendance? As shown below:

In this case, the wildcard "*" expands to 26 arguments. How can we handle so many parameters? The shell provides a slightly clumsy way to handle it. Each time the shift command is executed, the values of all parameters are "down one". In fact, with the shift command, we can do all of the program tasks by processing only one parameter (a parameter other than $ one, a constant value of $). The following programs:

#!/bin/shift. SH : script to display all argumentscount=1and0do        echo " Argument $count = $ "         Count1))         Shift  Done

Each time the shift command is executed, the value of the variable $ value is assigned to $ $, and the value of $ $ is assigned to the variable, and so on. The variable $ #的值同时减去1. Therefore, the above code execution results as shown:

(031) The shell location parameters of Linux

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.