一、基本判断条件

1)逻辑运算符

-a    expr1 -a expr2    逻辑与

-o    expr1 -o expr2    逻辑或

!     !expr1                   逻辑非

2)数值判断

-eq    num1 -eq num2    是否相等

-ne    num1 -ne num2    是否不相等

-gt     num1 -gt num2     是否大于

-ge    num1 -ge num2    是否大于等于

-lt      num1 -lt num2      是否小于

-le     num1 -le num2     是否小于等于

3)字符串判断

=      str1 = str2       字符串是否相等

!=    str1 != str2      字符串是否不等

=~   str1 =~ str2     str1包含str2,注意整个条件放在"[[]]"之间

-n    -n str1              字符串长度是否不等于0

-z    -z str2               字符串长度是否等于0

4)文件判断

-r       -r filename          文件是否存在且可读

 -w -w filename  文件是否存在且可写

 -s   -s filename   文件是否存在且长度非0

 -f   -f filename    文件是否存在且是普通文件

 -d  -d filename   文件是否存在且是一个目录

二、if判断语句基本格式:

1)if判断语句基本格式1:

if  [ 判断条件 ]

then

commands

else

fi

举例:

#数值判断:

read -p "enter a number(0/1): " num

if [ $num -eq 1 ]

then

echo "true"

else

echo "false"

fi

#字符串判断:

str1="this is a string"

if [[ "$str1" =~ "this" ]]

then

echo "true"

else

echo "false"

fi

#文件判断:

if [ -f ./test1.sh ]

then

echo "true"

else

echo "false"

fi

2)if判断语句基本格式2:

if  [ 判断条件 ]; then

commands

elif [ 判断条件 ]; then

commands

else

commands

fi

#举例:

read -p "enter the score: " score

if [ "$score" -lt 0 ]; then

echo "enter a score in 0~100"

elif [ "$score" -gt 100 ]; then

echo "enter a score in 0~100"

elif [ "$score" -lt 60 ]; then

echo "fail score"

elif [ "$score" -lt 70 ]; then

echo "pass score"

elif [ "$score" -lt 80 ]; then

echo "fair score"

elif [ "$score" -lt 90 ]; then

echo "good score"

else

echo "excellent score"

fi

#注意:

1、 if与[]之间必须有空格

2、判断条件前后必须有空格

3、then如果和if在同一行要用;分开

4、判断条件含有变量,要用双引号引起来

三、while循环语句基本格式

1)while循环语句基本格式:

#while  [ 判断条件 ]

min=1

max=10

while [ $min -le $max ]

do

echo $min

min=`expr $min + 1`

done

#注意:

1、while与[]之间必须要有空格

2、判断条件前后要有空格

# while((判断条件))

i=1

while(($i<=12))

do

if(($i%4==0))

then

echo $i

fi

i=$(($i+1))

done

#注意:

1、此种形式的内部结构类似c语言

2、注意赋值计算:i=$(($i+1))

四、for循环语句基本格式

1)for循环语句基本格式:

#数字段形式

for i in {1..10}

do

echo $i

done

#详细列出(项数不多)

for i in 'a' 'b' 'c' 'd'

do

echo $i

done

#seq形式 起始从1开始

for i in `seq 10`

do

echo $i

done

#语法循环--有点像C语法

for ((i=1; i<+10; i++));

do

echo $i

done

#对存在的文件进行循环

for shname in `ls *.sh`

do

echo "$shname" | awk -F. '{print $1}'

done

最新文章

  1. 通读AFN③--HTTPS访问控制(AFSecurityPolicy),Reachability(AFNetworkReachabilityManager)
  2. 【shadow dom入UI】web components思想如何应用于实际项目
  3. FineUI官方论坛出现空白页的解决办法!
  4. 采用UDP协议实现PIC18F97J60 ethernet bootloader
  5. ASP.NET Core 发布至Linux生产环境 Ubuntu 系统
  6. Zipf定律
  7. dataTables表格分页排序等交互
  8. HDU 1160 FatMouse&#39;s Speed (sort + dp)
  9. Android xml 解析
  10. 利用System.Net.Mail和多线程实现邮件发送
  11. UIWebView与JavaScript(JS) 回调交互 -备
  12. 框架和css基础
  13. Missing artifact net.sf.json-lib:json-lib:jar:2.4错误和Eclipse安装Maven插件错误
  14. Wp-UserAgent——让WordPress在评论后面加上浏览器和操作系统信息
  15. 火狐浏览器中如何删除保存的cookie
  16. multiset的erase()操作中出现跳过元素的问题
  17. gulp打包普通项目
  18. ComM(通信管理)和CanNm(network)
  19. input标签checkbox选中触发事件的方法
  20. hive reduce 阶段GC Exception

热门文章

  1. eclipse 配置maven 项目tomcat 运行
  2. C语言中的union使用方法
  3. 倍福TwinCAT(贝福Beckhoff)应用教程11.1 TwinCAT应用小程序1 如何读写数字量模拟量输入输出(DI,DO,AI,AO)
  4. vue - 总结build.js
  5. 轻量级代码生成器-OnlyCoder 第一篇
  6. docker发布spring cloud应用
  7. datagridview 日期列排序
  8. word转pdf
  9. DNS的概念,用途,DNS查询的实现算法
  10. js 判断是否为数组