Shell Base 05 handles user input

Source: Internet
Author: User

1. Command-line parameters------similar to javac parameter 1 parameter 2

Java-like compiled Javac parm1 ..... In the shell, the arguments are separated from the arguments by a space. Use positional parameters to identify corresponding parameter values: $ $is the program name, $ is the first parameter , and so on, you know the 9th parameter $9. For more than 9 parameters, add curly braces around the variable numbers, such as ${10}.

Note: Not only can you handle numeric values on the command line, but you can also handle strings.

1[Email protected] documents]$CatTest2.SH2#!/bin/Bash3Total=$[$1*$2 ]4 EchoThe first parm is $15 EchoThe second parm is $26a=$37 EchoThe third parm is $38b=$49 EchoThe Forth Parm is $4Ten[Email protected] documents]$ bash test2.SH 2 3 "Hello World"min OneThe first parm is2 AThe second parm is3 - The third parm is Hello world -The Forth Parm is min
View Code
1[Email protected] documents]$Cattest32#!/bin/Bash3 EchoThe tenth Parm is ${Ten}4 EchoThe eleventh Parm is ${ One}5[Email protected] documents]$ bash TEST31 2 3 4 5 6 7 8 9 Ten  One6The tenth Parm isTen7The eleventh Parm is One8[Email protected] documents]$
View Code

Note: $ $ Returns the script name and, if bash, only returns the script name; if the./script runs to return the current pathname, you can also try the basename command to return a script name that does not contain a path.

2. $#,$*,[email protected],${!#}

s# the number of arguments used to count the command line

S* is used to access all command-line arguments and to form a whole string output

[email protected] with s*, only the result is scattered into a string array, each element in the array is a parameter

${!#} represents the last parameter, because the curly braces are not available in $, so use! To replace it.

1[Email protected] documents]$Cattestfile2#!/bin/Bash3 EchoThe"\$*"Is $*4 EchoThe"\[email protected]"is [email protected]5 EchoThe"\$#"is $#6 EchoThe"\${!#}"Is ${!#}7 8 [[email protected] documents]$ bash testfile a b c D9The $*Is a b c DTen The [email protected] is a B c D OneThe $# is4 AThe ${!#} is D
View Code
1[Email protected] documents]$Cattestfile2#!/bin/Bash3 Echo4Count=15  forParaminch "$*"6  Do 7    Echo "\$* Parameter # $count = $param"8count=$[$count +1 ]9  DoneTen  One Echo ACount=1 -  forParaminch "[email protected]" -  Do the    Echo "\[email protected] Parameter # $count = $param" -count=$[$count +1 ] -  Done -  + [[email protected] documents]$ bash testfile a b c D -  +$* Parameter #1=a b c D A  at[Email protected] Parameter #1=a -[Email protected] Parameter #2=b -[Email protected] Parameter #3=C -[Email protected] Parameter #4=D -[Email protected] documents]$
View Code

3. Shift Move Variable

Shift can be used to traverse without knowing how many parameters, and the value of each parameter, because it can always print only the first value. By default, it moves each parameter variable one position to the left. So the value of the variable $ is moved to $, and the value of $ is moved to $ $, and the value of the variable is deleted (Note:$0 represents the program, does not change)

You can also shift n to specify how many left to move, Eg:shift 2, then $ $ will be moved to $ $, so you can skip some values do not traverse.

1[Email protected] documents]$CatTest3.SH2#!/bin/Bash3 Echo "The original parameters is $*"4 Shift 25 Echo "The new first parameter is $"6[Email protected] documents]$ bash test3.SH 1 2 3 4 57The original parameters is1 2 3 4 58The new first parameter is39[Email protected] documents]$
View Code

Note: With the use of shift, the logic in the shell script can also be used to determine whether it is an option or a parameter, so that the parameters get the expected output. Getopt and Getopts are also available in the Bash shell to determine whether the options or parameters-------use when referencing them.

4. Get user input-------more interactivity, similar to scanner+system.in usage in Java

Use the read command . Read followed by the variable name, you can save the input value into the variable, if you do not enter the variable name, it is automatically saved in the special environment variable reply.

1[Email protected] documents]$Cattest12#!bin/Bash3 Echo-N"Enter your name:"4 Read name5 Echo "Hello $name"6 [email protected] documents]$ bash test17 Enter your Name:tom8 Hello, Tom .9[Email protected] documents]$
View Code

Note1: If the user has not entered, read will wait, so you can set the timer with the-t option. Time has gone by, and it will not wait.

EG:READ-T 5 Name

Note2: Like password input, hidden way read, only need to add-s can

Note3: can also be read from a file, one line of read

1[Email protected] documents]$Cattest12#!bin/Bash3 a4 b5 C6[Email protected] documents]$Cattest27#!/bin/Bash8 CatTest1 | whileRead Line #采用了管道9  DoTen     Echo "The line is $line" One  Done A Echo "Read is done" - [email protected] documents]$ bash test2 -The line is #!bin/Bash the The line is a - The line is b - The line is C -Read is Done +[Email protected] documents]$
View Code

Supplemental Piping:

Command1 | Command2 is redirecting the output of command 1 to Command2. Can be multi-level redirection, add more | just fine.

Reference Documents :

Linux command line and Shell Scripting Encyclopedia (3rd edition) [Mei] Bloom (Richard Blum), Bresnahan (Christine Bresnahan), Jia, Wuhai

Shell Base 05 handles user input

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.