Shell introduction-bash programming-process control (1)
Source: Internet
Author: User
Article title: shell introduction-bash programming-process control (1 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Before introducing process control, let's take a look at the twst command. The parameter of the test command is a criterion. if it is true, a non-zero value is returned.
When the condition is false, zero is returned. The test command must be used to determine whether the process is true or false. Another method
Brackets [] are used, and most of them are used. Test types include:
A: string test
String1 = string 2: whether the two strings are equal
String1! = String2: whether the two strings are not equal
Whether the string is null
-Z string length is 0
-N string length is not 0
B: integer test
-Eq equals
-Ne
-Lt is less
-Gt greater
-Le is less than or equal
-Ge is greater than or equal
C: File test
-Block B files
-C Character File
-D Directory
-F: General files
-R readable
-W writable
-X executable
-K sets the limit bit.
-G sets the group bit.
-U sets the use id.
-P pipeline
-S file size is not 0
The following describes various process control
A ::
If then
Syntax:
If (condition)
Then
Then-commands
Fi
Condition is a test command. The conditon commands in the various procedures described in the previous section are test commands.
For example:
Test4.sh
--------------------------------------------------
#! /Bin/bash
If (test $ #! = 0)
Then
Echo Arg1: $1
Fi
--------------------------------------------------
$/Test4.sh hello
Arg1: hello
$./Test4.sh
$
B ::
If then else
Syntax:
If (confition)
Then
Then-commands
Else
Else-commands
Fi
C ::
If then elif
Syntax:
If (conditon1)
Then
Commands1
Elif (condition2)
Then
Commands2
Else
Commands3
Fi
For example:
Test5.sh
-------------------------------------------------------------
#! /Bin/bash
Echo 'word 1 :'
Read word1
Echo 'word 2 :'
Read word2
Echo 'word 3 :'
Read word3
If (test "$ word1" = "$ word2"-a "$ word2" = "$ word3 ")
Then
Echo 'match: words 1, 2 & 3'
Elif (test "$ word1" = "$ word2 ")
Then
Echo 'match: word 1 & 2'
Elif (test "$ word1" = "$ word3 ")
Then
Echo 'match: words 1 & 3'
Elif (test "$ word2" = "$ word3 ")
Then
Echo 'match: words 2 & 3'
Else
Echo 'no match'
------------------------------------------------------
$./Test5.sh
Word 1:
Do
Word 2:
Do
Word 3:
Do
Match: words 1, 2 & 3
D ::
For in
Syntax:
For var in arg-list
Do
Commands
Done
For example:
Test6.sh
--------------------------------------------------
#! /Bin/bash
For a in xx yy zz
Do
Echo $
Done
---------------------------------------------------
The result is as follows:
Xx
Yy
Zz
E ::
Syntax:
For var
Do
Commands
Done
For example
Test7.sh
-------------------------------
#! /Bin/bash
For
Do
Echo $
Done
-----------------------
$./Test7.sh xx yy zz
Xx
Yy
Zz
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.