Get the App
SLTechnology News&Howtos  ›  Development  › 

A tutorial of Mathematical Operation methods commonly used in Shell script programming

Shulou Source: shulou.com Published: 2022-06-03 08:19:44 10月02日 Update

This article mainly introduces "Shell script programming commonly used mathematical operation method tutorial", in daily operation, I believe many people in Shell script programming commonly used mathematical operation method tutorial problems have doubts, small make up consult all kinds of information, sort out simple and easy to use operation method, hope to answer "Shell script programming commonly used mathematical operation method tutorial" doubts helpful! Next, please follow the small series to learn together!

addition operation

Create a new file "Addition.sh", enter the following and give it executable permissions.

The code is as follows:

#!/ bin/bash

echo "Enter the First Number: "

read a

echo "Enter the Second Number: "

read b

x=$(expr "$a" + "$b")

echo $a + $b = $x

Output:

The code is as follows:

[root@tecmint ~]# vi Additions.sh

[root@tecmint ~]# chmod 755 Additions.sh

[root@tecmint ~]# ./ Additions.sh

"Enter the First Number: "

12

"Enter the Second Number: "

13

12 + 13 = 25

subtraction operation

The code is as follows:

#!/ bin/bash

echo "Enter the First Number: "

read a

echo "Enter the Second Number: "

read b

x=$(($a - $b))

echo $a - $b = $x

Note: here we do not use "expr" to perform mathematical operations as in the example above.

Output:

The code is as follows:

[root@tecmint ~]# vi Substraction.sh

[root@tecmint ~]# chmod 755 Substraction.sh

[root@tecmint ~]# ./ Substraction.sh

"Enter the First Number: "

13

"Enter the Second Number: "

20

13 - 20 = -7

multiplication operation

The code is as follows:

#!/ bin/bash

echo "Enter the First Number: "

read a

echo "Enter the Second Number: "

read b

echo "$a * $b = $(expr $a \* $b)"

Output:

The code is as follows:

[root@tecmint ~]# vi Multiplication.sh

[root@tecmint ~]# chmod 755 Multiplication.sh

[root@tecmint ~]# ./ Multiplication.sh

"Enter the First Number: "

11

"Enter the Second Number: "

11

11 * 11 = 12

division operation

The code is as follows:

#!/ bin/bash

echo "Enter the First Number: "

read a

echo "Enter the Second Number: "

read b

echo "$a / $b = $(expr $a / $b)"

Output:

generation

[root@tecmint ~]# vi Division.sh

[root@tecmint ~]# chmod 755 Division.sh

[root@tecmint ~]# ./ Division.sh

"Enter the First Number: "

12

"Enter the Second Number: "

3

12 / 3 = 4

array

The script below prints a set of numbers.

The code is as follows:

#!/ bin/bash

echo "Enter The Number upto which you want to Print Table: "

read n

i=1

while [ $i -ne 10 ]

do

i=$(expr $i + 1)

table=$(expr $i \* $n)

echo $table

done

Output:

The code is as follows:

[root@tecmint ~]# vi Table.sh

[root@tecmint ~]# chmod 755 Table.sh

[root@tecmint ~]# ./ Table.sh

"Enter The Number upto which you want to Print Table: "

29

58

87

116

145

174

203

232

261

290

You can download the code for this example from here

judgement parity

The code is as follows:

#!/ bin/bash

echo "Enter The Number"

read n

num=$(expr $n % 2)

if [ $num -eq 0 ]

then

echo "is a Even Number"

else

echo "is a Odd Number"

fi

Output:

The code is as follows:

[root@tecmint ~]# vi EvenOdd.sh

[root@tecmint ~]# chmod 755 EvenOdd.sh

[root@tecmint ~]# ./ EvenOdd.sh

Enter The Number

12

is a Even Number

1

2

3

4

5

[root@tecmint ~]# ./ EvenOdd.sh

Enter The Number

11

is a Odd Number

Factorial number

The code is as follows:

#!/ bin/bash

echo "Enter The Number"

read a

fact=1

while [ $a -ne 0 ]

do

fact=$(expr $fact \* $a)

a=$(expr $a - 1)

done

echo $fact

Output:

The code is as follows:

[root@tecmint ~]# vi Factorial.sh

[root@tecmint ~]# chmod 755 Factorial.sh

[root@tecmint ~]# ./ Factorial.sh

Enter The Number

12

479001600

You can download the code for this example here

Judge Armstrong number

Armstrong numbers: Among three-digit positive integers, such as abc, there are some that satisfy (a^3)+(b^3)+(c^3)=abc, i.e., the cube sum of the digits is exactly the number itself. These numbers are called Armstrong numbers.

The code is as follows:

#!/ bin/bash

echo "Enter A Number"

read n

arm=0

temp=$n

while [ $n -ne 0 ]

do

r=$(expr $n % 10)

arm=$(expr $arm + $r \* $r \* $r)

n=$(expr $n / 10)

done

echo $arm

if [ $arm -eq $temp ]

then

echo "Armstrong"

else

echo "Not Armstrong"

fi

Output:

The code is as follows:

[root@tecmint ~]# vi Armstrong.sh

[root@tecmint ~]# chmod 755 Armstrong.sh

[root@tecmint ~]# ./ Armstrong.sh

Enter A Number

371

371

Armstrong

1

2

3

4

5

6

[root@tecmint ~]# ./ Armstrong.sh

Enter A Number

123

36

Not Armstrong

prime number judgment

The code is as follows:

#!/ bin/bash

echo "Enter Any Number"

read n

i=1

c=1

while [ $i -le $n ]

do

i=$(expr $i + 1)

r=$(expr $n % $i)

if [ $r -eq 0 ]

then

c=$(expr $c + 1)

fi

done

if [ $c -eq 2 ]

then

echo "Prime"

else

echo "Not Prime"

fi

Output:

The code is as follows:

[root@tecmint ~]# vi Prime.sh

[root@tecmint ~]# chmod 755 Prime.sh

[root@tecmint ~]# ./ Prime.sh

"Enter Any Number"

12

"Not Prime"

At this point, on the "Shell script programming commonly used mathematical operation method tutorial" learning is over, I hope to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Tags: Code operations results output math methods scripts commonly used tutorials scripting programming learning examples face-to-face more help practical and then individual digits Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Linux MySQL Apple Huawei