shell的if-else的基本用法

if 语句通过关系运算符判断表达式的真假来决定执行哪个分支。Shell 有三种 if ... else 语句:

  • if ... fi 语句;
  • if ... else ... fi 语句;
  • if ... elif ... else ... fi 语句。

1) if ... else 语句

if ... else 语句的语法:

if [ expression ]
then
   Statement(s) to be executed if expression is true
fi

如果 expression 返回 true,then 后边的语句将会被执行;如果返回 false,不会执行任何语句。

最后必须以 fi 来结尾闭合 if,fi 就是 if 倒过来拼写,后面也会遇见。

注意:expression 和方括号([ ])之间必须有空格,否则会有语法错误。

举个例子:

  1. #!/bin/sh
  2. a=10
  3. b=20
  4. if [ $a == $b ]
  5. then
  6. echo "a is equal to b"
  7. fi
  8. if [ $a != $b ]
  9. then
  10. echo "a is not equal to b"
  11. fi

运行结果:

a is not equal to b

2) if ... else ... fi 语句

if ... else ... fi 语句的语法:

if [ expression ]
then
   Statement(s) to be executed if expression is true
else
   Statement(s) to be executed if expression is not true
fi

如果 expression 返回 true,那么 then 后边的语句将会被执行;否则,执行 else 后边的语句。

举个例子:

  1. #!/bin/sh
  2. a=10
  3. b=20
  4. if [ $a == $b ]
  5. then
  6. echo "a is equal to b"
  7. else
  8. echo "a is not equal to b"
  9. fi

执行结果:

a is not equal to b

3) if ... elif ... fi 语句

if ... elif ... fi 语句可以对多个条件进行判断,语法为:

if [ expression 1 ]
then
   Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
   Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
   Statement(s) to be executed if expression 3 is true
else
   Statement(s) to be executed if no expression is true
fi

哪一个 expression 的值为 true,就执行哪个 expression 后面的语句;如果都为 false,那么不执行任何语句。

举个例子:

  1. #!/bin/sh
  2. a=10
  3. b=20
  4. if [ $a == $b ]
  5. then
  6. echo "a is equal to b"
  7. elif [ $a -gt $b ]
  8. then
  9. echo "a is greater than b"
  10. elif [ $a -lt $b ]
  11. then
  12. echo "a is less than b"
  13. else
  14. echo "None of the condition met"
  15. fi

运行结果:

a is less than b

if ... else 语句也可以写成一行,以命令的方式来运行,像这样:

  1. if test $[2*3] -eq $[1+5]; then echo ‘The two numbers are equal!‘; fi;

if ... else 语句也经常与 test 命令结合使用,如下所示:

  1. num1=$[2*3]
  2. num2=$[1+5]
  3. if test $[num1] -eq $[num2]
  4. then
  5. echo ‘The two numbers are equal!‘
  6. else
  7. echo ‘The two numbers are not equal!‘
  8. fi

输出:

The two numbers are equal!

test 命令用于检查某个条件是否成立,与方括号([ ])类似。

时间: 2024-08-26 02:30:56

shell的if-else的基本用法的相关文章

shell中$0,$?,$!等的特殊用法

shell中$0,$?,$!等的特殊用法 变量说明: $$Shell本身的PID(ProcessID)$!Shell最后运行的后台Process的PID$?最后运行的命令的结束代码(返回值)$-使用Set命令设定的Flag一览$*所有参数列表.如"$*"用「"」括起来的情况.以"$1 $2 … $n"的形式输出所有参数.[email protected]所有参数列表.如"[email protected]"用「"」括起来的情况

(转)轻松掌握shell编程中数组的常见用法及示例

缘起:在老男孩进行linux培训shell编程教学中,发现不少水平不错的网友及同学对数组仍然很迷糊,下面就给大家分享下数组的用法小例子,希望能给大家一点帮助.其实SHELL的数组很简单,好用.我们学习都应该遵循简单.易用的原则. shell编程中数组的简单用法及示例 新版本的Bash支持一维数组. 数组元素可以使用符号variable[xx]等方式来初始化. 另外, 脚本可以使用declare -a variable语句来指定一个数组等.要引用一个数组元素(也就是取值), 可以使用大括号, 访问

