Shell script

1 How to determine whether a file or folder exists in a shell script?

if [ test -d folder name] ; then 
echo "Folder exists!" 
else 
echo "Folder does not exist!" 
fi 


if [ -f file.txt ]; then 
   echo "File exists!" 
else 
   echo "File does not exist !" 
fi 



Example
#!/bin/sh

myFile="/var /log/httpd/access.log"
if [ ! -e "$myFile" ]; then
     touch "$myFile"
fi

The difference between -e and -f is , -f for regular file (regular file), -e for all files of any type

Refer to the following:

-e filename True if filename exists
-d filename True if filename is a directory
-f filename If filename is a regular file -L filename True
if filename is a symbolic link
-r filename True if filename is readable
-w filename True if filename is writable
-x filename True if filename is executable
-s filename True if file length is not 0
-h filename True if file is a softlink

2 shell script How to write a multi-conditional statement in the if in the

variable a is equal to aa and the variable b is equal to bb or the variable c is equal to cc and the variable d is equal to dd If the conditions are met, the output is success

if [ $a = "aa" -a $b = " bb" ] || [$c = "cc" -a $d = "dd" ];
then
  echo "success"
fi


3. Logical OR and AND expressions in the shell
Today let's summarize the logical shutdown expressions in the Linux shell.
Expression of logical and:
1), if [ $xxx=a -a $xx=b ]
2), if [ $xxx=a ] && [ $xx=b ]
Expression of logical or:
1), if [ $xxx =a -o $xx=b ]
2), if [ $xxx=a ] || [ $xx=b ]

4. Cyclic Shell break and continue commands
Reference : http://c.biancheng.net/cpp/view /7010.


If you want to add a character after the variable, you can use the following method:
$value1=home
$value2=${value1}"="
echo $value2Add
{} to the string variable to be added, and you need to put $ outside .
The result of this output is: home=, which means that the connection is successful.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326383019&siteId=291194637