If statement verbose usage

Source: Internet
Author: User

If single branch syntax

if [condition]

Then

Instructions

Fi

===================

If double branch syntax

if [condition]

Then

Instructions

Else

Instructions

Fi

===================

If multi-branch syntax


if [condition]

Then

Instructions

elif [conditions]

Then

Instructions

......

Else

Instructions

Fi

===================

if [condition]

Then

Instructions

elif [conditions]

Then

Instructions

elif [conditions]

Then

Instructions

......

Else

Instructions

Fi

===================

###################################



Used in shell programming to determine parameters within an if statement

- B returns True when file exists and is a block file

- C returns True when file exists and is a character

- D returns True when pathname exists and is a directory

- E returns True when a file or directory specified by pathname is present

- F returns True when file exists and is regular

- G returns True when the file or directory specified by pathname is present and the Sgid bit is set

- H returns True when file exists and is a symbolic link file, this option is not valid on some old systems

- K returns True when a file or directory specified by pathname exists and the "sticky" bit is set

- P returns True when file exists and is a command pipeline

- R returns True when the file or directory specified by pathname is present and readable

- s when file size is larger than 0 o'clock returns true

- U returns True when the file or directory specified by pathname is present and the SUID bit is set

-W Returns True when the file or directory specified by pathname exists and is executable. A directory must be executable for its content to be accessed.

-O Returns True when the file or directory specified by pathname is present and is owned by the user specified by the active user ID of the current process.

UNIX Shell comparison character notation:

-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 unequal

- N non-empty string

-------------------------------------------------------------------------

A more detailed description:

Operator Description Example

File comparison Operators

-e filename true if filename exists [-e/var/log/syslog]

-D filename True if filename is a directory [-d/tmp/mydir]

-F filename True if filename is a regular file [-f/usr/bin/grep]

-L filename True if filename is a symbolic link [-l/usr/bin/grep]

-R filename True if filename is readable [-r/var/log/syslog]

-W filename if filename is writable, true [-w/var/mytmp.txt]

-X filename is true if filename is executable [-l/usr/bin/grep]


Filename1-nt filename2

True if filename1 is newer than filename2 [/tmp/install/etc/services-nt/etc/services]


Filename1-ot filename2

If filename1 is older than filename2, it is true [/boot/bzimage-ot Arch/i386/boot/bzimage]

String comparison operators (note the use of quotation marks, which is a good way to prevent whitespace from disturbing the code)


- z str true if str length is zero [-Z $myvar]

- n str If str length is nonzero, true [-N $myvar]

str1 = str2 True if STR1 is the same as str2 [$myvar = one of the three]

Str1! = str2 true if str1 is different from str2 [$myvar! = one, three]

 


Arithmetic comparison operators

Num1-eq num2 equals [3-eq $mynum]

Num1-ne num2 Not equal to [3-ne $mynum]

Num1-lt num2 less than [3-lt $mynum]

Num1-le num2 less than or equal to [3-le $mynum]

NUM1-GT num2 Greater than [3-gt $mynum]

Num1-ge num2 greater than or equal to [3-ge $mynum]


#################################################################


Check the memory of the script and alarm

[Email protected] script]# VI cheak_mem.sh

#!/bin/sh

Used_mem= ' Free-m | awk ' Nr==3{print $NF} '

If [$used _mem-lt 10000]

Then

echo "Mem is not enough, $used _mem." >> Mem.log

echo "Mem is not enough, $used _mem." | Mail-s "Warning $ (date +%f)" [Email protected]

Fi



Do timed tasks

[email protected] script]# cat cheak_mem.sh

#!/bin/sh

Used_mem= ' Free-m | awk ' Nr==3{print $NF} '

If [$used _mem-lt 10000]

Then

echo "Mem is not enough, $used _mem." >> Mem.log

echo "Mem is not enough, $used _mem." | Mail-s "Warning $ (date +%f)" [Email protected]

Fi

[Email protected] script]# crontab-l

#########################

*/3 * * * */bin/sh/root/script/cheak_mem.sh &>/dev/null

[Email protected] script]#


########################################################

Calculating the parameters

[email protected] script]# cat jisuan.sh

#!/bin/sh

If [$#-ne 2]

Then

echo "Error:please input number!"

echo "Usage:$0 num1 num2"

Exit 1

Fi

Expr $ + $ &>/dev/null

If [$?-ne 0]

Then

echo "Error:please input number!"

echo "Usage:$0 num1 num2"

Exit 2

Else

echo "$-$ = $ (($1-$2))"

echo "$ + $ = $ (($1+$2))"

echo "$ * $ = $ (($1*$2))"

echo "$/$ = $ (($1/$2))"

echo "% $ = $ (($1%$2))"

Fi

[Email protected] script]#


This article is from the "Struggle Bar" blog, please be sure to keep this source http://lvnian.blog.51cto.com/7155281/1701060

If statement verbose usage

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.