MST
星途 面试题库

面试题:Bash脚本中的条件判断

编写一个Bash脚本,接受两个数字作为参数,使用条件判断语句比较这两个数字的大小,并输出较大的数字。如果两个数字相等,输出'两个数字相等'。
17.1万 热度难度
编程语言Bash

知识考点

AI 面试

面试题答案

一键面试
#!/bin/bash

if [ $# -ne 2 ]; then
    echo "请提供两个数字作为参数"
    exit 1
fi

num1=$1
num2=$2

if [ $num1 -gt $num2 ]; then
    echo $num1
elif [ $num1 -lt $num2 ]; then
    echo $num2
else
    echo "两个数字相等"
fi