Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Numeric comparison script

Shulou Source: shulou.com Published: 2022-06-03 07:16:57 09月25日 Update

Use the two methods of the if statement to determine the size of two numbers.

The first kind:

If the number of #! / bin/basha=$1b=$2# parameters is not equal to 2, [$#-ne 2] & & {echo "USAGE:$0 NUM1 NUM2" exit 1} # calculate the return value expr $a + 0 & > / dev/nullRETVAL1=$?expr $b + 0 & > / dev/nullRETVAL2=$?# return value must all be 0 $? The return value of 0 from the previous command indicates that test $RETVAL1-eq 0-a $RETVAL2-eq 0 | {echo "Please enter input two" num "again." Exit 2} # compare if [$a-eq $b]; then echo "$a = $b" elif [$a-gt $b]; then echo "$a > $b" elif [$a-lt $b]; then echo "$a

< $b"else exitfi注:脚本在此页面可能有的字符会自动变成中文字符,如果贴出来运行有可能会报错,只能慢慢照着敲。 第二种 #!/bin/bashread -t 5 -p "Please enter input two num:" a b#判断是字符串长度是否等于零,如果等于零则为真[ -z "$a" ] || [ -z "$b" ]&&{ echo "Please enter input two num agagin." exit 1}#判断返回值的结果是否为trunexpr $a + 0 &>

/ dev/nullRETVAL1=$?expr $b + 0 & > / dev/nullRETVAL2=$?# determines whether the returned values are all truntest $RETVAL1-eq 0-a $RETVAL2-eq 0 | {echo "Please enter input two" num "again." Exit 2} # judge the size of if [$a-eq $b]; then echo "$a = $b" elif [$a-gt $b]; then echo "$a > $b" elif [$a-lt $b]; then echo "$a < $b" else exit 3fiexit Note: the possible characters of the script on this page will automatically become Chinese characters, if posted to run may report errors, can only be typed slowly.

Summary:

Learning scripts can only be practiced more, in order to slowly form a script thinking, otherwise you will never be able to write your own script.

Tags: Script character result size according to page Chinese will report run number success two number parameter command string thought situation method statement Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information NVidia Microsoft MariaDB