Shell Programming (v)

Source: Internet
Author: User

Learning from the previous articles, we learned the basic syntax of the shell. In the actual operation of Linux, we often see the command will have many parameters, such as: Ls-al, and so on, then how this parameter is handled? Let's take a look at the shell script's handling of user input parameters.

command-line parameter handling gets parameters based on parameter location

The bash shell can get parameters based on the parameter location. Command -line arguments for 1th through 9th are obtained through $9 . The shell name is $ A. If the parameter is more than 9, then it can only be obtained through ${} , for example, to get the 10th parameter, then write as ${10}.

Example one:

#!/bin/"file name:$ $""Base file name: $ (basename $) "  "param1: $" "param2: ${2}"

 

Run the shell above

 A  the

 

The final results are as follows:

File name:./testinput4.sh

Base file name:testinput4.sh

Param1:12

Param2:34

Successfully obtained the file name and command line input parameters (command line arguments are separated by a space, if the argument contains a space, then long must be added quotation marks)

The default is to get the name of the current shell file, but it also contains (./), and if you run with the full path, it will also contain the directory name. Therefore, the above is done by basename command to get the Simple file name $ (basename $).

Imagine, if we write a lot of this parameter of the shell, if it is like the above one to get parameters, it is not to write crazy! Let's take a look at how to solve this situation.

Read all parameter methods one

Since the bash shell can get parameters by location, it means that if we know the total number of parameters, we can get the parameters sequentially by loop. So how do you get the total number of parameters?

The total number of parameters can be obtained through $# in the bash shell.

Example: (Loop get parameters)

#!/bin/bash for ((index=0, index <= $#; index++ ))do    echo ${ ! Index}done

 

For the above example, we get General Staff number by $# . The parameters for each position are then cycled through. Note: According to normal understanding, the above ${!index} should be ${$index}, right? However, because the $ symbol cannot be written in ${}, the bash shell is used in this place! symbol, so the above is written for ${!index}.

Method Two

All parameters can also be obtained through $* and [email protected] in the bash shell. But there is a big difference between the two:

$* will save all arguments provided on the command line as a single word, and the value we get is equivalent to a string whole.

[email protected] treats all arguments provided on the command line as multiple independent words in the same string.

Maybe the text looks less clear, so let's look at the difference between the two by example:

#!/bin/bash#testinput.shvar1=$*var2=[Email Protected]echo"var1: $var 1"Echo"var2: $var 2"countvar1=1countvar2=1 forParaminch "$*" DoEcho"First loop param$countvar1: $param"countvar1=$[$countvar 1 +1]doneecho"countvar1: $countvar 1" forParaminch "[email protected]" DoEcho"second param$countvar2: $param"countvar2=$[$countvar 2 +1]doneecho"countvar2: $countvar 2"

 

Perform the above example:

./testinput.sh 12 34 56 78  

The output of the above example is:

Var1:12 34 56 78

Var2:12 34 56 78

Param1:12 34 56 78

Countvar1:2

Param1:12

Param2:34

param3:56

param4:78

Countvar2:5

As the results above show, the direct output looks the same as the results, but the difference between them can be seen through the for loop. In the previous article we talked about the for loop being split by the value defined by the IFS, so by default, if we do not quote in the For loop above, then dividing it according to the space defined in the IFS will eventually cause the difference to be seen.

Get user Input Single input

Sometimes we interact with the user by getting input from the user during the execution of the shell. This is done through the read command. Let's look at its usage:

Example one:

#!/bin/"Yes or no (y/n)? "  "your choice: $choice"

 

Running the above example, the first output is "Yes or no (y/n)", and then wait for the user input (the-n parameter means no line break, so the bank waits for user input), when the user input, the user input value will be assigned to the choice variable, and then the final output "Your choice: ( what you entered). "

In fact, we can not specify the variable name after read , and if we do not specify it, the read command will put any data it receives into the special environment variable reply. As follows:

Example two:

#!/bin/"Yes or no (y/n)? "  "your choice: $REPLY"

 

The above example is equivalent to example one.

Sometimes, we need the user to enter multiple parameters, of course, the shell is supported to accept more than one parameter input at a time.

Multiple inputs

Example three:

#!/bin/"what' s your name? " " First Lastecho First: $firstecho Last: $last

 

The example above first outputs "What's your name?" and then waits for user input at the bank (where Read-p implements the non-wrapping effect of the above example's Echo-n + Read command), and the input parameters are separated by spaces, The shell assigns the input values to first and last two variables in turn. If you enter too many values, if I enter 3 values, the shell assigns the remaining values to the last variable (that is, the value of the first 232 is assigned to the previous variable).

Think about it, there is a problem, if the user has not entered, how to do? Been waiting?

Timeout settings

We can specify the time-out (in seconds) by READ-T, and the Read command returns a status code other than 0 if the user does not enter it within the specified time.

Example four:

#/bin/bashif5""  name    then"Hello $name"else " "Fi    

 

Running the above example, if it is not entered for more than 5 seconds, then the else inside will be executed.

Summary

This article simply introduces the input parameters of the shell and receives user input. You can extrapolate, combined with the basic knowledge previously learned, can write some small script application.

Shell Programming (v)

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.