Showing posts with label numbers. Show all posts
Showing posts with label numbers. Show all posts

20 January 2012

Shell Script to add two numbers by the use of Function

13 comments

Q. How do I write Bash Script to add two numbers using function


Ans:

#!/bin/bash
#function to add two numbers
add()
{
x=$1
y=$2
echo -e "Number entered by u are: $x and $y"
echo "sum of $1 and $2 is `expr $x + $y` "
}
# main script
echo "enter first number"
read first
echo "enter second number"
read sec
#calling function
add $first $sec
echo "end of the script"
Read More...

16 January 2012

Shell Script to Display numbers Using Array

2 comments

Q. How to use array in Bash for taking input and displaying it


Ans:

#!/bin/bash
for((i=0;i<5;i++))
do
echo "enter `expr $i + 1` number"
read arr[$i]
done
echo "the numbers you hav entered are"
for((i=0;i<5;i++))
do
echo ${arr[$i]}
done
Read More...