Linux Shell教程(一) 互动版

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

if else语句与test结合使用

if ... else 语句也经常与 test 命令结合使用。

范例1

#!/bin/bash
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

输出:

The two numbers are equal!

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

编写并运行范例脚本。