Shell Judgments and comparisons

Source: Internet
Author: User
Tags syslog

Http://blog.chinaunix.net/uid-7553302-id-183648.html

1 Shell's $! , $?, $$,[email protected]

    • $n The first parameter,$2 the second ...
    • $# the number of command-line parameters.
    • The name of the current program.
    • $? Last command or function ' s return value.
    • $$ The program ' s PID.
    • $! Last program ' s PID.
    • [Email protected] Save all the parameters.


Almost any shell book would talk about Them,from which you can get their detail usages.

2 Linux SHELL if command parameter description

    • –b return 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

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 digest is from the network

+++++++++++++++++++++++++++++++++++++++++++

Shell judge file, directory exists or has permissions article source: http://hi.baidu.com/haigang/blog/item/e5f582262d639c118b82a167.html#!/bin/sh
Mypath= "/var/log/httpd/"
Myfile= "/var/log/httpd/access.log"
#这里的-x parameter determines whether $mypath exists and has executable permissions
if [!-X "$myPath"]; Then
mkdir "$myPath"
Fi
#这里的-D parameter to determine if $mypath exists
if [!-D "$myPath"]; Then
mkdir "$myPath"
Fi
#这里的-F parameter to determine if $myfile exists
if [!-F "$myFile"]; Then
Touch "$myFile"
Fi
#其他参数还有-n,-n is to determine whether a variable has a value
if [!-n "$myVar"]; Then
echo "$myVar is empty"
Exit 0
Fi
#两个变量判断是否相等
If ["$var 1" = "$var 2"]; Then
Echo ' $var 1 eq $var 2 '
Else
Echo ' $var 1 not EQ $var 2 '
Fi Note: If the format, the statement of spaces can not be less.
========================================
Shell Judgment Statement

The process controls the "if" expression if the condition is true then the following part is executed: if ...; Then
....
Elif ...; Then
....
Else
....
Fi
In most cases, you can use test commands to test the condition.   For example, you can compare strings, determine whether the file exists and whether it is readable, etc... The condition test is usually represented by "[]". Note that the space here is important. The space to ensure the square brackets.
[-F "somefile"]: Determine if it is a file
[-X "/bin/ls"]: Determine if/bin/ls exists and has executable permissions
[-N ' $var]: Determine if the $var variable has a value
["$a" = "$b"]: Determine if $ A and $b are equal-r file user readable as True
-W file user can write as true
-X file user can execute as true
-F file is true for regular files
-d file files are directory-True
-C File file is true for character special files
-B file files are true for block special files
-S file files non-0 o'clock True
-T file is true when the specified device is terminal (default = 1)
######################################################## #含条件选择的shell脚本
Simple shell scripts are generally competent for tasks that do not contain variables. However, when you perform some decision-making tasks, you need to include the if/then criteria to judge. Shell scripting supports such operations, including comparison operations, determining whether a file exists, and so on. The basic if Condition command options are:-eq-compare two parameters for equality (for example, if [2–eq 5])
-ne-comparison of two parameters is not equal
-lt-parameter 1 is less than parameter 2
-le-parameter 1 is less than or equal to parameter 2
-gt-parameter 1 is greater than parameter 2
-ge-parameter 1 is greater than or equal to parameter 2
-f-Check if a file exists (for example, if [-F "filename"])
-D Check if directory exists
Almost all judgments can be implemented with these comparison operators. The common-f command option in a script checks to see if it exists before executing a file. ################################################################## determine if the file exists #!/bin/sh
Today= ' date-d yesterday +%y%m%d '
File= "Apache_$today.tar.gz"
Cd/home/chenshuo/shell
If [-F "$file"];then
echo "OK"
Else
echo "Error $file" >error.log
Mail-s "fail backup from Test"[email protected]<error.log
Fi

Shell Judgments and comparisons

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.