Some keywords in shell programming-to be sorted

Source: Internet
Author: User
Tags syslog

1 shell $! , $ ?, $, $ @

 

• $ N $1 the first parameter, $2 the second...
• $ # The number of command-line parameters.
• $0 the name of current program.
• $? Last Command or function's return value.
• $ The program's PID.
• $! Last program's PID.
• $ @ Save all the parameters.

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

2 Linux Shell if command parameter description

•-B returns true if the file exists and is a block File
•-C returns true if the file exists and is a character file
•-D returns true if pathname exists and is a directory
•-E returns true if the file or directory specified by pathname exists
•-F returns true if the file exists and is a regular file
•-G returns true if a file or directory specified by pathname exists and the sgid bit is set.
•-H returns true if the file exists and is a symbolic link file. This option is invalid in some old systems.
•-K returns true if the file or directory specified by pathname exists and a "Sticky" bit is set
•-P returns true if the file exists and is a command Pipeline
•-R returns true if a file or directory specified by pathname exists and is readable
•-S returns true if the file size is greater than 0
•-U returns true if a file or directory specified by pathname exists and SUID is set
•-W returns true if the file or directory specified by pathname exists and is executable. A directory must be executable for its content access.
•-O returns true if a file or directory specified by pathname exists and is owned by the user specified by the valid user ID of the current process.
Comparison character writing in Unix shell:

•-EQ equals
•-Ne is not equal
•-GT greater
•-Lt is less
•-Le is less than or equal
•-Ge is greater than or equal
•-Z empty string
• = Two equal characters
•! = Two characters
•-N non-empty strings
Summary:

Document comparison operator
-E filename if filename exists, it is true [-E/var/log/syslog]
-D filename if filename is a directory, it is true [-D/tmp/mydir]
-F filename if filename is a regular document, it is true [-F/usr/bin/grep]
-L filename if filename is a symbolic link, it is true [-L/usr/bin/grep]
-R filename is true if filename is readable [-r/var/log/syslog]
-W filename if filename is writable, it is true [-W/var/mytmp.txt]
-X filename if filename is executable, it is true [-L/usr/bin/grep]
Filename1-nt filename2 if filename1 is newer than filename2, true [/tmp/install/etc/services-nt/etc/services]
Filename1-ot filename2 if filename1 is older than filename2, true [/boot/bzimage-ot ARCH/i386/boot/bzimage]
String comparison operator (please note the use of quotation marks, which is a good way to prevent space from disturbing the code)
-Z string if the string length is zero, it is true [-z "$ myvar"]
-N string if the string length is not zero, it is true [-n "$ myvar"]
String1 = string2 if string1 and string2 are the same, it is true ["$ myvar" = "One Two Three"]
String1! = String2 if string1 is different from string2, it is true ["$ myvar "! = "One Two Three"]
Arithmetic comparison operator
Num1-eq num2 equals to [3-EQ $ mynum]
Num1-ne num2 is not equal to [3-ne $ mynum]
Num1-lt num2 less than [3-lt $ mynum]
The num1-le num2 is less than or equal to [3-Le $ mynum]
Num1-gt num2 greater than [3-GT $ mynum]
Num1-ge num2 is greater than or equal to [3-ge $ mynum]

This document is excerpted from the network

++ ++

Shell checks whether a file or directory exists or has permissions.
Source: http://hi.baidu.com/haigang/blog/item/e5f582262d639c118b82a167.html
#! /Bin/sh
Mypath = "/var/log/httpd /"
Myfile = "/var/log/httpd/access. log"
# Here, the-x parameter determines whether $ mypath exists and has executable permissions.
If [! -X "$ mypath"]; then
Mkdir "$ mypath"
Fi
# Here, the-D parameter determines whether $ mypath exists.
If [! -D "$ mypath"]; then
Mkdir "$ mypath"
Fi
# Here, the-F parameter determines whether $ myfile exists.
If [! -F "$ myfile"]; then
Touch "$ myfile"
Fi
# Other parameters include-N, which determines whether a variable has a value.
If [! -N "$ myvar"]; then
Echo "$ myvar is empty"
Exit 0
Fi
# Determine whether two variables are equal
If ["$ var1" = "$ var2"]; then
Echo '$ var1 EQ $ var2'
Else
Echo '$ var1 not EQ $ var2'
Fi

Note: The if format cannot contain fewer spaces.
==========================================================
Shell judgment statement

If the "if" expression of process control is true, the part after then is executed: If...; then.
....
Elif...; then
....
Else
....
Fi
In most cases, you can use test commands to test the conditions. For example, you can compare strings, determine whether a file exists, and whether the file is readable... "[]" Is usually used to represent a conditional test. Note that spaces are important. Make sure that the square brackets have spaces.
[-F "somefile"]: determines whether it is a file.
[-X "/bin/ls"]: determines whether/bin/ls exists and has the executable permission.
[-N "$ Var"]: determines whether the $ var variable has a value.
["$ A" = "$ B"]: determines whether $ A and $ B are equal.-R file users can be read as true.
-W file users can write to true
-X file user executable is true
-F file: the regular file is true.
-D file: the directory is true.
-C file: the special character file is true.
-B file: The Block special file is true.
-S file is true if the file size is not 0
-T file: this parameter is true when the specified device is a terminal in the file descriptor (1 by default ).
######################################## #################
Shell script with conditional Selection
Simple shell scripts for tasks without variables are generally competent. However, when executing some decision-making tasks, it is necessary to include the IF/then condition judgment. Shell script programming supports such operations, including comparison operations and determining whether a file exists. The basic if condition Command Options include-eq-to compare whether two parameters are equal (for example, if [2-EQ 5]).
-Ne-compare whether two parameters are not equal
-Lt-parameter 1: whether it is smaller than parameter 2
-Le-parameter 1: whether it is less than or equal to parameter 2
-GT-whether parameter 1 is greater than parameter 2
-Whether Ge-parameter 1 is greater than or equal to parameter 2
-F-check whether a file exists (for example, if [-F "FILENAME"])
-D-check whether the directory exists
Almost all judgments can be implemented using these comparison operators. In the script, the common-F Command Option checks whether a file exists before executing it. ######################################## ######################### Determine whether a 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" test@test.com <error. Log
Fi

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/lihe2008125/archive/2009/07/14/4348136.aspx

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.