Friday 26 October 2012

cmd (print n words, sum of digit)

if [ $# -ne 3 ]
then
echo " Insuffient command line arguments "
else
choice=1
echo "1) Print the word N times"
echo "2) Sum of digit no."
echo "3) Exit"
echo "Enter your choice : "
read choice
case $choice in
1)
count=$1
while [ $count -gt 0 ]
do
 echo "$2"
    count=`expr $count - 1`
done
;;
2)
num=$3
sum=0
while [ $num -gt 0 ]
do
    r=$(($num%10))
    sum=$(($sum+$r))
    num=$(($num/10))
done
    echo "Sum of digit of no is : $sum"
;;
3)
exit
;;
*)
echo "Invalid choice"
;;
esac
fi


No comments:

Post a Comment