• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >linux shell > Shell脚本if else语句小结

Shell脚本if else语句小结

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了shell脚本if语句,shell脚本if语句嵌套,shell脚本if,shell脚本中if,linux shell脚本 if等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

和Java、PHP等语言不一样,sh的流程控制不可为空,如:

<?php
if (isset($_GET["q"])) {
    search(q);
}
else {
    //do nothing
}
?>
</div>

在sh/bash里可不能这么写,如果else分支没有语句执行,就不要写这个else,就像这样:
if condition
then
    command1
    command2
    ...
    commandN
fi
</div>

当然,也可以写成一行(适用于终端命令提示符),像这样:

if test $[2*3] -eq $[1+5]; then echo 'The two numbers are equal!'; fi;
</div>

末尾的fi就是if倒过来拼写,后面还会遇到类似的。

if else格式

if condition
then
    command1
    command2
    ...
    commandN
else
    command
fi

</div>

if else-if else格式

if condition1
then
    command1
elif condition2
    command2
else
    commandN
fi

</div>

if else语句经常与test命令结合使用,如下所示:
num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
    echo 'The two numbers are equal!'
else
    echo 'The two numbers are not equal!'
fi
</div>

输出:
The two numbers are equal!

</div>

您可能想查找下面的文章:

  • shell脚本编程之循环语句
  • Shell脚本编程之判断语句
  • Shell脚本IF条件判断和判断条件总结
  • Shell脚本case语句简明教程
  • Shell脚本if else语句小结
  • shell脚本中case条件控制语句的一个bug分析
  • shell脚本编程之for语句、if语句使用介绍

相关文章

  • Linux下查看.so和可执行文件是否debug编译的方法(必看)
  • Shell根据日期、月份、星期判断年份的代码
  • linux下定时执行任务的方法及crontab 用法说明(收集整理)
  • Shell脚本中判断输入参数个数的方法
  • Ubuntu服务器配置apache2.4的限速功能shell脚本分享
  • Shell中判断字符串是否为数字的6种方法分享
  • Shell时间运算及时间差计算方法
  • 编写你的shell其实很简单
  • 一天一个shell命令 linux文本操作系列-head,tail命令详解
  • Linux oracle数据库自动备份自动压缩脚本代码

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Shell中比较大小简洁总结
    • shell脚本学习指南[一](Arnold Robbins & Nelson H.F. Beebe著)
    • 阿里云主机一键安装lamp、lnmp环境的shell脚本分享
    • Cygwin下安装vim后,vim中退格键无法正常使用的解决方法
    • linux Shell脚本里面把一个数组传递到awk内部进行处理
    • Shell中函数返回值超出问题
    • linux使用管道命令执行ps获取cpu与内存占用率
    • 从apnic提取ip信息脚本分享
    • linux rsync同步命令(值得收藏)
    • Shell实现系统时间和BIOS时间同步校准脚本分享

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有