Shell knowledge point supplement (5)-case. In. esac/function/while done/until done

Source: Internet
Author: User

1. case... in... esac

The syntax of case... in... esac is as follows:
Case $ variable name in
"First variable content ")
Program Section
;;
"Second variable content ")
Program Section
;;
*)
Other Program Execution segments that do not contain the first variable content or the second variable content
Exit 1
;;
Esac
Note that this syntax starts with case and ends with esac,

Write a program: allows the user to enter one, two, three, and display the user's variables on the screen. If it is not one, two, three, only these three options are available.
[Root @ Linux Scripts] # vi sh11.sh
#! /Bin/bash
# Program:
# Let user input one, two, three and show in screen.
# History:
#2005/08/29 vbird first release
Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export path
Echo "this program will print your selection! "
# Read-P "input your choice:" Choice
# Case $ choice in
Case $1 in
"One ")
Echo "your choice is one"
;;
"Two ")
Echo "your choice is two"
;;
"Three ")
Echo "your choice is three"
;;
*)
Echo "usage {one | two | three }"
;;
Esac
In this case, you can run the "Sh sh11.sh two" command to receive the corresponding response. The above method is directly issued. If interactive mode is used, remove the "#" of the 10th and 11 rows above and add the 12 rows with comments (#)

Practice:

1 #! /Bin/bash
2
3
4 echo "practice to use" one "" two "" three ""
5 read-P "input your choise (from the up suggest):" answer
6
7 case "$ answer" in
8 "one ")
9 echo "your input is 'one', that is $ answer"
10 ;;
11 "two ")
12 echo "your input is 'two', that is $ answer"
13 ;;
14 "three ")
15 echo "your input is 'three ', that is $ answer"
16 ;;
17 *)
18 echo "$ answer is wrong, {you only have the hinted three choices }"
19 ;;
20 esac

LOONG:/home/Yee/shell # sh case_practise.sh
Practice to use one two three
Input your choise (from the up suggest): One
Your input is 'one', that is one
LOONG:/home/Yee/shell # sh case_practise.sh
Practice to use one two three
Input your choise (from the up suggest): Two
Your input is 'two', that is two
LOONG:/home/Yee/shell # sh case_practise.sh
Practice to use one two three
Input your choise (from the up suggest): Three
Your input is 'three ', that is three
LOONG:/home/Yee/shell # sh case_practise.sh
Practice to use one two three
Input your choise (from the up suggest): xxxx
XXXX is wrong, {you only have the hinted three choices}
LOONG:/home/Yee/shell #

2. Function

Function also has built-in variables ~ His built-in variables are similar to shell scripts. The function name represents $0, and the subsequent variables are replaced by $1, $2 ~ It's easy to make a mistake here ~ Because $0, $1... in "function fname () {program segment}" is different from $0 in shell script. For the sh11-2.sh above, if I say "Sh sh11-2.sh one", this indicates that $1 in shell script is the string "one. But in printit ()
The $1 in is irrelevant to this one. Let's rewrite the above example to make it clearer!
# Vi sh11-3.sh [root @ Linux Scripts] #
#! /Bin/bash
# Program:
# Let user input one, two, three and show in screen.
# History:
#2005/08/29 vbird first release
Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export path
Function printit (){
Echo "your choice is $1"
}
Echo "this program will print your selection! "
Case $1 in
"One ")
Printit 1
;;
"Two ")
Printit 2
;;
"Three ")
Printit 3
;;
*)
Echo "usage {one | two | three }"
;;
Esac
In the above example, if you enter "Sh sh11-3.sh one", the word "your choice is 1" will appear ~ Why is it 1? In the program section, if we write "printit 1", that 1 will become $1 in the function.

3. While done/until done

While do done, until do done
In general, the most common loop is the following two states:
While [condition]
Do
Procedure Section
Done
In this method, while is "when .... therefore, this method refers to the meaning of "when the condition is set, a loop is executed until the condition of the condition is not set.
Until [condition]
Do
Procedure Section
Done
This method is the opposite of while. It says, "when the condition is set, the loop is terminated; otherwise, the loop continues .』

What if I want to calculate the data of 1 + 2 + 3 +... + 100? Use Loops ~ He is like this:
[Root @ Linux Scripts] # vi sh13.sh
#! /Bin/bash
# Program:
# Try to use loop to calculate the result "1 + 2 + 3... + 100"
# History:
#2005/08/29 vbird first release
Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export path
S = 0
I = 0
While ["$ I "! = "100"]
Do
I = $ ($ I + 1 ))
S = $ ($ S + $ I ))
Done
Echo "the result of '1 + 2 + 3 +... + 100 'is => $ S"
Hey! After you run "Sh sh13.sh", you can get 5050 of the data! So you can call ~ So let's do it by yourself. If you want the user to enter a number and set the program to 1 + 2 +... until the number you entered, how can you write it?

Practice:

Version 1: While done

