Linux-shell the size of the judgment

Source: Internet
Author: User

Combat 1: Enter 2 integers to determine the size

Three ways to achieve:

1 defining a variable 2 script parameter 3 read read-in

The first step, give the user prompt, let it enter an integer

The second step is to determine that the first value and the second value are not empty

The third step is to determine the two number as an integer

Fourth, the first value is compared to the second value

 [[email protected] scripts]# cat #!/bin/bashcat <<eof   1-10...   zhengshuEOFread -p  "Panduan daxiao:"  a b[ $ {#a} -eq 0 ]&&{      echo  "Diyigecanshu"        exit 1}[ ${#b} -eq 0 ]&&{       echo  "Diergecanshu"       exit 1}expr  $a  + 1  &>/dev/nullRE_A=$?expr  $b  + 1 &>/dev/nullRE_B=$?if [  $RE _a  -ne 0 -o  $RE _b -ne 0 ]  then       echo  "One of you input is not int"       exit  1fi if [  $a  -eq  $b  ];       then       echo  "$a  =  $b"   elif [  $a  -gt  $b  ]       then      echo  "$a  >  $b"   else       echo  "$a  <  $b" fi [[email protected]  scripts]# sh   [[email  Protected] scripts]# sh   1-10...   zhengshupanduan  daxiao:e eone of you input is not int[[email protected]  scripts]# sh   1-10...   zhengshupanduan daxiao:2  342 < 34[[email protected] scripts]# sh   1-10 ...    zhengshupanduan daxiao:3 33 = 3[[email protected] scripts]#

Implementation of script-pass method

I'm here to make a change to the above. into Vim.

%s# $a #$1#g%s# $b #$2#g when you delete a few lines, you implement the way the script is passed

 [[email protected] scripts]# cat #!/bin/bash[ ${#1} -eq  0 ]&&{      echo  "Diyigecanshu"        exit 1}[ ${#2} -eq 0 ]&&{      echo   "Diergecanshu"       exit 1}expr $1 + 1 &>/ dev/nullre_a=$?expr $2 + 1 &>/dev/nullre_b=$?if [  $RE _a -ne 0  -o  $RE _b -ne 0 ]  then      echo  "one  of you input is not int "      exit 1fi  if [ $1 -eq $2 ];       then       echo  "$1 = $2"   elif [ $1 -gt $2 ]       then      echo  "$1 > $2"   else       echo  "$1 < $2" Fi [[email protected] scripts]# sh 3 done of you input is not int[[email protected]  scripts]# sh 3 43 < 4[[email protected] scripts]#  sh 3 33 = 3[[email protected] scripts]#

How variables are defined

Here I replace the following original variables with the following command to test

 [[email protected] scripts]# sh 3 < 4[[email  protected] scripts]# sed -i  "S#3#4#g" [[email protected]  Scripts]# sh 4 = 4[[email protected] scripts]# sed -i   "S#4#dd#g" [[email protected] scripts]# sh one  of you input is not int[[email protected] scripts]# cat #!/bin/basha=ddb=dd[ ${#a} -eq 0 ]&&{       echo  "Diyigecanshu"       exit 1}[ ${#b} -eq 0  ]&&{      echo  "Diergecanshu"        exit 1}expr  $a  + 1 &>/dev/nullRE_A=$?expr  $b  + 1  &>/dev/nullre_b=$?if [&nBSP; $RE _a -ne 0 -o  $RE _b -ne 0 ]  then       echo  "One of you input is not int"        exit 1fiif [  $a  -eq  $b  ];        then      echo  "$a  =  $b"   elif [  $a  -gt   $b  ]      then      echo  $a   >  $b   else      echo  $a  <  $b "fi [[ email protected] scripts]#

This article from the "Clear Sky" blog, declined reprint!

Linux-shell the size of the judgment

Related Article

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: 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.