How many arguments does the shell support at most? _linux Shell

Source: Internet
Author: User

In the scripting process, the input of parameters is usually involved. For example, SH 1.sh 10 20, in the execution 1.sh this script, 10 is the first parameter, 20 is the second parameter. Sometimes, there is this confusion, that is, how many variables can a shell script support? Puzzled, the following script was 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--Here for convenience, the name of the foot is extremely simple. The general idea is to give a specific value of n, as the maximum number of input parameters, and then 1,2,3...N as the input parameters of the script, which are 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. 1.sh realizes the function is through a dead loop, Increment generation N, by referring to 2.sh to determine whether N is reasonable. The following script is specifically visible.

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

Copy Code code as follows:

#!/bin/bash
I=0
echo 0 > Currnumber
While True
Todo
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 is mainly to provide a dead loop, $i refers to the number of input parameters, 2.sh to determine whether the number of given parameters is reasonable, if reasonable, then append the value to currnumber this file, if not reasonable, then represent $i-1 is the shell can accept the maximum number of parameters. The value is exported 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 number output a row, considering the maximum number of rows in the file limit, where the value is output to a row. The Sed-i ' 1s/$/& ' $i '/' Currnumber is implemented to add the $i value to the end of the line.

And take a look at the script 2.sh.

Copy Code code 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++))
Todo
Sed-i ' 2s/$/& ' $i '/' 1.test
Done
SH 1.test

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

Copy Code code as follows:

#!/bin/bash
SH 3.sh 1 2 3 4 5 6 7 8 9 10

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

Copy Code code as follows:

#!/bin/bash
echo 0 > 2.test
num=$#
For ((i=1;i<= $num; i++))
Todo
Sed-i ' 1s/$/& ' $i '/' 2.test
Shift 1
Done

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

Summarize:

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

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

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

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

5> shift shifts the position of the input parameter to the left. The default is to move 1 bits to the left. As shift 3 means that the original $ $ is now $, the original $ is now $ $ and so on, the original $ $, $ $, $ $ no move.

6> the script has a hidden danger that the lines of the text file have the maximum character limit. But when you test 100000 as the number of input parameters, there is no question that the text file line can hold 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 view the content of 2.test, but the law has a disadvantage, more data, can not be displayed in a screen, it will only show the previous fixed data, the new data will not show, but in this case, we are more concerned about whether the data increase. This function can be achieved by the following script:

Copy Code code as follows:

#!/bin/bash
While True
Todo
Cat 2.test
Sleep 30
Done

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.