Linux system Shell if statement usage summary

  

1, string judgment

str1 = str2 When two strings have the same content, length is true str1 != str2 When the strings str1 and str2 are not equal True-n str1 True when the length of the string is greater than 0 (string is not empty) -z str1 True when the length of the string is 0 (empty string) str1 True when the string str1 is not empty 

2, number Judgment

int1 -eq int2 The two numbers are equal to true int1 -ne int2 The two numbers are not equal to true int1 -gt int2 int1 is greater than int2 is true int1 -ge int2 int1 is greater than or equal to int2 is true int1 -lt int2 Int1 is less than int2 is true int1 -le int2 int1 is less than or equal to int2 is true 

3 file judgment

-r file user readable as true -w file user can be written as true -x file user executable True -f file for a regular file is true -d file The directory is true - the c file file is the character special file is true - the b file file is the block special file is true - s file is not true when the file size is non-zero -t file When the file descriptor (default is 1) specified device True for the terminal 

3, complex logic judgment

-a and -o or! Non 

Here are some use cases:

#!/bin/shmyPath=" ;/var/log/httpd/"myFile="/var /log/httpd/access.log"# The -x parameter here determines if $myPath exists and has executable permissions if [ ! -x "$ myPath"]; thenmkdir "$myPath"fi# The -d parameter here determines whether $myPath exists if [ ! -d "$myPath"]; thenmkdir "$myPath"fi# where the -f parameter evaluates $myFile Is there if [ ! -f "$myFile" ]; thentouch "$myFile"fi#Other parameters are also -n, -n is to determine whether a variable has a value if [ ! -n "$myVar" ] ;nenecho "$myVar is empty" Exit 0fi#Two variables determine whether they are equal [ "$var1" == "$var2" ]; thenecho '$var1 eq $var2'elseecho '$var1 not eq $var2'fi if list then do something here elif List then do another thing here else do something else here fi EX1:#!/bin/shSYSTEM=`uname -s` #Get the operating system type, my local is linuxif [ $SYSTEM = "Linux" ] ; then # if For Linux, print the linux string echo "Linux"elif [ $SYSTEM = "FreeBSD" ] ; then echo "FreeBSD"elif [ $SYSTEM = "Solaris" ] ; thenecho "Solaris"elseecho "What?" ;fi #ifend
> Basically the same as other scripting languages. There is not much difference. But it is worth noting. The condition inside [] is judged. > ========================================================= ========================== $HOME/.bash_profilecd $HOMEsh .bash_profilecd $HOME/jnjzapp/sos_dx/echo -e 'Start'. /jn_card_dx.shwait#sqlplus $DBSTR <

1, there must be a space between the 2 and [] and the judgment condition must also have a space between the 3,] and ; there can be no space between

Copyright © Windows knowledge All Rights Reserved