Shell Script Application (v) if statement

Source: Internet
Author: User

    1. Single-branch if statement:

If condition test action

Then

Command Sequence

Fi

Example: Determine if the mount point directory exists, or create a new directory if it does not exist.

[email protected] script]# cat chkmountdir.sh

#!/bin/bash

Mount_dir= "/media/cdrom"

if [!-D $MOUNT _dir]

Then

Mkdir-p $MOUNT _dir

Fi

[Email protected] script]# chmod +x chkmountdir.sh

[Email protected] script]#./chkmountdir.sh

[Email protected] script]# ls/media/

CDROM rhel_6.5 x86_64 Disc 1

Example: If the current user is not root, then prompt an error:

[email protected] script]# cat chkifroot.sh

#!/bin/bash

If ["$USER"! = "root"];then

echo "Error: Non-root user, insufficient permissions! "

Exit 1

Fi

Fdisk-l/DEV/SDA

[Email protected] script]# chmod +x chkifroot.sh

[Email protected] script]#./chkifroot.sh


disk/dev/sda:53.7 GB, 53687091200 bytes

255 heads, Sectors/track, 6527 cylinders

Units = Cylinders of 16065 * 8225280 bytes

Sector size (logical/physical): bytes/512 bytes

I/O size (minimum/optimal): bytes/512 bytes

Disk IDENTIFIER:0X000E59BD


Device Boot Start End Blocks Id System

/DEV/SDA1 * 1 512000 Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2 6528 51915776 8e Linux LVM

[Email protected] script]# su-oracle

[Email protected] ~]$ source/script/chkifroot.sh

Error: Non-root user, insufficient permissions!

2. Dual-Branch If statement:

If condition test action

Then

command sequence 1

Else

Command Sequence 2

Fi

Example: Write a test script for connectivity that provides the destination host address via the location parameter. Then the corresponding prompt is given according to the ping test results.

[email protected] script]# cat ping.sh

#!/bin/bash

Ping-c 3-i 0.2-w 3 $ &>/dev/null

If [$?-eq 0]

Then

echo "Host is up."

Else

echo "Host is down."

Fi

[Email protected] script]# chmod +x ping.sh

[Email protected] script]#/ping.sh 192.168.1.11

Host 192.168.1.11 is up.

[Email protected] script]#/ping.sh 192.168.1.12

Host 192.168.1.12 is down.

Example: Check whether the HTTPD service is running through shell script;

[email protected] script]# cat chkhttpd.sh

#!/bin/bash

/ETC/INIT.D/HTTPD Status &>/dev/null

If [$?-eq 0];then

echo "Listening Address: $ (netstat-anpt |grep httpd | awk ' {print $4} ') "

echo "Process PID Number: $ (pgrep-x httpd)"

Else

echo "Warning: httpd Service Unavailable"

Fi

[Email protected] script]# chmod +x chkhttpd.sh

[Email protected] script]#./chkhttpd.sh

Warning: The HTTPD service is not available

[[Email protected] script]# service httpd start

[Email protected] script]#./chkhttpd.sh

Listening Address::: 80

Process PID Number: 2398

3. Multi-Branch If statement

If condition test action 1

Then

Command sequence 1

Elif Condition Test Action 2

Then

Command Sequence 2

Else

Command Sequence 3

Fi

Example: Writing a score sub-file script;

[email protected] script]# cat gradediv.sh

#!/bin/bash

Read-p "Please enter your score [0-100]:" GRADE

If [$GRADE-ge] && [$GRADE-le];then

echo "$GRADE points! You have a good score.

elif [$GRADE-ge] && [$GRADE-le];then

echo "$GRADE points! It's okay, lad.

Else

echo "$GRADE points! You're a piece of junk.

Fi

[Email protected] script]# chmod +x gradediv.sh

[Email protected] script]#./gradediv.sh

Please enter your score [0-100]:100

100 points! You have a great score.

[Email protected] script]#./gradediv.sh

Please enter your score [0-100]:78

78 points! It's okay, lad.

[Email protected] script]#./gradediv.sh

Please enter your score [0-100]:50

50 points! You're a piece of crap.

Here it is, slowly accumulating.

This article is from the "one dish can no longer dish of the Little rookie" blog, please be sure to keep this source http://liliming.blog.51cto.com/10925034/1793023

Shell Script Application (v) if statement

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.