Shell if judgment

Source: Internet
Author: User
Tags snmp snmp error syslog

#!/bin/sh


/ETC/INIT.D/SNMPD start && >/dev/null

Pid= '/ETC/INIT.D/SNMPD status| grep pid ' # '/etc/init.d/mysqld status| grep "not running" to see if the status of the service is healthy with the status state

If ["$pid" = = ""]; Then #判断grep的关键词是否为空

echo "SNMP error";

Else

echo "SNMP is OK";

Fi



#mysql进程是否存在

#!/bin/sh


Pid= '/etc/init.d/mysqld status| grep "Not Running" '

If ["$pid" = = ""]; Then

echo "MySQL OK";

Else

echo "$pid"

Service mysqld Start


UNIX Shell comparison character notation:

    • -eq    equals

    • -ne    not equals

    • -gt    greater than

    • -lt    less than

    • -le    is less than or equal to

    • -ge   is greater than or equal to

    • -z    empty string

    • =     two words typeface etc.

    • !=    two characters are not equal to

    • -n    non-empty string

Summarize:

Document comparison operators
-e filename If filename exists, true [-e/var/log/syslog]
-d filename If filename is a directory, then true [-d/tmp/mydir]
-F filename True if filename is a regular document [-f/usr/bin/grep]
-L filename True if filename is a symbolic link [-l/usr/bin/grep]
-r filename If filename is readable, true [-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 if filename1 than filename2 new, then true [/tmp/install/etc/services-nt/etc/services]
Filename1-ot filename2 if filename1 than filename2 old, then 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 string true [-Z ' $myvar] if string length is zero
-N String if string length is nonzero, true [-n ' $myvar ']
string1= string2 if string1 and string2 are the same, then true ["$myvar" = "One of the three"]
string1!= string2 if string1 and string2 are different, then true ["$myvar"! = "one of the 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]


This article from "Ren Long" blog, reproduced please contact the author!

Shell if judgment

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.