Five process Control of Linux shell--Select

Source: Internet
Author: User
Tags readable

Process control can be handled differently depending on the situation, and the specified program area can be executed repeatedly, demonstrating the productivity of the program. In the bash shell, Process Control can be divided into two main categories:

"Select" and "cycle";

1. Select: If, case, select

2. Loop: For, while, until, select

Command Select is both a choice and a loop.

The end state of a command

After each command in the shell executes, it returns an end-state value of only two, and if successful, returns 0 and fails back to non-0.

When the command executes, use $? To view the status return value, $?=0 is true when the shell is tested for conditions, and $?= 0 is false.

[[email protected] ~]# Pwd/root[[email protected] ~]# echo $?0[[email protected] ~]# Pwdddd-bash:pwdddd:command not Foun D[[email protected] ~]# echo $?127

When a command executes normally, $?=0. When a command is entered incorrectly, $?= is not 0

For example, yum installs software, copies files, deletes files, determines whether files exist, and so on. If executed correctly, then $?=0, if there is an error in the middle, $?= not 0


Second, if condition judgment

if condition test; then

Command Area

Fi

If the condition test is true, execute the Command Area directive, or continue with the next line of the FI.

[email protected] ~]# cat test.sh #!/bin/bash#if grep-q ^root/etc/passwd;thenecho "The user root is exist" Fi[[email Pro Tected] ~]# bash test.sh The user root is exist

Use grep to search for keywords that start with root in/etc/passwd, and if true, continue with the command below (GREP-Q indicates only true and false, not output).

The simplest if syntax: If-then-else

if condition test; then

Command Area 1

Else

Command Area 2

Fi

If the condition test is true, execute the Command Area 1 directive, or execute command area 2.

[email protected] ~]# cat test.sh #!/bin/bash#if grep-q ^roothi/etc/passwd;thenecho "The user Roothi is exist" Elseecho "The user Roothi not exist" Fi[[email protected] ~]# bash test.sh the user Roothi not exist

Determine if the user Roothi exists.

Full syntax for IF

If condition test 1;then

Command Area 1

elif condition Test 2;then

Command Area 2

Else

Command Area 3

Fi

If the condition Test 1 is true, the command area 1 is executed, if false, the condition test 2 is executed, if the condition test 2 is true, the command area 2 is executed, and if the condition test 2 is false, then the command area 3 (elif condition can have more than one) is executed.

[Email protected] ~]#/test.sh 5 is the bigger! [Email protected] ~]#/test.sh 5 1010 is the bigger! [Email protected] ~]#/test.sh 5 = 5 [[email protected] ~]# cat test.sh #!/bin/bash#declare-i a b #声明a b all in positive integer a=$1b =$2if (($a > $b)) Thenecho $a is the Bigger!elif (($a < $b)), Thenecho $b is the Bigger!elseecho "$a = $b" fi


Third, the wording of the condition test

There are 10 types of conditional tests. (Returns the result 0 is true, not 0 is a false value)

1. The result of executing a command.

If grep-q "root"/etc/passwd;then (-Q silence, only to judge $?)

echo "Root is Exist"

The end state is the result of the last command execution

2. Returns the opposite value of the result of a command.

! Command (space separated by spaces)

if! Grep-q "Root"/etc/passwd;then (-Q silence, only to judge $?)

echo "Root is not exist"

If the command returns a result of 0, then add! Returns a value of 1, or vice versa if the command returns a value other than 0, plus! Returns a value of 0 after the return.

3. Use compound command (calculation)

The result of the operation is not 0 true and 0 is false

((8*3)) 24 true

((5-5)) 0 fake

((20<30)) really

((20>30)) false

((20&&30)) really

((20&&0)) false

4. Use the BASH keyword ' [[]] ' to make a sentence: [[judging]]

there is at least one space before [[Post and]]

if [[str > XYZ]];then

echo "string str relatively large"

5. Using built-in commands: test judgment

if test "str" \> "XYZ"; then (> for special characters, with \ Escape)

echo "string str relatively large"

6. Use the built-in command: [] Judgment

[] Same as test usage

if ["str" \> "xyz"];then (> for special characters, with \ Escape)

echo "string str relatively large"

7. Logic combination using-A,-O

[-R filename-a-x filename] true if filename is readable and executable. -A, and.

[-R Filename-o-x filename] true if filename is readable or executable. -O, or.

8. Command 1&& Command 2

Command 1 really executes command 2, all true line back to value 0, otherwise 1

If grep-q "root"/etc/passwd && ((8<10)); then

9. Command 1| | Command 2

Command 1 False to execute command 2 if a command or both is true line back to value 0, otherwise 1

((2<3)) | | ((4<5)); then

10, $$ and | | Share

if it is "[judging] && directive 1| | Directive 2 "form, as If-then-else

["2" \> "1"]&& echo "right" | | echo "Error" is equivalent to the following script:

If ["2" \> "1"];then

echo "Right"

Else

echo "Error"

Fi