1 #! /Bin/bash
2
3 read-P "Please input the stop number:" Input
4 S = 0
5 I = 0
6 While ["$ I "! = "$ Input"]
7 do
8 I =$ ($ I + 1); # echo "I = $ I"
9 S = $ ($ S + $ I); # echo "s = $ S"
10 done
11 echo "the result is: $ S"

LOONG:/home/Yee/shell # sh self_add.sh
Please input the stop number: 100
The result is: 5050
LOONG:/home/Yee/shell #

Version 2: until done

1 #! /Bin/bash
2
3 read-P "Please input the stop number:" Input
4 S = 0
5 I = 0
6 Until ["$ I" =
"$ Input"] # You only need to modify it in two places.
7 do
8 I =$ ($ I + 1); # echo "I = $ I"
9 S = $ ($ S + $ I); # echo "s = $ S"
10 done
11 echo "the result is: $ S"
~
Please input the stop number: 50
The result is: 1275
LOONG:/home/Yee/shell # sh self_add2.sh
Please input the stop number: 100
The result is: 5050
LOONG:/home/Yee/shell # sh self_add2.sh
Please input the stop number: 4
The result is: 10
LOONG:/home/Yee/shell #

4. For... do... Done

The for syntax is in the state of "know how many cycles are required! His syntax is:
For (initial value; Restriction value; execution step ))
Do
Program Section
Done
This syntax is suitable for numeric calculation. The content of the three strings following the for clause indicates:
• Initial Value: the initial value of a variable in the loop, which is directly set with a variable similar to I = 1;
• Limit Value: when the value of a variable is within the limit value range, the loop continues. For example, I <= 100;
• Execution step: the amount of variable changes during each cycle. For example, I = I + 1.
It is worth noting that, in the "execution step" setting, if you add 1 at a time, you can use a method similar to "I ++, that is to say, I adds one to each loop. Okay. Let's use this method to accumulate 1 to 100!
[Root @ Linux Scripts] # vi sh14.sh
#! /Bin/bash
# Program:
# Try do calculate 1 + 2 +... + 100
# History:
#2005/08/29 vbird first release
Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export path
S = 0
For (I = 1; I <= 100; I = I + 1 ))
Do
S = $ ($ S + $ I ))
Done
Echo "the result of '1 + 2 + 3 +... + 100 'is => $ S"

You can also use the following method to perform non-digital operations!
For VaR in con1 con2 con3...
Do
Program Section
Done
In the preceding example, when the $ var variable content cyclically works:
1. During the first loop, the content of $ VaR is con1;
2. For the second loop, the content of $ VaR is con2;
3. For the third loop, the content of $ VaR is con3;
4 .....
We can make a simple exercise. Suppose I have three animals: dog, cat, and elephant. If I want to output the following words in each row: "There are dogs...", you can:
[Root @ Linux Scripts] # vi sh15.sh
#! /Bin/bash
# Program:
# Using for... loop to print 3 animal
# History:
#2005/08/29 vbird first release
Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export path
For animal in Dog Cat elephant
Do
Echo "there are" "$ animal" "s ...."
Done

In addition, you can test the directory in sequence as follows:

Filelist = 'ls $ dir'
For filename in $ filelist
Do
Perm = ""
Test-R "$ DIR/$ FILENAME" & perm = "$ perm readable"
Test-W "$ DIR/$ FILENAME" & perm = "$ perm writable"
Test-X "$ DIR/$ FILENAME" & perm = "$ perm executable"
Echo "the file $ DIR/$ filename's permission is $ perm"
Done

5. shell script tracing and debugging

[Root @ Linux ~] # Sh [-nvx] scripts. Sh
Parameters:
-N: do not execute the script. Only query the syntax;
-V: Before executing scloud, output the content of scripts to the screen;
-X: displays the used script content on the screen. This is a useful parameter!
Example:
Example 1: Is there a syntax problem in the test sh16.sh?
[Root @ Linux ~] # Sh-N sh16.sh
# If the syntax is correct, no information is displayed!
Example 2: list all the sh15.sh execution processes ~
[Root @ Linux ~] # Sh-x sh15.sh
+ Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/home/vbird/bin
+ Export path
+ For animal in Dog Cat elephant
+ Echo 'there are dogs ....'
There are dogs ....
+ For animal in Dog Cat elephant
+ Echo 'there are cats ....'
There are cats ....
+ For animal in Dog Cat elephant
+ Echo 'there are elephants ....'
There are elephants ....
# Using-X is really a good method for tracking scripts. It can lead all the program segments with execution,
# If it is a program section, + font size will be added at the beginning of the output, indicating that it is the program code,
# The actual output is related to standard output ~ As shown above.
In Example 2 above, we can use this simple parameter-X to achieve the debug goal. This is a rare parameter. Generally, if a problem occurs when you execute a script, with this-x parameter, you can know the row where the problem occurs!

From laruence's private dish

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.