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 know the maximum number of parameters supported by Shell

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to know the maximum number of parameters supported by Shell". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In the process of scripting, the input of parameters is usually involved. For example, sh 1.sh 10 20, in the execution of the 1.sh script, 10 is the first parameter and 20 is the second parameter. Sometimes there is the question of how many variables can a shell script support? In addition to doubt, the following script is written to find out the maximum number of parameters that can be entered in the Shell script.

The script involves three small scripts: 1.sh 2.sh 3.sh-where script names are extremely simple for convenience. The general idea is to give a specific value n as the maximum number of input parameters, and then take 1Magne2meme3. N as the input parameter of the script, which is achieved through the script 2.sh, and then output these parameters as is. If all these parameters can be output, it means that the given n is reasonable. This is done through the script 3.sh. The function of 1.sh is to generate n incrementally through an endless loop, and to determine whether n is reasonable by referencing 2.sh. See the following script for details.

First, let's take a look at the first script, 1.sh.

The code is as follows:

#! / bin/bash

ITunes 0

Echo 0 > currnumber

While true

Do

ITunes $[$item1]

Sh 2.sh $I

If [$?-ne 0]; then

Echo $I > maxnumber

Exit 1

Else

Sed-i'1s Universe currnumber &'$ispur Placement'

Fi

Done

The script mainly provides an endless loop. $I refers to the number of parameters that can be entered. 2.sh is used to determine whether the number of given parameters is reasonable. If so, append the value to the currnumber file. If not, it means that $iMub 1 is the maximum number of parameters acceptable to shell. The numeric value is output to a maxnumber file.

The application of currnumber file makes it easy to detect the execution of scripts. Originally intended to be echo $I > > currnumber, that is, each reasonable value will output one line, considering that the file has a maximum number of lines, here, the value will be output to one line. This is done by sed-i'1s currnumber, which adds the value of $I to the end of the line.

Let's take a look at the script 2.sh

The code is as follows:

#! / bin/bash

Rm-f 1.test

Touch 1.test

Num=$1

Echo "#! / bin/bash" > 1.test

Echo "sh 3.sh" > > 1.test

For ((iTun1; I 2.test)

Num=$#

The maximum number of input parameters allowed by the shell script can be calculated by for ((item1 sh 1.sh).

2 > because of the limited conditions, the value is not calculated in detail. But we can skip 1.sh and simply use 2.sh to determine a specific value. For example, sh 2.sh 100000, there is no problem with 100000 input parameters tested.

3 > the highlight of the script is how to append a specific value to the end of the line, which is mainly achieved through the sed-i'1s 2.test, where 1s represents the first line. $represents the end of the line.

4 > in vim, 0 can skip to the beginning of the line and $can skip to the end of the line.

5 > shift moves the position of the input parameter to the left. The default is 1 bit to the left. For example, shift 3 means that the original $4 is now $1, the original $5 is now $2, and so on, the original $1, $2, $3 are discarded, and $0 is not moved.

6 > the hidden danger of this script is that the lines of the text file have the maximum character limit. However, when testing with 100000 as the number of input parameters, there is no problem, indicating that a line of text files can still hold a considerable number of characters.

PS: when monitoring the results of 2.test, we can use watch cat 2.test, that is, to check the contents of 2.test every two seconds, but this method has the disadvantage that when there is too much data, it cannot be displayed on a screen. It will only display the previous fixed data, and the new data will not be displayed, but in this case, we are more concerned about whether the data is increased. The following script does this:

The code is as follows:

#! / bin/bash

While true

Do

Cat 2.test

Sleep 30

Done

This is the end of the content of "how to know how many parameters Shell supports". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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