Summarize:

[[]], test, [] usage is similar, but [[]] more free (do not worry about special symbols, do not have to escape), (()) is not affected by the special symbol.

in [[judging]], if you use = = or! = and the right string does not use single quotes, double quotation marks, it is considered to compare the string formed by the "style", if it matches, return 0, otherwise 1.

!/bin/bash

A= "STR"

if [[$a = =???]]; Then it means: with a value of $ A, the contrast style??? (3 strings)

echo "Hello"

Fi

If you change the IF line to

if [[$a = = "???"]]; Then it means: use $ A and string??? is equal


Iv. the true and false value of conditional judgment

Judging type: The formula that affects the procedure flow, the judgment type divides into "the unit" and "two Yuan"

such as-f filename, tests whether the file exists. The file of-F followed by an operand is called a "unit".

If the parameter 1-gt parameter 2, the test parameter 1 value is greater than the parameter 2,-gt the parameters to be compared, this judgment is called "two Yuan".

-------------------- Judgments about documents --------------------

Judging what the case is true

-a/e file exists

-B file exists, and the file is a zone device file

-c file exists, and the file is a character device file

-D file exists and the file is a directory

-p file exists, and the file is a pipe file (FIFO)

-f file exists, and the file is a generic file

-G file exists, and the file sets the Set group ID Property-U UID

-h/l file exists, and the file is a symbolic link file

-R file exists and the file has a readable property-W writable-X Executable

-S file exists and the file size is greater than 0 (the file exists and the file has data)

-T file descriptor if the file descriptor is open and links to a terminal

-s file exists, and the file is a socket file

The-n file exists, and the file has been modified since it was last read.

Files 1-nt File 2 If file 1 is newer than file 2, file 1 exists, but file 2 does not exist.

File 1-ot File 2 If file 1 is older than file 2, file 1 does not exist, but file 2 exists.

File 1-ef File 2 if file 1 and file 2 refer to the same device and inode number.

-------------------- conditional judgment on strings --------------

Judging what the case is true

-Z length is 0 (i.e. empty string)

-N length is not 0 (that is, non-empty string)

String string length is not 0

= = and = are the same (the left and right sides have a string respectively)

! = Unequal < Less than > is greater than (if <> appears in [] or test, to be escaped with \)

-------------------- The conditional judgment on the calculation ----------------

-eq equal

-ne Not Equal

-lt less than

-le less than or equal to

-GT Greater than

-ge greater than or equal to

------------------- Conditional-judgment on bash options -------------------

The name of the-o set option is true if the option is enabled.

!/bin/bash

Set-o

If [-O history];then

echo ' bash option ' open '

Else

echo ' bash option history closed '

Fi


V. Case CONDITION judgment

If condition judgment refers to the if condition to judge, once to carry out the condition test situation more, if and elif syntax will become very lengthy, case conditional judgment grammar can complement its insufficiency.

The syntactic structure of the------------------case ------------------

Case Test Item in

Style serial 1) command area 1;;

Style serial 2) command area 2;;

Style serial 3) command area 3;;

.....

*) command area;;

Esca

Style serial can be a string: such as a, B, Mary

Wildcard characters: * Any length (including null characters)? One character

CharSet: [A-d]mm, representing AMM, BMM, CMM, DMM

Delimiter: |

If the item to be tested is in line with the style, the case executes the command area after the style, and the command area can be a single instruction or multiline instruction, and finally; End.

Style serial can be made up of strings and wildcards, which are case-insensitive by default.

If the serial has several characters to compare, separated by |, | has or means (as long as one of them succeeds).

Style serial can be written as ' (style serial) ' or ' style serial '

*) is usually placed in the last area of the case to capture other situations that do not conform to the specified style.

Example:

!/bin/bash

Name=$1 the first input parameter is appended to the name

Case $name in

A|B|C) matching option 1, when the variable is a or B or C

echo "HELLO,ABC Group" output result 1

;; ;; Can be placed in the output 1 or another line

Huan) match option 2, when the variable is Huan

echo "I love You" output results 2

;;

s*) match option 3, the variable starts with s string

echo "You are first S" Output result 3

;;

*) match Option 4 String that does not meet any of the above criteria

echo "What's This?"              ";; Output 4 "What's This?" ".......

Esca End

-----------------Advanced Style--------------------

Style conforms to the situation

? (style serial) conforms to 0 or 1

* (style serial) conforms to more than 0

+ (style serial) conforms to more than 1

@ (style serial) conforms to 1 of these

! (style serial) as long as the style is not in parentheses, even if it fits

[Email protected] (AC|XY|PQ) k|join|joe echo ' Hello ';;

As long as the variables match jack, Jxyk, Jpqk, join, Joe One of them even matches

if [[$cmd = = @ (a|b)]];then

As long as a or B is successful in comparison.

This article is from the "Welcome to Linux World" blog, so be sure to keep this source http://linuxnote.blog.51cto.com/9876511/1641204

Five process Control of Linux shell--Select

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.