Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Numeric comparison script

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report