gpt4 book ai didi

linux - 在 bash 脚本中比较数字

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:04:38 26 4
gpt4 key购买 nike

我写了这个脚本来比较 bash 中的 2 个数字,但它给了我一些数字的错误答案。就像如果我给它 2&2 作为输入,它会给我“X 大于 Y”

#!/bin/bash 
read num1
read num2
if [ $num1 > $num2 ]
then
echo "X is greater than Y"
elif [ $num1 < $num2 ]
then
echo "X is less than Y"
elif [ $num1 = $num2 ]
then
echo "X is equal to Y"
fi

最佳答案

您可以尝试使用 bash 算术上下文:

#!/bin/bash 
read num1
read num2
if (( num1 > num2 ))
then
echo "X is greater than Y"
elif (( num1 < num2 ))
then
echo "X is less than Y"
elif (( num1 == num2 ))
then
echo "X is equal to Y"
fi

关于linux - 在 bash 脚本中比较数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46021194/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com