Mathematical Aspect of Linux Shell Programming – Part IV
2013-09-21 08:50
525 查看
In this post I will be discussing the Scripts from
the Mathematical and Number point
of view. Although I have posted a more complex script (Simple Calculator)
in the previous post, but on a user part it was difficult to understand and hence I thought to make you people learn the other useful side of learning in small packets.
Prior to this article, three article of Shell Scripting Series are published and they are:
Understand
Linux Shell and Basic Shell Scripting – Part I
5
Shell Scripts to Learn Shell Programming – Part II
Sailing
Through The World of Linux BASH Scripting – Part III
Let’s start the further learning process with some new exciting scripts, start withMathematics scripts:
Create a file “Addition.sh” and chmod
755 to the script as described in previous post and run it.
#!/bin/bashecho “Enter the First Number: ” read a
ter the Second Number: ”
read b echo “E
n
x=$(expr "$a" + "$b")
echo $a + $b = $x
Sample Output
[root@tecmint ~]# vi Additions.sh[root@tecmint ~]# chmod 755 Additions.sh[root@tecmint ~]# ./Additions.sh
nter the Second Number: ”
“Enter the First Number: ”
12
“
E13
13 = 2512 +
Download
Additions.sh
#!/bin/bashecho “Enter the First Number: ” read a
ter the Second Number: ”
read b echo “E
n
x=$(($a - $b))
echo $a - $b = $x
Note: Here we replaced the expr and
let the mathematical calculation be performed in shell.
Sample Output
[root@tecmint ~]# vi Substraction.sh[root@tecmint ~]# chmod 755 Substraction.sh[root@tecmint ~]# ./Substraction.sh
r the Second Number: ”
20 “Enter the First Number: ”
13
“Ent
e
- 20 = -71
3
Download
Substraction.sh
So far you would be enjoying a lot, learning scripts in such an easy way, so the next in chronological order is Multiplication.
#!/bin/bashecho “Enter the First Number: ” read a
ter the Second Number: ”
read b echo “E
n
ho "$a * $b = $(expr $a \* $b)"e
c
Note: Yup! Here we didn’t put the value of multiplication in a variable but performed it directly in output statement.
Sample Output
[root@tecmint ~]# vi Multiplication.sh[root@tecmint ~]# chmod 755 Multiplication.sh[root@tecmint ~]# ./Multiplication.sh
the Second Number: ”
11
1“Enter the First Number: ”
11
“Enter
1 * 11 = 121
Download
Multiplication.sh
Right! Next is Division, and again it is a very simple script. Check it Yourself.
#!/bin/bashecho “Enter the First Number: ” read a
ter the Second Number: ”
read b echo “E
n
ho "$a / $b = $(expr $a / $b)"e
c
Sample Output
[root@tecmint ~]# vi Division.sh[root@tecmint ~]# chmod 755 Division.sh[root@tecmint ~]# ./Division.sh
Enter the Second Number: ” “Enter the First Number: ”
12
“
3
3 = 412 /
Download
Division.sh
Fine! What after these basic mathematical operation. Lets write a script that prints table of any number.
#!/bin/bashecho “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
Sample Output
[root@tecmint ~]# vi Table.sh[root@tecmint ~]# chmod 755 Table.sh[root@tecmint ~]# ./Table.sh
ou want to Print Table: ”
29
58
87
116
145
174
20“Enter The Number upto which
y3
232
261
290
Download
Table.sh
We as a child always have carried out calculation to find if the number is odd or even. Won’t it be a good idea to implement it in script.
#!/bin/bashecho "Enter The Number" read n
pr $n % 2)
if [ $nnum=$(e
xum -eq 0 ]
then
ber"
else
echo "is a Oecho "is a Even Nu
mdd Number"
fi
Sample Output
[root@tecmint ~]# vi EvenOdd.sh[root@tecmint ~]# chmod 755 EvenOdd.sh[root@tecmint ~]# ./EvenOdd.sh
NumberEnter The Number
12
is a Even
[root@tecmint ~]# ./EvenOdd.sh
Enter The Number
11
is a Odd Number
Download
EvenOdd.sh
Next is to find the Factorial.
#!/bin/bash echo "Enter The Number" read a
fact=1
]
do
fact=$(exprwhile [ $a -ne
0 $fact \* $a)
a=$(expr $a - 1)
done
factecho
$
Sample Output
[root@tecmint ~]# vi Factorial.sh[root@tecmint ~]# chmod 755 Factorial.sh[root@tecmint ~]# ./Factorial.sh
Enter The Number
12
479001600
You may now relax with a feeling that calculating 12*11*10*9*7*7*6*5*4*3*2*1 would be more difficult than a simple script as produced
above. Think of the situation where you require to find 99! or something like that. Sure! This script will be very much handy in that
situation.
Download
Factorial.sh
Armstrong Number! Ohhh You forget what an Armstrong
Number is. Well an Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is
an Armstrong number since 3**3 + 7**3 + 1**3 = 371.
#!/bin/bash echo "Enter A Number" read n
arm=0
[ $n -netemp=$n
while
0 ]
do
% 10)
arm=$(exprr=$(expr $
n $arm + $r \* $r \* $r)
cho $arm
if [ $arn=$(expr $n / 10)
done
em -eq $temp ]
then
echo "Armstrong"
else
echo "Not Armstrong"
fi
Sample Output
[root@tecmint ~]# vi Armstrong.sh[root@tecmint ~]# chmod 755 Armstrong.sh[root@tecmint ~]# ./Armstrong.sh
gEnter A Number
371
371
Armstro
n
[root@tecmint ~]# ./Armstrong.sh
Enter A Number
123
36
Not Armstrong
Download
Armstrong.sh
The last script is to distinguish whether a number is prime or not.
#!/bin/bash echo “Enter Any Number”read n
i=1
c=1
$n ]
do
i=$(expr $iwhile [ $i -le
+ 1)
r=$(expr $n % $i)
expr $c + 1)
fiif [ $r -eq 0 ]
then
c=$
(
done
if [ $c -eq 2 ]
then
echo “Prime”
else
echo “Not Prime”
fi
Sample Output
[root@tecmint ~]# vi Prime.sh[root@tecmint ~]# chmod 755 Prime.sh[root@tecmint ~]# ./Prime.sh
Prime”“Enter Any Number”
12
“Not
Download
Prime.sh
That’s all for now. In our very next article we will be covering other mathematical programs in the shell Scripting programming language. Don’t forget to mention your views regarding article in the Comment section. Like and share
us and help us spread. Come Visitingtecmint.com for News and
articles relating to FOSS. Till then Stay tuned.
the Mathematical and Number point
of view. Although I have posted a more complex script (Simple Calculator)
in the previous post, but on a user part it was difficult to understand and hence I thought to make you people learn the other useful side of learning in small packets.
Prior to this article, three article of Shell Scripting Series are published and they are:
Understand
Linux Shell and Basic Shell Scripting – Part I
5
Shell Scripts to Learn Shell Programming – Part II
Sailing
Through The World of Linux BASH Scripting – Part III
Let’s start the further learning process with some new exciting scripts, start withMathematics scripts:
Script 1: Additions
Create a file “Addition.sh” and chmod755 to the script as described in previous post and run it.
#!/bin/bashecho “Enter the First Number: ” read a
ter the Second Number: ”
read b echo “E
n
x=$(expr "$a" + "$b")
echo $a + $b = $x
Sample Output
[root@tecmint ~]# vi Additions.sh[root@tecmint ~]# chmod 755 Additions.sh[root@tecmint ~]# ./Additions.sh
nter the Second Number: ”
“Enter the First Number: ”
12
“
E13
13 = 2512 +
Download
Additions.sh
Script 2: Substraction
#!/bin/bashecho “Enter the First Number: ” read ater the Second Number: ”
read b echo “E
n
x=$(($a - $b))
echo $a - $b = $x
Note: Here we replaced the expr and
let the mathematical calculation be performed in shell.
Sample Output
[root@tecmint ~]# vi Substraction.sh[root@tecmint ~]# chmod 755 Substraction.sh[root@tecmint ~]# ./Substraction.sh
r the Second Number: ”
20 “Enter the First Number: ”
13
“Ent
e
- 20 = -71
3
Download
Substraction.sh
Script 3: Multiplication
So far you would be enjoying a lot, learning scripts in such an easy way, so the next in chronological order is Multiplication.#!/bin/bashecho “Enter the First Number: ” read a
ter the Second Number: ”
read b echo “E
n
ho "$a * $b = $(expr $a \* $b)"e
c
Note: Yup! Here we didn’t put the value of multiplication in a variable but performed it directly in output statement.
Sample Output
[root@tecmint ~]# vi Multiplication.sh[root@tecmint ~]# chmod 755 Multiplication.sh[root@tecmint ~]# ./Multiplication.sh
the Second Number: ”
11
1“Enter the First Number: ”
11
“Enter
1 * 11 = 121
Download
Multiplication.sh
Script 4: Division
Right! Next is Division, and again it is a very simple script. Check it Yourself.#!/bin/bashecho “Enter the First Number: ” read a
ter the Second Number: ”
read b echo “E
n
ho "$a / $b = $(expr $a / $b)"e
c
Sample Output
[root@tecmint ~]# vi Division.sh[root@tecmint ~]# chmod 755 Division.sh[root@tecmint ~]# ./Division.sh
Enter the Second Number: ” “Enter the First Number: ”
12
“
3
3 = 412 /
Download
Division.sh
Script 5: Table
Fine! What after these basic mathematical operation. Lets write a script that prints table of any number.#!/bin/bashecho “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
Sample Output
[root@tecmint ~]# vi Table.sh[root@tecmint ~]# chmod 755 Table.sh[root@tecmint ~]# ./Table.sh
ou want to Print Table: ”
29
58
87
116
145
174
20“Enter The Number upto which
y3
232
261
290
Download
Table.sh
Script 6: EvenOdd
We as a child always have carried out calculation to find if the number is odd or even. Won’t it be a good idea to implement it in script.#!/bin/bashecho "Enter The Number" read n
pr $n % 2)
if [ $nnum=$(e
xum -eq 0 ]
then
ber"
else
echo "is a Oecho "is a Even Nu
mdd Number"
fi
Sample Output
[root@tecmint ~]# vi EvenOdd.sh[root@tecmint ~]# chmod 755 EvenOdd.sh[root@tecmint ~]# ./EvenOdd.sh
NumberEnter The Number
12
is a Even
[root@tecmint ~]# ./EvenOdd.sh
Enter The Number
11
is a Odd Number
Download
EvenOdd.sh
Script 7: Factorial
Next is to find the Factorial.#!/bin/bash echo "Enter The Number" read a
fact=1
]
do
fact=$(exprwhile [ $a -ne
0 $fact \* $a)
a=$(expr $a - 1)
done
factecho
$
Sample Output
[root@tecmint ~]# vi Factorial.sh[root@tecmint ~]# chmod 755 Factorial.sh[root@tecmint ~]# ./Factorial.sh
Enter The Number
12
479001600
You may now relax with a feeling that calculating 12*11*10*9*7*7*6*5*4*3*2*1 would be more difficult than a simple script as produced
above. Think of the situation where you require to find 99! or something like that. Sure! This script will be very much handy in that
situation.
Download
Factorial.sh
Script 8: Armstrong
Armstrong Number! Ohhh You forget what an ArmstrongNumber is. Well an Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is
an Armstrong number since 3**3 + 7**3 + 1**3 = 371.
#!/bin/bash echo "Enter A Number" read n
arm=0
[ $n -netemp=$n
while
0 ]
do
% 10)
arm=$(exprr=$(expr $
n $arm + $r \* $r \* $r)
cho $arm
if [ $arn=$(expr $n / 10)
done
em -eq $temp ]
then
echo "Armstrong"
else
echo "Not Armstrong"
fi
Sample Output
[root@tecmint ~]# vi Armstrong.sh[root@tecmint ~]# chmod 755 Armstrong.sh[root@tecmint ~]# ./Armstrong.sh
gEnter A Number
371
371
Armstro
n
[root@tecmint ~]# ./Armstrong.sh
Enter A Number
123
36
Not Armstrong
Download
Armstrong.sh
Script 9: Prime
The last script is to distinguish whether a number is prime or not.#!/bin/bash echo “Enter Any Number”read n
i=1
c=1
$n ]
do
i=$(expr $iwhile [ $i -le
+ 1)
r=$(expr $n % $i)
expr $c + 1)
fiif [ $r -eq 0 ]
then
c=$
(
done
if [ $c -eq 2 ]
then
echo “Prime”
else
echo “Not Prime”
fi
Sample Output
[root@tecmint ~]# vi Prime.sh[root@tecmint ~]# chmod 755 Prime.sh[root@tecmint ~]# ./Prime.sh
Prime”“Enter Any Number”
12
“Not
Download
Prime.sh
That’s all for now. In our very next article we will be covering other mathematical programs in the shell Scripting programming language. Don’t forget to mention your views regarding article in the Comment section. Like and share
us and help us spread. Come Visitingtecmint.com for News and
articles relating to FOSS. Till then Stay tuned.
相关文章推荐
- Compile Java Codes in Linux Shell instead of Ant Script
- BASH Shell change the color of my shell prompt under Linux or UNIX
- Bash/KSH/SH Shell: Find The Length Of a String On a UNIX, Linux, BSD, and OS X
- Requirement-Driven Linux Shell Programming
- Requirement-Driven Linux Shell Programming
- 《Shell Programming in Unix, Linux and OS X, 4th Edition 》 笔记之Passing Arguments
- Requirement-Driven Linux Shell Programming
- 【Linux】Shell脚本“syntax error: unexpected end of file”原因及处理
- 《A Practical Guide to Linux Commands, Editors, and Shell Programming》之命令行解析
- Requirement-Driven Linux Shell Programming
- 5 Shell Scripts for Linux Newbies to Learn Shell Programming – Part II
- SHELL syntax error:unexpected end of file 提示错误; windows下修改linux文件导致linux文件无法使用
- Requirement-Driven Linux Shell Programming
- a kind of programming font on linux -- Inconsolata
- Analysing the ASM code of a simple sample of C programming language at Linux Platform by GCC and GDB
- Echo dependencies of an installed rpm package over Linux bash shell
- 《Shell Programming in Unix, Linux and OS X, 4th Edition 》 笔记之Can I Quote You On That?
- 《Shell Programming in Unix, Linux and OS X, 4th Edition 》 笔记之Decisions,Decisions
- Linux Shell programming
- My life of programming Java & Linux