Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use shell position parameters under Linux

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use the shell position parameters under Linux. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Before the introduction, we did not deal with the program's ability to receive and process command-line options and parameters. This section describes the shell feature that allows programs to access command-line content.

Second, accessing the command line shell provides a set of variables called position parameters, which are used to store keywords on the command line. You can show these variables in the following ways.

1 #! / bin/bash 2 3 # posit-param: script to view command line parameters 4 5 echo "6\ $0 = $07\ $1 = $1 8\ $2 = $29\ $3 = $3 10\ $4 = $4 11\ $5 = $5 12\ $6 = $6 13\ $7 = $7 14\ $8 = $8 15\ $9 = $9 16" this simple script shows the value from variable to 9. The result of executing this script without any command line arguments is as follows:

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

Note that using the parameter extension technique, users can actually get more than 9 parameters. To mark a number greater than 9, enclose the number in curly braces, such as {11}, ${888}, etc.

Third, determine the number of arguments shell also provides the variable $# to give the number of command line arguments. The following code is shown:

#! / bin/bash # posit-param: script to view command line parameters echo "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 running results of the above programs are as follows:

4. Shift-- handles a large number of arguments, but what happens if you provide a large number of arguments to the program? As follows:

In this case, the wildcard "*" extends to 26 arguments. How can we deal with so many parameters? Shell provides a slightly clumsy approach to processing. Each time the shift command is executed, the values of all parameters are "moved down one bit". In fact, with the shift command, we can complete all program tasks by dealing with only one parameter (one parameter other than the one with a constant value of 0). The procedure is as follows:

#! / bin/bash # shift.sh: script to display all arguments count=1 while [[$# > 0]]; do echo "Argument $count=$ 1" count=$ ((count + 1)) shift done each time the shift command is executed, the value of the variable is assigned to 1, the value of the variable is assigned to variable 2, and so on. The value of the variable $# is also subtracted by 1. Therefore, the execution result of the above code is shown in the following figure:

5. Use position parameters in the shell function just as position parameters can be used to pass parameters to shell scripts, and position parameters can also be used to pass arguments to shell functions, as shown in the following script:

1 file_info () {2 if [[- e "$1"]] Then 3 echo "File Type:" 4 file "$1" 5 echo "File Status:" 6 stat "$1" 7 else 8 echo "$FUNCNAME: usage: $FUNCNAME file" > & 29 return 1 10 fi 11} now, if a script containing the file_info function calls file_info with a file name as an argument, this argument is passed to the file_info function.

Under such conditions, we can write a lot of useful shell functions that can be used not only in ordinary scripts, but also in .bashrc files.

In addition, it is important to note that the shell variable of FUNCNAME, shell automatically updates FUNCNAME to track the currently executed shell function.

This is the end of this article on "how to use shell position parameters under Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report