一、基本判断条件

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

linux之shell之if、while、for语句介绍的更多相关文章

  1. Shell脚本中执行sql语句操作mysql

    对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...

  2. Linux Bash Shell 快速入门

    BASH 的基本语法 最简单的例子 —— Hello World! 关于输入.输出和错误输出 BASH 中对变量的规定(与 C 语言的异同) BASH 中的基本流程控制语法 函数的使用 2.1     ...

  3. Linux/Unix shell 脚本中调用SQL,RMAN脚本

    Linux/Unix shell脚本中调用或执行SQL,RMAN 等为自动化作业以及多次反复执行提供了极大的便利,因此通过Linux/Unix shell来完成Oracle的相关工作,也是DBA必不可 ...

  4. Linux Bash Shell入门教程

    Linux 系统根据 "#!" 及该字串后面的信息确定该文件的类型,关于这一问题同学们回去以后可以通过 "man magic"命令 及 /usr/share/m ...

  5. Linux和Shell教程

    文档资料参考: 参考:http://www.runoob.com/linux/linux-tutorial.html 软件下载参考: centos 下载地址:https://www.centos.or ...

  6. Shell脚本中执行sql语句操作mysql的5种方法【转】

    对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...

  7. linux base shell 基础语法

    转载 本文主要是基础的基础,希望对大家有所帮助 一.Shell基本类型的变量: (1)Shell定义的环境变量: Shell在开始执行时就已经定义了一些和系统的工作环境有关的变量,用户还可以重新定义这 ...

  8. “windows的批处理”与“Linux的shell script”的类比学习

    从2005年开始,做了将近10年的系统维护,先是做网络接入管理,然后做网络安全与审计,然后做服务器管理等整个网络系统的运营管理:现在又兼着做一些Linux下的视频监控系统的软硬件维护.过程中遇到太多重 ...

  9. Linux中Shell

    Linux中Shell Shell是什么 ​ Shell是一个命令行解释器,为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,可以用Shell来启动.挂起.停止.编写一些程序. S ...

  10. Linux 下shell 编程学习脚手架

    linux body { font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 1.6; padding-t ...

随机推荐

  1. Linux命令未找到(command not found),误删Linux path原始路径

    1.执行:/bin/vim /etc/profile (打开并编辑profile将Path修改正确,然后保存退出) 2.执行:export PATH=/usr/bin:/usr/sbin:/bin:/ ...

  2. 使用Tornado实现Ajax请求

    Ajax,指的是网页异步刷新,一般的实现均为js代码向server发POST请求,然后将收到的结果返回在页面上.   这里我编写一个简单的页面,ajax.html <html> <h ...

  3. libevent2源码分析之五:关键的调用链

    用一个调用链来表示函数调用的流程,看起来更直观.根据上面的分析,总结了一些重要的调用链. 初始化 event_base_new event_base_new_with_config min_heap_ ...

  4. 访问vector元素方法的效率比较(转)

    LInux下: gcc 4.47,red hat6 #include<iostream> #include<vector> #include<time.h> usi ...

  5. mac os x 安装adb

    http://stackoverflow.com/questions/31374085/installing-adb-on-mac-os-x Option 1 - Using Homebrew Thi ...

  6. 【Android Studio探索之路系列】之中的一个:Android Studio开篇

    作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.co ...

  7. Spring整合Activiti工作流

    代码地址如下:http://www.demodashi.com/demo/11911.html 一. 前期准备 安装必要的开发环境 eclipse/intellij+maven 3.5.x + tom ...

  8. Quartz.Net线程处理用到的两个Attribute

    1.DisallowConcurrentExecution 加到IJob实现类上,主要防止相同JobDetail并发执行. 简单来说,现在有一个实现了IJob接口的CallJob,触发器设置的时间是每 ...

  9. mongoDB 高级查询之复杂查询$where

    http://blog.csdn.net/drifterj/article/details/7833883

  10. Java代码Bug分析插件 FindBugs

    http://www.oschina.net/p/findbugs FindBugs是一个能静态分析源代码中可能会出现Bug的Eclipse插件工具.