网站建设的步骤有哪些网站开发交接
if 语句
if 语句是 Shell 脚本中用于条件判断的基本结构。
基本语法
if 语句的基本语法如下:
if [ condition ]
thencommands
fi
condition是要测试的条件。commands是在条件为真时要执行的命令。
示例
简单条件判断
#!/bin/bashif [ 1 -eq 1 ]
thenecho "1 is equal to 1"
fi
if-else 语句
if-else 语句允许你在条件为假时执行其他命令。
#!/bin/bashif [ 1 -eq 2 ]
thenecho "This will not be printed"
elseecho "1 is not equal to 2"
fi
if-elif-else 语句
if-elif-else 语句允许你测试多个条件。
#!/bin/bashnum=3if [ $num -eq 1 ]
thenecho "The number is 1"
elif [ $num -eq 2 ]
thenecho "The number is 2"
elseecho "The number is neither 1 nor 2"
fi
条件测试
数值比较
-eq:等于-ne:不等于-lt:小于-le:小于等于-gt:大于-ge:大于等于
#!/bin/basha=5
b=10if [ $a -lt $b ]
thenecho "$a is less than $b"
fi
字符串比较
- =:等于
!=:不等于<:小于(在 ASCII 顺序上)>:大于(在 ASCII 顺序上)-z:字符串为空-n:字符串不为空
#!/bin/bashstr1="hello"
str2="world"if [ "$str1" = "$str2" ]
thenecho "The strings are equal"
elseecho "The strings are not equal"
fi
文件测试
-e filename:文件存在-r filename:文件可读-w filename:文件可写-x filename:文件可执行-d filename:文件是目录-f filename:文件是普通文件-s filename:文件非空
#!/bin/bashfile="test.txt"if [ -e "$file" ]
thenecho "File exists"
elseecho "File does not exist"
fi
