Shell condition test, operator, selection structure, for loop structure

Source: Internet
Author: User
Tags arithmetic

One, shell condition Test man bash//view Help

Commands for conditional tests

Test option file name or directory name

Or

[option file name or directory name]


Cases:

[-E/ETC/PASSD]


File status

-F//judgment is not a file, is the file is True

- D//Determine if the directory is true

-E//judgment is not present, existence is true

-R//Judging if it can be read and read as True

-w//Determine if writable, can be written as true

-x//To determine if executable, execute as True

-L//Determine if the link is true



Numeric comparison (integer comparison)

[value 1 option value 2]

Or

Test value 1 Option value 2


equals-eq

Not equal to -ne

Less than-LT

Less than or equal to-le

Greater than-GT

Greater than or equal to-ge


H

[Comparison of character options]


equals = =

Not equal to! =

-Z string is empty for true

! -Z String not empty for true


Cases:

[$USER ==root] && echo "admin"

[-Z $w]



Logical comparison (when judging, is 2 or more than 2 conditions when using logical judgment)

Logic and && or use-a multiple conditions to be set up at the same time

Logical OR | | Or use the-O or with multiple conditions as long as one condition is established can

Logical Non! Take the number-Z to empty! -Z plus an exclamation mark is not null


[5-GT 6] | | [7 lt 8] | | [0-lt 1]

Or

[5-gt 6-o 7-lt 8-o 0-lt 1]



Whether the process control is performed depends on whether the judgment result of the condition is true. Condition to judge the result to be true,

Process Control will not be executed, otherwise it will not execute. Make judgments about different objects and use different ways of judging and options.



Second, shell operators and Operation commands

+ - * / %

++ --

+= -= *= /= %=


Arithmetic commands

Expr value 1 operator value 2


Example 1:

#!/bin/bash

#计算用户从终端输入的任意2个数的和

Read-p "Please enter the first number" NUM1

Read-p "Please enter a second number" num2

sum= ' expr $num 1 + $num 2 '

echo "$num 1 + $num 2 = $sum"


Example 2

Expr can only do integer operations to return the calculation result by default

#expr num1 + num2-num3 \* num4//multiplication sign to do escape

#expr 10 of 8

#a =10

#b =20

#sum = ' expr $a + $b '


+ + auto-add arithmetic each time the number automatically plus 1

--automatically minus 1 per digit of the self-subtraction operation

Let only do operations and do not return calculation results

Let variable operator


I=1

Let i++//i=i+1

Echo $i


j=10

Let j--//j=j-1

Echo $j


+= -= *- /= %=

i=10

Let i+=2//i=i+2

Echo $i


Use of the second operation of the addition calculation $ (()) $[]

#echo $ ((10*30))

#x =10

#b =20

#echo $ ((x*y))

#echo $[10*30]


i=10

echo $ ((++i))//Perform the operation first, then participate in the program

i=10

echo $ ((i++))//participate in the program first, then perform the operation



Process Control in the shell

Select Structure if (only if the condition determines that the result is true, do the corresponding action)


Single Branch

If condition

The action to be performed when the condition is determined

.......

Fi


Cases:

Read-p "Please input IP address" addr

If [-Z $addr];then

echo "You did not enter the IP address to ping"

Exit

Fi

Ping-c 3 $addr &>/dev/null

If [$?-eq 0];then

echo "Host Online"

Fi

echo "Script over"



Dual Branch

If condition test command; then

The action to be performed when the condition is determined

.........

Else

The action to perform when the condition is not determined

.........

Fi


#!bin/bash

Service sshd Status &>/dev/null

If [$? -eq 0];then

echo "sshd in Running"

Else

Service sshd Start

Fi


Multi-Branch

If condition test command 1;then

Sequence of commands executed when a condition test is established

......

......

elif Conditional Test Command 2;then

Sequence of commands executed when a condition test is established

......

......


elif Conditional Test Command 3;then

Conditional test 30% sequence of commands executed immediately

......

......

elif Conditional Test Command N;then

Conditional test n A sequence of commands executed at the time of establishment

......

......

Else

All of the above conditions are determined when the command is executed

......

......

Fi



Loop structure (use loop structure when performing the same operation repeatedly)

For syntax format


For variable name in variable Value list

Do

When a variable value list has a value assigned to a variable name, the statement block that is executed

......

.....

doneasswd


#!/bin/bash

#for I in 1 2 3 4 5

#for i in ' SEQ 30 45 '

#for i in ' seq 254 '

#for i in ' seq 10 2 30 '

#for i in {1..30}

#for i in {A.. Z

For i in ' head/etc/'

For i in ' ls/shell/*.sh '

Do

Echo $i

Done



While

C-for

Until


Branch structure case (performs the corresponding action based on the matching result of the value of the variable)


This article is from the "Wsyht blog" blog, make sure to keep this source http://wsyht2015.blog.51cto.com/9014030/1786325

Shell condition test, operator, selection structure, for loop structure

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.