Shift is an ingenious solution to the problem of incoming parameter in shell programming _linux shell

Source: Internet
Author: User
I said, the shell is my conventional weapons, although not yet proficient, but I love the shell of the language, under Linux mixed, always write a script. Programmers have genes, there is a preference for programming languages, you let me write C code, I will feel very cool, there will be difficulties, there will be painful groping and learning, but I am willing to learn shell/python, I would like to, even lisp this unpopular language I am also full of curiosity, I don't know anything about go and Erlang now, but I'm interested in both languages, and I'll make love to them if I take out my hand. There is love will have hate, I painfully realize that I am a very emotional programmer, even if I scold myself over and over again bitch is bitchy, over and over to tell themselves to correct attitude, learning css/html/still make me miserable, can be expected, Let me learn Java I will also be very painful (I also clearly understand that the fierce pain must not learn css/html pain). I know I digress, I am a cheap, but I repressed too hard, do not find a place to vent, I will be able to suppress the bad.

How do you handle shell programming, which often encounters a situation where the number of parameters is variable? Shift is going to be a shining debut.

$ #是入参的个数,
$@ is all of the current incoming parameters
$ is the name of the script
$ is the first entry, and $ is the second argument ....

Bash defines $9 in total, which determines that we can't get more incoming parameters with $11 and the like.
Now you need to use SHIFT, and shift will move the parameters right, as shown in the following figure:




Let's look at a piece of shell code to deepen our understanding:

Copy Code code as follows:

root@manu:~/code/shell/temp# Cat test.sh
#!/bin/sh
Until [$#-eq 0]
Todo
echo "$@"
Shift
Done


Look at the output:

Copy Code code as follows:

root@manu:~/code/shell/temp#./test.sh 10 9 8 7 6 5 4 3 2 1
10 9 8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
7 6 5 4 3 2 1
6 5 4 3 2 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1


Now, if the number of participants is uncertain, how do we handle the incoming parameters?
Copy Code code as follows:

Until [-Z ' $] # until all parameters used up
Todo
Store_input $
Shift
Done

The above pseudo code fragment shows how to handle indefinite incoming parameters, and by Store_input, we save each entry, and then we can handle it.
Finally, an example of dealing with indefinite parameters is given:
Copy Code code as follows:

manu@manu:~/code/shell/temp$ Cat sum.sh
#!/bin/sh
Sum=0
Until [Z $]
Todo
Sum= ' echo $sum +$1 |BC '
Shift
Done
Echo $sum

The output is as follows:
Copy Code code as follows:

manu@manu:~/code/shell/temp$./sum.sh 1 2 3 4 5 6 7 8 9 10
55


Reference articles:
Http://www.jb51.net/article/40259.htm
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.