博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【shell 脚本】算术测试需要使用(( ))
阅读量:7103 次
发布时间:2019-06-28

本文共 1077 字,大约阅读时间需要 3 分钟。

#!/bin/bash
  # 算术测试.
  # (( ... ))结构可以用来计算并测试算术表达式的结果.
  # 退出状态将会与[ ... ]结构完全相反!
  (( 0 ))
  echo "Exit status of \"(( 0 ))\" is $?."         # 1
  (( 1 ))
  echo "Exit status of \"(( 1 ))\" is $?."         # 0
  (( 5 > 4 ))                                      # 真
  echo "Exit status of \"(( 5 > 4 ))\" is $?."     # 0
  (( 5 > 9 ))                                      # 假
  echo "Exit status of \"(( 5 > 9 ))\" is $?."     # 1
  (( 5 - 5 ))                                      # 0
  echo "Exit status of \"(( 5 - 5 ))\" is $?."     # 1
  (( 5 / 4 ))                                      # 除法也可以.
  echo "Exit status of \"(( 5 / 4 ))\" is $?."     # 0
  (( 1 / 2 ))                                      # 除法的计算结果 < 1.
  echo "Exit status of \"(( 1 / 2 ))\" is $?."     # 截取之后的结果为 0.
                                                   # 1
  (( 1 / 0 )) 2>/dev/null                          # 除数为0, 非法计算.
 #           ^^^^^^^^^^^
  echo "Exit status of \"(( 1 / 0 ))\" is $?."     # 1
  # "2>/dev/null"起了什么作用?
  # 如果这句被删除会怎样?
  # 尝试删除这句, 然后在运行这个脚本.
 exit 0
======================
root@client.example.com ~/yang # ./calucate.sh
Exit status of "(( 0 ))" is 1.
Exit status of "(( 1 ))" is 0.
Exit status of "(( 5 > 4 ))" is 0.
Exit status of "(( 5 > 9 ))" is 1.
Exit status of "(( 5 - 5 ))" is 1.
Exit status of "(( 5 / 4 ))" is 0.
Exit status of "(( 1 / 2 ))" is 1.
Exit status of "(( 1 / 0 ))" is 1.
root@client.example.com ~/yang # 

转载地址:http://lguhl.baihongyu.com/

你可能感兴趣的文章
文档查看cat_more_less_head_tail
查看>>
jdk8重新认识hashmap
查看>>
Spring Cloud Alibaba迁移指南(二):零代码替换 Eureka
查看>>
Visual Paradigm 教程[UML]:如何绘制封装图?(下)
查看>>
初探AngularJS6.x---目录结构说明
查看>>
kafka解决了什么问题?
查看>>
android流式布局、待办事项应用、贝塞尔曲线、MVP+Rxjava+Retrofit、艺术图片应用等源码...
查看>>
ppwjs之bootstrap文字排版:<pre>元素 [scroll](预格式元素 [带滚动条)
查看>>
Spring经典的面试题,你值得拥有!
查看>>
Ember.js 属性值模糊查询
查看>>
squid配置
查看>>
OSChina 周三乱弹 —— 生活要懂得苦中作乐
查看>>
前端那些事之react--redux篇
查看>>
Ubuntu 16.04 U盘安装过程
查看>>
UIApplication、AppDelegate、委托
查看>>
hadoop单机安装
查看>>
Android实用笔记——使用GridView以表格的形式显示多张图片
查看>>
内部类使用外部类的成员属性
查看>>
基于const的重载
查看>>
虹软AI 人脸识别SDK接入 — 性能优化篇(多线程)
查看>>