Shell Script Process Control

Source: Internet
Author: User

4.1.3 Shell Process Control Statements

If condition judgment Statement

If ( expression ) #if (Variable in Array)

Statement 1

Else

Statement 2

Fi


Case one, test if the directory exists, does not exist then new (note that space must be between the brackets)

#!/bin/sh

#judge direxist

if [! -d/data/20140515];then

mkdir-p/data/20140515

Else

echo "This DIR isexist,please exit ..."

Fi

Logical operator parsing:

- F determine if a file exists Eg:if [-f filename]

- D determine if a directory exists Eg:if [-D dir]

-eq equals applies To: integer comparison

-ne not equal to: integer comparison

-lt less than applied to: integer comparison

-GT greater than applied to: integer comparison

-le less than or equal to: integer comparison

-ge greater than or equal to: integer comparison

-A Both sides were established ( and ) Logical Expression – a Logical Expressions

- o Unilateral Establishment ( or ) Logical Expression – o Logical Expressions

- Z empty string

Case three, multiple condition test judgment

#!/bin/sh

scores=80;

if [[$scores-GT 85]]; Then

echo "Very good!";

elif [[$scores-GT 75]]; Then

echo "good!";

elif [[$scores-GT 60]]; Then

echo "pass!";

Else

echo "No pass!";

Fi

Loop statement for

for variables inch string

Do

Statement 1

Done

case one, print seq Multiple Numbers

#!/bin/sh

For i in ' seq 15 '

Do

echo "NUMis $i"

Done

case two, find the relevant Log , and then bulk pack

#!/bin/sh

For i in ' Find/var/log-name "*.log" '

Do

TAR–CZF 2014log.tgz $i

Done

Looping Statements while

while conditional Statements

Do

Statement 1

Done

Case One, while Conditional Judgment Number

#!/bin/sh

I=1;

while [[$i-lt]];d o

echo $i;

((i++));

Done

case Two, while read a file's contents row by line

#!/bin/sh

While Read line

Do

Echo $line;

done</etc/hosts

Case SELECT statement

Case $arg in

PATTERN1)

Statement 1

;;

PATTERN2)

Statement 2

;;

*)

Statement 3

;;

Esac

Case one, create a selection parameter script

#!/bin/sh

Case $ in

Monitor_log)

Monitor_log

;;

Archive_log)

Archive_log

;;

*          )

echo "Usage:{$0 Monitor_log | Archive_log |help} "

;;

Esac


This article from "Do not forget Beginner's mind" blog, reproduced please contact the author!

Shell Script Process Control

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.