The IF statement for Linux shell programming

Source: Internet
Author: User

-eq equals

-ne Not equal to

-GT Greater than

-lt less than

-le less than or equal to

-ge greater than or equal to

-Z Empty string

= Two characters equal

! = Two characters are not equal

-N Non-empty string

Strings are commonly used to check user input, whether the system environment satisfies the criteria, and in shell scripts that provide interactive operations, to determine whether the user input positional parameters are compliant, and the usual operations of strings are as follows:


Document comparison operators:

-e filename If filenanme exists, it is true

-d filename If filename is a directory, true

-F filename True if filename is a regular document

-l filename If filename is a symbolic link, true

-r filename If filename is readable, true

-W filename if filename is writable, true

-x filename If filename is executable, true


The IF statement is a circular statement in the form of a control statement, by establishing conditions for the execution of the judgment, only when the condition is true, the corresponding code will be executed, otherwise no action will be taken.


* Single Branch if statement format:

If condition test action

Then

Command sequence

Fi


Example 1

Vim A.txt

#!/bin/bash

If

[!-d/media/cdrom]

Thenk

Mkdir-p/media/cdrom

Fi

wq!

Sh-x a.txt x option for viewing the debugging process


Example 2

Vim B.txt

#! /bin/bash

num=100

if (($NUM > 4)); then

echo "This num is $NUM greater 4!"

Fi

wq!

Sh-x B.txt



* Dual-Branch if statement format:

Two-branch selection results, the requirements for the condition is set up, the conditions are not set up to perform different operations, respectively

Grammatical structure

If condition test action

Then (conditional execution)

Command sequence 1

else (otherwise, if the condition is not set, then execute)

Command Sequence 2

Fi


Example 3

Vim C.txt

#!/bin/bash

Ping-c 3-i 0.2-w 3 $

If

[$?-eq 0]

Then

echo "IP is Up"

Else

echo "IP is down"

Fi

Sh-x C.txt 172.0.0.1


Example 4

Vim B.txt

#!/bin/bash

Word=$1

grep ^$1$ "/usr/share/dict/words-q

If [$?-eq 0];then

echo "Word is a dictionary word"

Else

echo "Word is not a dictionary word"

Fi



This article is from the Oracle Personal Learning Notes blog, so be sure to keep this source http://wuchunqiang.blog.51cto.com/1022331/1853496

The IF statement for Linux shell programming

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.