Linux shell script - if operation example

Thanks to the author's original address: http://blog.csdn.net/shandong_chu/article/details/8633588

 

The following is a specific example to describe the use of if statement in shell script

 

#!/bin/sh

#define a variable, define a variable, there cannot be spaces on both sides of the equal sign
v="test shell commond"
#print the variable, and no hyphen is also supported
echo "variable is: " $v

 

 

#Check if this file exists in the current directory

 

file="test.sh" #Define the file to look for

 

# [ -f "somefile" ] : Determine if it is a file

if [ -f "$file" ];then #It should be noted here that there must be a space after the if, and there must be a space in front of the [behind, ], then a semicolon, and finally then
   echo "001"
else
   echo "002"
fi

 

 

#According to the set number size to judge, and then output the result

num=8
echo "the number is: " $num
if [ $num -gt 10 ];then
   echo "the unmber is larger than 10"
elif [ $num -eq 10 ];then
   echo "the number is equal with 10"
else
   echo "the number is smaller than 10"

be

 

 

 

The following is the content summarized by the cattle in the network for reference.

 

The shell determines whether a file or directory exists or has permissions

 

#!/bin/sh
myPath="/var/log/httpd/"
myFile="/var /log/httpd/access.log" #The
-x parameter here determines whether $myPath exists and whether it has executable permissions
if [ ! -x "$myPath"]; then
mkdir "$myPath"
fi #The
-d parameter here 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, -n is to determine whether a variable has a value
if [ ! -n "$ myVar" ]; then
echo "$myVar is empty"
exit 0
fi #Two
variables are equal
if [ "$var1" = "$var2" ]; then
echo '$var1 eq $var2'
else
echo '$var1 not eq $var2'
fi

=========================================
shell judgment statement

flow control "if" expression If the condition is true then execute the part after then:

 

if ....; then
....
elif ....; then
....
else
....
fi

In most cases, conditions can be tested using the test command. For example, you can compare strings , determine whether the file exists and whether it is readable , etc...   

 

Usually " [ ] " is used to denote conditional tests. Note that spaces are important here. Be sure to space the square brackets.
[ -f "somefile" ] : Determine whether it is a file
[ -x "/bin/ls" ] : Determine whether /bin/ls exists and has executable permissions
[ -n "$var" ] : Determine whether the $var variable is Has value
[ "$a" = "$b" ] : Determine whether $a and $b are equal         

-r file user-readable as true
-w file user-writable as true
-x file user-executable as true
-f file file is regular file is true
-d file file is directory true
-c file file is character special file is true
-b file file is a block special file true
-s file true if the file size is non-zero
-t file true when the device specified by the file descriptor (1 by default) is a terminal

######### ################################################ with conditions The chosen
    shell script is generally adequate for tasks that do not contain variables. However, when performing some decision-making tasks, it is necessary to include if/then conditional judgments. Shell script programming supports such operations, including comparison operations, judging whether a file exists, etc. The basic if conditional command options are:
-eq — compares two arguments for equality (for example, if [ 2 –eq 5 ])
-ne — compares two arguments for inequality
-lt — argument 1 is less than argument 2
-le — Whether parameter 1 is less than or equal to parameter 2
-gt - whether parameter 1 is greater than parameter 2
-ge - whether parameter 1 is greater than or equal to parameter 2
-f - check whether a file exists (for example, if [ -f "filename" ])
-d — Check for the existence of a directory
Almost all decisions can be made with these comparison operators. The -f command option is commonly used in scripts to check for the existence of a file before executing it.
################################################## ################ 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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326614169&siteId=291194637