Conditional judgment in shell programming (Shell 05)

Source: Internet
Author: User
Tags date1

Conditional judgment in shell programming
Conditions
If-then
Case

If-then
Single condition
if command
Then
Commands
Fi
When the command return code is 0 o'clock, the condition is set

if.sh

#! /bin/Bashif Date Then        Echo "command EXEC"fiifdate123 Then        Echo "Command EXEC1"fiEcho "Out if"[[Email protected]SH]# ./if.SHSaturday, October 29, 2016Ten: the: -Edtcommand exec./if.SH: Line8: Date123:command not foundoutif

Full Coverage

if command
Then
Commands
Else
Commands
Fi

if.sh

#! /bin/BashifDate1 Then        Echo "Command"        Echo "OK"Else        Echo "Commond1"        Echo "fail"fi[[Email protected]SH]# ./if.SH./if.SH: Line2: Date1:command not Foundcommond1fail

Conditional nesting

If Command1
Then
Commands
If Command2
Then
Commands
Fi
Commands
Fi

Multi-criteria
If Command1
Then
Commands
Elif Command2
Then
Commands
Else
Commands
Fi

Test command:
The first form of
if test condition
Then
commands
fi
The second form of
Brackets []:
if [condition]
Then
commands
fi
Compound condition judgment
[Condition1] && [Condition2]
[Condition1] | | [Condition2]

Three types of conditions
Numerical comparison
string comparison
File comparison
if.sh

Echoplese input a numread numif[$num-GTTen ] Then        Echo "The num>10"fiif[$num-ltTen] && [$num-gt5 ] Then        Echo "The 5<num<10"fi

[[Email protected]SH]# ./if.SHplese input a num OneThe num>Ten

Cond

Conditional judgment in shell programming (Shell 05)

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.