The maximum number of arguments the shell supports

Source: Internet
Author: User

This article turns from: http://www.jb51.net/article/56548.htm This article mainly describes how many parameters the shell supports? This article is a test article on how many parameters a shell can enter, and the friend you need can refer to the following

During scripting, input to parameters is usually involved. For example, SH 1.sh 10 20, in the execution of 1.sh this script, 10 is the first parameter, 20 is the second parameter. Sometimes there is this confusion about how many variables can a shell script support? Confused, the following script was written to find out the maximum number of parameters that can be entered in a shell script.

The script involves three small scripts: 1.sh 2.sh 3.sh--Here for convenience, the name of the foot is extremely simple. The general idea is to give a specific value n, as the maximum number of input parameters, and then use 1,2,3...N as the input parameter of the script, which is implemented by script 2.sh, and then output these parameters as-is. If all of these parameters can be output, it is reasonable to represent the given n. This is done through script 3.sh. The function of 1.sh implementation is to determine whether n is reasonable by referring to 2.sh through a dead loop, incrementing the generation of N. Specifically, the following script is visible.

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

Copy CodeThe code is as follows:
#!/bin/bash
I=0
echo 0 > Currnumber
While True
Do
i=$[$i +1]
SH 2.sh $i
If [$?-ne 0];then
echo $i > MaxNumber
Exit 1
Else
Sed-i ' 1s/$/& ' $i '/' Currnumber
Fi
Done

The script mainly provides a dead loop, $i refers to the number of input parameters, 2.sh is used to determine whether the number of parameters given is reasonable, if reasonable, the value is appended to currnumber this file, if not reasonable, then the $i-1 is the maximum number of parameters the shell can accept. The value is output to the MaxNumber file.

The application of the Currnumber file facilitates the detection of script execution. Originally intended to be echo $i >> Currnumber, that is, each reasonable value is output one line, considering the maximum number of rows of the file limit, here, the value is output to one line. Sed-i ' 1s/$/& ' $i '/' Currnumber implements this function, adding the value of $i to the end of the line.

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

Copy CodeThe 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 ((I=1; i<= $num; i++))
Do
Sed-i ' 2s/$/& ' $i '/' 1.test
Done
SH 1.test

The function of script 2 is to convert the sed-i. $i as an input parameter of 3.sh, similarly, the 2s/$/& ' $i '/' 1.test implementation is to output 1,2,3...N to one line. For example, if $num=10, then the contents of 1.test are as follows:

Copy CodeThe code is as follows:
#!/bin/bash
SH 3.sh 1 2 3 4 5 6 7 8 9 10

Finally, let's take a look at script 3.sh

Copy CodeThe code is as follows:
#!/bin/bash
echo 0 > 2.test
num=$#
For ((i=1;i<= $num; i++))
Do
Sed-i ' 1s/$/& ' $i '/' 2.test
Shift 1
Done

The script implements the output input parameter as-is and outputs the input parameter to 2.test. Similarly, the sed-i ' 1s/$/& ' $i '/' 2.test implements an append parameter to one line.

Summarize:

1> sh 1.sh to find out the maximum number of input parameters allowed by the shell script.

2> This value is not specifically calculated due to limited conditions. But we can skip the 1.sh and simply use 2.sh to determine a particular value. For example, SH 2.sh 100000, tested 100,000 input parameters no problem.

3> the highlight of the script is how to append a specific value to the end of the line, mainly through the Sed-i ' 1s/$/& ' $i '/' 2.test, in fact 1s represents the first line. $ represents the end of the line.

4> in vim, 0 jumps to the beginning of the line, and $ can jump to the end of a row.

5> shift left shifts the position of the input parameter. The default is to move 1 bits to the left. As Shift 3 indicates that the original $4 now become $ $, the original $ $ now becomes $ $, and so on, the original $, $, $ discard, $ not move.

6> the script has a hidden danger, that is, the line of the text file has the maximum character limit. However, when testing 100000 as the number of input parameters, there is no problem, stating that a line of text files can accommodate a significant number of characters.

PS: When monitoring the results of 2.test, we can use watch cat 2.test, That is, every two seconds to see the content of 2.test, but the law has a disadvantage, the data is more, can not be displayed in a screen, it will only show the previous fixed data, the new data will not be displayed, but in this case, we are more concerned about whether the data is increased. The following script can be used for this function:

Copy CodeThe code is as follows:
#!/bin/bash
While True
Do
Cat 2.test
Sleep 30
Done

The maximum number of arguments the shell supports

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.