Shell basics: Use read and command line scripts to input two integers and calculate the parameters
Shell basic exercise questions: Use read interactive input and command line script to PASS Parameters in two ways to input two integer numbers and calculate addition, subtraction, multiplication, division, and so on. Evaluate the knowledge of shell, including variable definition, read, if judgment statement, and regular expression. The first method is to read the input parameters: determines whether the input 2nd variables are null. If it is null, two numbers are prompted. If it is not null, the system determines whether the input is an integer and expr is used, this function is used to add two variables. If the result is 0, 2 are numbers. If the result is not 0, the input is not an integer, indicating that the input is not a non-integer;
#!/bin/bashread -p "pls input two number:" a bif [ ! -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" fielse echo "you must input two number"fi
The execution result is as follows: an error is returned when you enter a letter instead of an integer. If you enter only one parameter, an error is returned;
[baby@localhost ~]$ sh a.shpls input two number:4 2a-b=2a+b=6a*b=8a/b=2[baby@localhost ~]$ sh a.shpls input two number:a 2input is not zhengshu[baby@localhost ~]$ sh a.shpls input two number:10you must input two number
The script has a bug. If you enter three parameters, the following error will be reported: [baby @ localhost ~] $ Sh. shpls input two number: 1 3 3a. sh: line 3: [: 3: binary operator expectedyou must input two number. Modify the preceding script bug as follows: the idea is to add another variable c and determine if, first, judge whether $ a is empty. If it is empty, enter two numbers and exit. Then, judge whether $ B is empty. If it is not empty, enter two numbers and exit; if $ a $ B is not empty, all of them have input values, and then judge whether $ c is empty. If not, execute the following integer judgment, if $ c is not blank, the system prompts you to enter two numbers and exit;
#!/bin/bashread -p "pls input two number:" a b cif [ -z $a ] then echo "you must input two number" exitelif [ -z $b ] then echo "you must input two number" exitfiif [ -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" fielse echo "you must input two number"fi
The execution result is as follows. If you enter only one character, two numbers are displayed. If you enter two values, a non-integer is displayed;
[baby@localhost ~]$ sh a.sh pls input two number:you must input two number[baby@localhost ~]$ sh a.shpls input two number:1you must input two number[baby@localhost ~]$ sh a.shpls input two number:1 ainput is not zhengshu[baby@localhost ~]$ sh a.shpls input two number:2 1a-b=1a+b=3a*b=2a/b=2
Method 2: PASS Parameters through the command line script. The train of thought is to define two variables a and B and accept the parameters passed by the command line. $ # indicates the total number of input parameters; if the number of input parameters is not equal to 2, the system prompts that two numbers must be entered. If the number is equal to 2, the following script is executed;
[baby@localhost ~]$ cat b.sh#!/bin/basha=$1b=$2if [ $# -ne 2 ]then echo "you must input two number" exit 1else 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 fifi
The execution result is as follows: if the parameter is null and the parameter is 3, two numbers are required. If the parameter is not a number, the system prompts that the input is not an integer;
[baby@localhost ~]$ sh b.sh 3 ainput is not zhengshu[baby@localhost ~]$ sh b.sh 3 2 3you must input two number[baby@localhost ~]$ sh b.sh 3 2a-b=1a+b=5a*b=6a/b=1
Conclusion: read can interact well with users, and the script is more complicated. if judgment efficiency is not high. command line parameters are passed using expressions to judge input parameters, which improves execution efficiency and comprehension;