Shell Program Design (2)

Source: Internet
Author: User

5 while
Number of cycles
Foo = 1 // initial cyclic values
While ["$ foo"-le 2] // le: <= while (FOO <= 2)
Do
Echo "Are you there"
Foo = $ ($ Foo + 1) // This form is very important to foo ++
Done

6
Until Condition
Do
Statements
Done
The until loop is executed repeatedly until the condition is true, while the while loop is executed repeatedly when the condition is true.

Until who | grep "$1">/dev/null
Do
Sleep 600
Done
Echo-E //
Echo "***** $1 has just logged in ****"
Exit 0
Input:/bash/SH yzj
Output:
-E
* *** Yaozhangjun has just logged in ****

If you comment out>/dev/null, yes
Yzj tty7 2009-10-19 (: 0)
Yzj pts/0 2009-10-19 10:00 (: 0.0)
-E
* *** Yaozhangjun has just logged in ****
The function of the/dev/null recycle bin is displayed.

7 case
Case variable in
Pattern [| pattern]...) statements ;;
...
Esac

In fact, the case in shell is the same as the switch-case in C.
In this example, strings are compared in order, instead of finding the best match.
This causes
Case "$ timeofday" in
*) Echo "sorry, not be recognized ";;
Yes) echo "good morning ";;
No) echo "Good afternoon ";;
Y) echo "good morning ";;
N) echo "Good afternoon ";;
No matter what you enter, the reason why sorry will be executed

Case "$ timeofday" in
Yes | Y | Yes | yes) echo "good morning"; // here | it is not a pipeline but a logic or. Keep the distance.
N * | N *) echo "Good afternoon"; // double quotation marks
*) Echo "sorry ";;

Case "$ timeofday" in
[YY] | [YY] [EE] [ss]) // pay attention to the distance. If it is [YY] [EE] [ss], it will not work. Why not, because I tried syntax error: Word unexpected (expecting ")")
Echo "good morning" // for multiple statements in one case, you only need to add;
Echo "up bright and early this morning ";;

2.6.4 Functions
1. No return value is returned when the function is declared.
2. When using a function, you can directly use the function name or directly add real parameters without brackets.
3. The local variable is declared as a local variable.
4. Use the return command to let the function return a number group. A common way for the function to return a string value is to let the function Save the string in a variable.
5 echo> like touch Fred, Fred creates an empty file.
6 While: implement infinite loop instead of while true
7 break: jump out of the current loop (the loop that executes the break statement)
8. Continue: jump out of this loop (the rest of the current loop is still executed)
9. Command
# Classic_set.sh

Version = classic
PS1 = "classic>"

# Lastest_set.sh

Version = lastest
PS1 = "lastest version>"

Execute Command
$ ../Classic_set.sh
Classic>
Classic>. lastest_set.sh
Lastest version> echo $ version
Lastest
Through this experiment, I think it seems that the command can be executed instead of/bin/sh.
10. In bash, echo-n "hello" is used to delete line breaks.
11 Eval: allows evaluation of parameters
Foo = 10
X = foo
Y = '$ x
Echo $ Foo 10
Echo $ X foo
Echo $ y $ foo
While
Foo = 10
X = foo
Eval y = '$ X has the same functions as y =$ ($ X ).
Echo $ Foo 10
Echo $ X foo
Echo $ y 10
12 exit n
The exit command causes the script program to end with exit code n. 0 indicates success
13 Export
The Export command exports the variable used as its parameter to the sub-shell and stores it in the sub-shellzhong youxiao. export creates its own parameter as an environment variable, which can be seen by other scripts and programs called by the current program.

# Export2
Echo "$ foo"
Echo "$ bar"

# Export1
Foo = "the first meta-syntactic variable"
Export bar = "the second meta-syntactic varibale"
Export2

Run $/bin/sh export2 // The Wishing book uses export1. After the execution, the export2 not found is displayed.
// Empty rows
The second meta-syntactic varibale // indicates that the bar value is imported to exprot2, but Foo does not

E1 E2 E3
|
|
|
E2 E3
The bar value remains unchanged until E3, But it is strange that

E1 E2 E3
|
|
|
E2 E2

When, even if the bar values in E1 and E3 are different, E2 still outputs
Set-A or set-allexport will export all the variables declared after it

14 set: Set the parameter variables for shell. A field in the output result must be used.
Set $ (date)
Echo the month is $2 // equivalent to echo $ (date + % B)
Set is used to extract location parameters.

15 shift: Left all parameter variables to the left, $3-> $2. $4-> $3, and the original value of $1 will be discarded.

While ["$1 "! = ""]; Do
Echo "$1"
Shift
Done
Run:
$/Bin/sh shift1 A B C E
A
B
C
E

16 trap: the action taken after receiving the signal. When the script program is interrupted, the cleaning is completed.
Trap Command Signal
Command: The action to take when the specified quotation mark is received.
Signal: processing signal name
Role of command
-Reset the processing condition of a signal to the default value.
''Ignore a signal

The following example has already scared me.

Trap 'rm-F/tmp/my_tmp_file _ $ 'int

Echo creating file/tmp/my_tmp_file _ $
Date>/tmp/my_tmp_file _ $
Echo "Press interrupt to interrupt ..."
While [-F/tmp/my_tmp_file _ $]; do
ECHO file exists
Sleep 1
Done
Echo the file no longer exists
When:/bin/sh trap1 is executed
1 Creating file/tmp/my_tmp_file _ $ ($ indicates the number of generated files)
2. Write the current time
3 Press interrupt to interrupt...
4 file exists (occurs once every 1 minute)
5 Ctrl + c
6. Execute int and delete/tmp/my_tmp_file _ $.

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.