A common mathematical operation instance in shell script programming _linux Shell

Source: Internet
Author: User
Tags chmod

This section mainly discusses the mathematical related shell script programming.

Addition operations

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

Copy Code code as follows:
#!/bin/bash
echo "Enter the" number: "
Read a
echo "Enter The Second Number:"
Read B
x=$ (expr "$a" + "$b")
echo $a + $b = $x

Output results:
Copy Code code as follows:

[Root@tecmint ~]# VI additions.sh
[Root@tecmint ~]# chmod 755 additions.sh
[Root@tecmint ~]#./additions.sh

"Enter the": "
12
"Enter The Second Number:"
13
12 + 13 = 25

Subtraction operations

Copy Code code as follows:

#!/bin/bash
echo "Enter the" number: "
Read a
echo "Enter The Second Number:"
Read B
x=$ (($a-$b))
echo $a-$b = $x

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

Output results:

Copy Code code as follows:

[Root@tecmint ~]# VI substraction.sh
[Root@tecmint ~]# chmod 755 substraction.sh
[Root@tecmint ~]#./substraction.sh

"Enter the": "
13
"Enter The Second Number:"
20
13-20 =-7

Multiplication operations

Copy Code code as follows:

#!/bin/bash
echo "Enter the" number: "
Read a
echo "Enter The Second Number:"
Read B
echo "$a * $b = $ (expr $a \* $b)"

Output results:
Copy Code code as follows:

[Root@tecmint ~]# VI multiplication.sh
[Root@tecmint ~]# chmod 755 multiplication.sh
[Root@tecmint ~]#./multiplication.sh

"Enter the": "
11
"Enter The Second Number:"
11
11 * 11 = 12

Division operations

Copy Code code as follows:

#!/bin/bash
echo "Enter the" number: "
Read a
echo "Enter The Second Number:"
Read B
echo "$a/$b = $ (expr $a/$b)"

Output results:
Copy Code code as follows:

[Root@tecmint ~]# VI division.sh
[Root@tecmint ~]# chmod 755 division.sh
[Root@tecmint ~]#./division.sh

"Enter the": "
12
"Enter The Second Number:"
3
12/3 = 4

Array

The following script can print a set of numbers.

Copy Code code as follows:

#!/bin/bash
echo "Enter the number upto which you want to Print Table:"
Read n
I=1
While [$i-ne 10]
Todo
i=$ (expr $i + 1)
table=$ (expr $i \* $n)
Echo $table
Done

Output results:
Copy Code code 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

Judging odd and even

Copy Code code 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 results:
Copy Code code 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

Number of factorial

Copy Code code as follows:

#!/bin/bash
echo "Enter the number"
Read a
Fact=1
While [$a-ne 0]
Todo
fact=$ (expr $fact \* $a)
a=$ (expr $a-1)
Done
Echo $fact

Output results:
Copy Code code 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 from here

Judge the number of Armstrong

Armstrong: In three-bit positive integers, such as ABC, there are some that may satisfy (a^3) + (b^3) + (c^3) =abc, that is, the cubic of the individual digits and exactly the number itself. These numbers are called Armstrong numbers.

Copy Code code as follows:

#!/bin/bash
echo "Enter A number"
Read n
Arm=0
temp= $n
While [$n-ne 0]
Todo
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 results:
Copy Code code 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

Judging prime numbers

Copy Code code as follows:

#!/bin/bash
echo "Enter any number"
Read n
I=1
C=1
While [$i-le $n]
Todo
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 results:
Copy Code code as follows:

[Root@tecmint ~]# VI prime.sh
[Root@tecmint ~]# chmod 755 prime.sh
[Root@tecmint ~]#./prime.sh

"Enter any number"
12

"Not Prime"

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.