Welcome, Guest
Username Password: Remember me

Shell script program 2
(1 viewing) (1) Guest
Shell Script
  • Page:
  • 1

TOPIC: Shell script program 2

Shell script program 2 1 year, 5 months ago #408

  • scmuser
  • OFFLINE
  • Gold Boarder
  • scm master
  • Posts: 237
  • Points: 3429
  • Karma: 0
  • Honor Medal 2009
Use a script to take two numbers as arguments and output their sum using (i) bc (ii) expr. include error - checking to test whether two arguments were entered?

Re: Shell script program 2 1 year, 5 months ago #412

  • tpatil
  • OFFLINE
  • Junior Boarder
  • Posts: 27
  • Points: 5107
  • Karma: 0
  • Honor Medal 2009
If I understand correctly your question, You want to pass 2 parameters to shell script and print the sum of those. Also, you want to throw error if the number of parametrs are less than 2.

 
#!/bin/bash

if [ $# -lt 2 ]
then
echo "Usage: $0 arg1 arg2"
exit
fi
 
echo "\$1=$1"
echo "\$2=$2"
 
let add=$1+$2
let sub=$1-$2
 
echo -e "Addition=$add\nSubtraction=$sub\n"
 
 


Hope this helps...

Re: Shell script program 2 1 year, 5 months ago #417

  • renjith
  • OFFLINE
  • Fresh Boarder
  • Posts: 3
  • Points: 15
  • Karma: 0
#!/bin/ksh

while echo " Enter two numbers :\n " ; do
echo "Enter num 1: "; read A;
echo "Enter num 2: "; read B;
if [ -z $A -o -z $B ] ;then
echo " Invalid input , pls enter again"
else
break
fi
done

export A B

echo " ************* MENU *********** \n"
echo " 1. cal using bc ";
echo " 2. cal using expr";
echo " enter you choice :";
read c;

case $c in
1) sum=`echo " $A + $B "|bc` ;;
2) sum=`expr $A + $B` ;;
*) echo "invalid option , run script again"
esac

if [ $? -eq 0 ]
then echo " Sum is $sum"
else
echo " Script failed "
fi
  • Page:
  • 1
Time to create page: 0.86 seconds

     
    
Home Forum