比较bash脚本中的数字 [英] Comparing numbers in bash scripting
本文介绍了比较bash脚本中的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了这个脚本来比较bash中的2个数字,但是对于某些数字却给出了错误的答案.就像如果我给它2& 2作为输入,它给我"X大于Y"
I wrote this script to compare 2 numbers in bash but it gives me wrong answers for some numbers. like if I give it 2&2 for input , it gives me "X is greater than 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算术上下文:
You can try with bash arithmetic contexts:
#!/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脚本中的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文