Shell Judgment Statement--Example interpretation

Source: Internet
Author: User
Tags case statement log log

1 if statement

The format of the IF statement:

If condition then command fi

If condition then command else command fi

If condition then command elif condition then command else command fi

Example:

Determines whether the current user is root, and if so, performs/home/test directory compression

#! /bin/bash

If ["$ (id-u)"-eq "0"];then

Tar czf/home/test.tar.gz/home/test

Fi


Require the user to enter a password and determine if the password is correct

#! /bin/bash

Read-p "Enter a passwd:" passwd

If ["$passwd" = = "123456"];then

echo "ok!"

Else

echo "error! Please input your passwd again "

Fi


Judging the score, less than 60 shows a failure, greater than 60 less than 70 display generally, greater than 70 less than 85 shows good, greater than 85 excellent

#! /bin/bash

if [$1-ge];then

echo "Excellent"

elif [$1-ge];then

echo "Good"

elif [$1-ge];then

echo "General"

Else

echo "Failed"

Fi


Case statement

It is a replacement solution for multiple if statements and is easy to read and write

Format 1:

Case $ variable name in

Mode 1)

Command

;;

Mode 2)

Command

;;

Mode 3)

Command

;;

*)

Esac

Format 2:

Case $ variable name in

Mode 1| Mode 2)

Command

;;

Mode 3| Mode 4)

Command

;;

Mode 5| mode 6)

Command

;;

*)

Esac


Example:

Process/var/log log directory, backup only Wednesday Friday data, delete Tuesday Thursday data

#/bin/bash

date=$ (Date +%a)

time=$ (Date +%y+%m+%d)

Case DATE in

wed| Fri)

tar-czf/home/log/${time}_log_tar.gz/var/log/& >/dev/null

;;

tue| Thu)

Rm-rf/var/log/*.2016-*.log

;;

*)

Esac


Determine whether the input is a number or a letter

#/bin/bash

Case $ in

[A-z] | [A-z])

echo "You have type a character"

;;

[[:d Igit:]])

echo "This is a number"

;;

*)

Esac





This article is from the "Practical Linux knowledge and Skills sharing" blog, please be sure to keep this source http://superleedo.blog.51cto.com/12164670/1889263

Shell Judgment Statement--instance interpretation

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.