2024年4月26日发(作者:)

第一部分 If的用法

第一种

if list then

do something here

fi

当list表述返回值为True(0)时,将会执行"do something here"。

#!/bin/sh

myPath="/var/log/httpd/"

myFile="/var /log/httpd/"

1. 是否存在并且是否具有可执行权限

#这里的-x 参数判断$myPath

if [ ! -x "$myPath"]; then

mkdir "$myPath"

fi

2. 目录是否存在

#这里的-d 参数判断$myPath是否存在

if [ ! -d "$myPath"]; then

mkdir "$myPath"

fi

3. 文件是否存在

#这里的-f参数判断$myFile是否存在

if [ ! -f "$myFile" ]; then

touch "$myFile"

fi

4. 变量是否有值

#其他参数还有-n,-n是判断一个变量是否是否有值