Digital comparison size Script

Source: Internet
Author: User

Use the IF statement to determine the size of the two numbers in two ways.


The first type:

The number of #!/bin/basha=$1b=$2# parameters is not equal to 2 of the case [ $# -ne 2 ]&&{         echo  "Usage:$0 num1 num2"         exit &NBSP;1} #计算返回值expr   $a  + 0 &>/dev/nullRETVAL1=$?expr  $b  + 0  &>/dev/nullretval2=$? #返回值的结果必须都为0   $?  The return value of the previous command  0 indicates a successful test  $RETVAL 1 -eq  0 -a  $RETVAL 2 -eq 0| | {        echo  "please enter input two " num "  again. " &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;EXIT&NBSP;2} #比较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 Note: The script may have characters on this page will automatically become Chinese characters, if posted out of the operation may be error, only slowly hit.


The second Kind

#!/bin/bashread -t 5 -p  "Please enter input two num:"  a b# The judge is whether the string length is equal to zero, and if equal to zero is true [ -z  "$a"  ] | |  [ -z  "$b"  ]&&{        echo  "please  enter input two num agagin. " &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;EXIT&NBSP;1} #判断返回值的结果是否为trunexpr   $a  + 0  &>/dev/nullRETVAL1=$?expr  $b  + 0 &>/dev/nullretval2=$? #判断返回值的结果是否都是truntest   $RETVAL 1 -eq 0 -a  $RETVAL 2 -eq 0| | {        echo  "please enter input two " num "  again. " &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;EXIT&NBSP;2} #判断大小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 script may have characters on this page will automatically become Chinese characters, if posted out of the operation may be error, only slowly hit.

Summarize:

Learning scripts can only be practiced more slowly to form a script thinking, or will never write their own script.

This article is from the "sub-non-fish" blog, please be sure to keep this source http://5323197.blog.51cto.com/5313197/1788053

Digital comparison size Script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.