Shell Basics: Input 2 integers using read, command-line script arguments, and calculate

Source: Internet
Author: User
Tags integer numbers



Shell basic exercises: using the read interactive input, command-line script 2 ways to implement input 2 integer numbers, and calculate subtraction. The basic knowledge of shell includes: variable definition, read, if Judgment statement, regular expression and so on.






First way: Read Interactive input parameters



The idea is: to determine whether the 2nd variable entered is empty, the blank is prompted to enter 2 numbers, not NULL to determine whether the input is an integer, with expr, the effect is to let 2 variables to add, if the result is 0 Description Input 2 is a number, if the result is not 0 input non-integer, prompt input is not a non-integer;


#!/bin/bash
read -p "pls input two number:" a b
if  [ ! -z $b ]
then
     expr $a + $b &>/dev/null
     if [ $? -eq 0 ]
      then
          echo "a-b=$(($a-$b))"
          echo "a+b=$(($a+$b))"
          echo "a*b=$(($a*$b))"
          echo "a/b=$(($a/$b))"
         else
              echo "input is not zhengshu"
         fi
else
     echo "you must input two number"
fi


The results are as follows: The input letter will be error, input is not an integer, input only 1 parameters will be error;



[baby@localhost ~]$ sh a.sh
pls input two number:4 2
a-b=2
a+b=6
a*b=8
a/b=2
[baby@localhost ~]$ sh a.sh
pls input two number:a 2
input is not zhengshu
[baby@localhost ~]$ sh a.sh
pls input two number:10
you must input two number

The script has a bug, if you enter 3 parameters, you will get an error as follows:



[Email protected] ~]$ sh a.sh



Pls input 3 3 number:1



A.sh:line 3: [: 3:binary operator expected



You must input number









For the above script bug, modify the following:



The idea is: add a variable C, and more if judgment, first determine whether a $ A is empty, such as null prompt input 2 number and exit, and then determine whether $b is empty, such as NOT NULL prompt input 2 number and exit, only a $ A $b is not empty that has the input value, and then determine whether the $c is empty, If you do not empty the following integer judgment, such as $c not null prompt input 2 numbers and exit;



#!/bin/bash
read -p "pls input two number:" a b c
if [ -z $a ]
  then
 echo "you must input two number"
 exit
elif [ -z $b ]
  then
 echo "you must input two number"
 exit
fi
if [ -z $c ]
then
 expr $a + $b &>/dev/null
 if [ $? -eq 0 ]
  then
  echo "a-b=$(($a-$b))"
  echo "a+b=$(($a+$b))"
  echo "a*b=$(($a*$b))"
  echo "a/b=$(($a/$b))"
 else 
  echo "input is not zhengshu"
 fi
else
 echo "you must input two number"
fi





The execution result is as follows, nothing is entered, input a character will prompt must enter 2 numbers, enter 2 values have the letter prompt input non-integer;



[baby@localhost ~]$ sh a.sh  
pls input two number:
you must input two number
[baby@localhost ~]$ sh a.sh
pls input two number:1
you must input two number
[baby@localhost ~]$ sh a.sh
pls input two number:1 a
input is not zhengshu
[baby@localhost ~]$ sh a.sh
pls input two number:2 1
a-b=1
a+b=3
a*b=2
a/b=2





The second way: command-line script-pass mode



The idea is: Define a b Two variables, accept the command line passed parameters, $ #为输入参数的总个数, the input parameter number is not equal to 2, the prompt must enter 2 numbers, equals 2 words to execute the following script;





[baby@localhost ~]$ cat b.sh
#!/bin/bash
a=$1
b=$2
if [ $# -ne 2 ]
then
 echo "you must input two number"
 exit 1
else
 expr $a + $b &>/dev/null
 if [ $? -eq 0 ]
  then
  echo "a-b=$(($a-$b))"
  echo "a+b=$(($a+$b))"
  echo "a*b=$(($a*$b))"
  echo "a/b=$(($a/$b))"
 else
  echo "input is not zhengshu"
  exit 1
 fi
fi




The result of the execution is as follows: null parameter, 3 will prompt must enter 2 numbers, the argument contains non-numeric input is not an integer;



[baby@localhost ~]$ sh b.sh 3 a
input is not zhengshu
[baby@localhost ~]$ sh b.sh 3 2 3
you must input two number
[baby@localhost ~]$ sh b.sh 3 2
a-b=1
a+b=5
a*b=6
a/b=1


Summarize:



Read can interact with the user better, the script is more complex and if the judgment is not efficient;



command-line arguments use expressions to determine input parameters, and perform efficiently and with better comprehension.






This article is from the "Model Student's Learning blog" blog, please be sure to keep this source http://mofansheng.blog.51cto.com/8792265/1702758



Shell Basics: Input 2 integers using read, command-line script arguments, and calculate


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