Bash Commands - Arithmetic Tests using (( ... ))
来源:互联网 发布:前端面试题js继承 编辑:程序博客网 时间:2024/05/19 11:45
The (( )) construct expands and evaluates an arithmetic expression. If the expression evaluates as zero, it
returns an exit status of 1, or "false". A non-zero expression returns an exit status of 0, or "true". This is in
marked contrast to using the test and [ ] constructs previously discussed.
#!/bin/bash
# arith-tests.sh# Arithmetic tests.
# The (( ... )) construct evaluates and tests numerical expressions.
# Exit status opposite from [ ... ] construct!
echo "Exit status of \"(( 0 ))\" is $?."
echo "Exit status of \"(( 1 ))\" is $?."
echo "Exit status of \"(( 5 > 4 ))\" is $?." # 0
echo "Exit status of \"(( 5 > 9 ))\" is $?." # 1
echo "Exit status of \"(( 5 == 5 ))\" is $?." # 0
(( 5 - 5 )) # 0
echo "Exit status of \"(( 5 - 5 ))\" is $?." # 1
echo "Exit status of \"(( 5 / 4 ))\" is $?." # 0
echo "Exit status of \"(( 1 / 2 ))\" is $?." # Rounded off to 0.
# 1
(( 1 / 0 )) 2>/dev/null # Illegal division by 0.
#
^^^^^^^^^^^
echo "Exit status of \"(( 1 / 0 ))\" is $?."
# 1
# What effect does the "2>/dev/null" have?
# What would happen if it were removed?
# Try removing it, then rerunning the script.
# ======================================= #
# (( ... )) also useful in an if-then test.
var1=5
var2=4
if (( var1 > var2 ))
then #Note: Not $var1, $var2. Why?
echo "$var1 is greater than $var2"
fi # 5 is greater than 4
exit 0
- Bash Commands - Arithmetic Tests using (( ... ))
- Linux Commands for Beginners--Arithmetic in Bash--expr,let commands
- Using Multi-Threaded Tests
- Bash Commands - Using the [[ ... ]] rather than [ ... ] can prevent many logic errors in scripts.
- Bash Commands - Special Charactors # ? $
- Bash Commands - $ # Variable Substitution
- Bash Commands - Comparison Operators
- Using Arithmetic Operators
- Using Arithmetic Operations
- Using WinDbg - Advanced commands
- Using SVN Commands
- Using TestContext in Unit Tests
- Bash Commands - tr cat tac
- Bash Commands - chmod # Invoking script
- Bash Commands - # Integer or string?
- Bash Commands - File test operators
- running bash commands in python
- Learn BASH -- Grouping Commands(from bash manual)
- 八款开源 Android 游戏引擎 (巨好的资源)
- Qsys学习笔记3
- PV操作
- asp.net 在线人数统计,页面访问量 Application ,session
- Bugzilla与Teslink安装与整合(Windows)
- Bash Commands - Arithmetic Tests using (( ... ))
- linux下两个有用的用户登录日志文件
- opent --基本核心函数 ---2 内部模型访问,分布,进程,事件,仿真,鉴定,拓扑函数集
- android 进程与线程 - 开发文档翻译 - 进程
- Windows 下Eclipse C
- flex 资料
- UVa 10148 - Advertisement
- jquery 或者说javascript中的加法运算
- hadoop 中添加和删除 datanode和tasktracker(一)