shell if 学习

if 表达式: shell


if 条件
then
    command
else
    command
fi
另外一种写法:



[ -f "~/test.sh" ] && echo "test.sh is ecisted"
shell 的 if 与 C 语言 if 的区别:


【if xx then】 shell then是xx执行结果为0执行,c是执行结果不为0执行 函数

【if [i -ne 0]】【if (i)】 整数为0判断,shell与c spa

【if [ str ]】【if (strlen == NULL || strlen(str) == 0)】字符串为空判断,shell与c code

if特性: ci

if支持多条command以及函数执行的返回值判断 字符串

if command == command+if $? ------- if [ $? -eq 0 ] class

if的条件表达式: test

文件: file

if [ -f file ] 文件存在判断 command

if [ -d ... ] 目录存在判断

if [ -s file ] 文件存在且不为空判断

if [ -r file ] 文件存在可读判断

if [ -w file ] 文件存在可写判断

if [ -x file ] 文件存在可执行判断

整数:

if [ a -eq b ] a==b判断

if [ a -ne b ] a!=b判断

if [ a -ge b ] a>=b判断

if [ a -gt b ] a>b判断

if [ a -le b ] a<=b判断

if [ a -lt b ] a<b判断

字符串:

if [ $a = $b ] 字符串a等于字符串b

if [ $a != $b ] 字符串a不等于字符串b

if [ -n $a ] 字符串a非空

if [ -z $a ] 字符串a为空

if [ $a ] 字符串a非空

表达式须要带$符号

-eq -ne -ge -gt -le -lt 只能用于整数

=用于等于判断时须要两边加空格

shell没有<或者>,只有-eq -ge -le -ht -lt

逻辑非!,逻辑与 -a,逻辑或 -o

相关文章
相关标签/搜索