In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use shift to solve the problem of entering parameters in shell programming. It is very detailed and has a certain reference value. Interested friends must read it!
Shell programming often encounters a situation where the number of parameters is variable. How to deal with this situation? Shift is about to make his debut.
$# is the number of input parameters
$@ is all the current input parameters
$0 is the name of the script
$1 is the first input parameter, and $2 is the second input parameter.
Bash defines $1 to $9, a total of 9 location variables, which determines that we can't unlimited use things like $11 to get more input parameters.
At this point, you need to use shift. Shift will move the input parameter to the right, as shown in the following figure:
Let's take a look at a piece of shell code to deepen our understanding:
The code is as follows:
Root@manu:~/code/shell/temp# cat test.sh
#! / bin/sh
Until [$#-eq 0]
Do
Echo "$@"
Shift
Done
Look at the output:
The code is 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
one
Now, if the number of participants is uncertain, how do we deal with the participants?
The code is as follows:
Until [- z "$1"] # Until all parameters used up
Do
Store_input $1
Shift
Done
The pseudo code snippet above shows how to deal with uncertain input parameters. Through store_input, we save each input parameter, and then we can deal with it.
Finally, an example of dealing with uncertain parameters is given:
The code is as follows:
Manu@manu:~/code/shell/temp$ cat sum.sh
#! / bin/sh
Sum=0
Until [- z $1]
Do
Sum= `echo $sum+$1 | bc`
Shift
Done
Echo $sum
The output is as follows:
The code is as follows:
Manu@manu:~/code/shell/temp$. / sum.sh 1 2 3 4 5 6 7 8 9 10
fifty-five
These are all the contents of this article entitled "how to use shift to solve the problem of entering parameters in shell programming". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.