Linux Shell中‘$‘符号的N种用法

在Shell中$是一个特殊的字符,在不同场景中有不同的用法. 引用变量 使用$直接引用变量,包括循环变量. 123 [email protected]:~# x=1[email protected]:~# echo $x1 双引号"括起来的字符串支持变量插值. 123 [email protected]:~# x=1[email protected]:~# echo "x = $x"x = 1 使用${}作为单词边界. 123 [email protected]:/var/l

Linux定时对日志批量打包Shell脚本及定时任务crontab 详细用法

一.需求背景     因此次项目的生产环境中部署了多套系统,每天会产生大量的日志(数百GB的量),侵占了服务器宝贵的存储资源空间.为了有效缓解服务器存储压力,考虑通过Linux的Shell脚本结合crontab定时每周一对上周7天的日志打包压缩,并删除原被打包的日志文件,以腾出更多可利用的存储资源空间. 对于初次接触Shell脚本的同学,建议先花几个小时时间学习一下Shell.附Shell教程链接如下: http://www.runoob.com/linux/linux-shell-proces

Linux shell中&,&&,|,||的用法

前言 在玩dvwa的命令注入漏洞的时候,遇到了没有预料到的错误,执行 ping 127.0.0.1 & echo "<?php phpinfo(); ?>" > shell.php 发现返回的执行结果如下图 理论上在dvwa的根目录里应该有一个shell.php但是并没有出现 [email protected]:/var/www/html/dvwa# ls about.php docs hackable login.php README.md test.php

shell中的(),{}几种语法用法

查看脚本语法是否有错误:bash -n modify_suffix.sh跟踪执行sh -x modify_suffix.sh aaa 1.${var} 2.$(cmd) 3.()和{} 4.${var:-string},${var:+string},${var:=string},${var:?string} 5.$((exp)) 6.$(var%pattern),$(var%%pattern),$(var#pattern),$(var##pattern) 1.Shell中变量的原形:${var}

shell后台进程 fg bg wait等用法

[[email protected] shelltest]# ./forever2.sh  2014年 10月 15日 星期三 03:36:24 CST 2014年 10月 15日 星期三 03:36:26 CST ^Z    (--注:ctrl+z) [1]+  Stopped                 ./forever2.sh [[email protected] shelltest]# fg %1 ./forever2.sh 2014年 10月 15日 星期三 03:36:32 C

shell的brake和continue的用法

在循环过程中,有时候需要在未达到循环结束条件时强制跳出循环,像大多数编程语言一样,Shell也使用 break 和 continue 来跳出循环. break命令 break命令允许跳出所有循环(终止执行后面的所有循环). 下面的例子中,脚本进入死循环直至用户输入数字大于5.要跳出这个循环,返回到shell提示符下,就要使用break命令. #!/bin/bash while : do echo -n "Input a number between 1 to 5: " read aNu

【转】shell expect spawn、linux expect 用法小记 看着舒服点

使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄.收藏.可是为什么要这么写却不知其然.本文用一个最短的例子说明脚本的原理. 脚本代码如下: ############################################## #!/usr/bin/expect set timeout 30 spawn ssh -l username 192.168.1.1 expect "password:" send "ispass\r&

Shell语法中的test命令用法

test命令用法.功能:检查文件和比较值 1)判断表达式 if test (表达式为真) if test !表达式为假 test 表达式1 –a 表达式2 两个表达式都为真 test 表达式1 –o 表达式2 两个表达式有一个为真 2)判断字符串 test –n 字符串 字符串的长度非零 test –z 字符串 字符串的长度为零 test 字符串1=字符串2 字符串相等 test 字符串1!=字符串2 字符串不等 3)判断整数 test 整数1 –eq 整数2 整数相等 test 整数1 –ge