Stuck with Shell prog in linux

techking_dinesh

Digit Squad Member
Hello,
I have just started learning sh in linux
I am using backtrack 5


When i run the following file, i get some error regarding case or in

Kindly guide me wat the error is

#======================================
# Hello World Programme
#=====================================
ch = 1
while (( $ch < 12 ))
do
echo Welcome to My Maths Programme
echo 1.Addition
echo 2.Subtraction
echo 3.Multiplication
echo 4.Division
echo 5.Odd Even Check
echo 6.Positive Negative check
echo 7.Prime Number Check
echo 8.Greatest number
echo 9.Palindrome Check
echo 10.Factorial
echo 11.Exit
echo Enter your choice:
read ch
case "$ch" in
#==================================
# Addition Programme
#=================================
1)echo Addition Programme
echo
echo Enter value for A:
read a
echo Enter value for B
read b
echo
echo First Number is: $a and Second Number is: $b
((c=$a+$b))
echo
echo Addition is $c
echo
echo
;;
#============================
# Subtraction Programme
#============================
2)echo Subtraction Programme
echo
echo Enter value for A
read a
echo Enter value for B
read b
echo
echo First Number is: $a and Second Number is: $b
((c=$a-$b))
echo
echo Subtraction is $c
echo
echo
;;
#===========================
# Multiplication
#===========================
3)echo Multiplication Programme
echo
echo Enter value for A
read a
echo Enter value for B
read b
echo
echo First Number is: $a and Second Number is: $b
((c=$a*$b))
echo
echo Multiplication is $c
echo
echo
;;
#===========================
# Division
#===========================
4)echo Division Programme
echo
echo Enter value for A
read a
echo Enter value for B
read b
echo
echo First Number is: $a and Second Number is: $b
((c=$a/$b))
echo
echo Division is $c
echo
;;
#===========================
# Odd Even check
#===========================
5)echo Odd or Even Programme
echo
echo Enter a Value
read a
echo
echo Your Number is: $a
((b=$a%2))
if [ $b = 0 ]
then echo Number is Even
else echo Number is Odd
fi
;;
#===========================
# + - check
#===========================
6)echo
echo Positive Negative check
echo
echo Enter a Value
read a
echo
echo Your Number is: $a
if [ $a > 0 ]
then echo Number is Positive
else echo Number is Negative
fi
;;
#===========================
# Prime Number Check
#===========================
7)echo
echo Prime Number check
echo
echo Enter a Value
read a
echo
echo Your Number is: $a
((r1=$a%2))
((r2=$a%3))
((r3=$a%5))
((r4=$a%7))
if [ $a = 2 ] || [ $a = 3 ] ||[ $a = 5 ] ||[ $a = 7 ]
then echo Number is Prime
elif [ $r1 -eq 0 ] || [ $r2 -eq 0 ] || [ $r3 -eq 0 ] || [ $r4 -eq 0 ]
then echo Not Prime Number
else echo Number is Prime
fi
;;

#===========================
# Greatest of 3
#===========================
8)echo
echo Greatest of 3 Numbers
echo
echo Enter First Number
read a
echo Enter Second Number
read b
echo Enter Third Number
read c
echo
echo Your Numbers are: $a , $b, $c
if [ $a -gt $b ] && [ $a -gt $c ]
then echo $a is Greatest
elif [ $b > $a ] && [ $b > $c ]
then echo $b is Greatest
else echo $c is Greatest
fi
;;

#===========================
# Palindrome
#===========================
9)echo yet to do
;;

#===========================
# Factorial
#===========================
10)echo
echo Factorial
echo
echo Enter Number
read a
echo
echo Your Numbers is: $a
res=1
for(( i=$a ; $i -ge0 ;i=$i-1 ))
do
(( res=$res*$i ))
done
echo Factorial is $res
;;


*)exit;;
esac
done

Waiting to knw where i went silly

Regards
 

doomgiver

Warframe
i think case should have brackets/parentheses of some sort.
check the man page, its usually the best source to check for errors
 

mediator

Technomancer
Mistakes
1. ">" is used for directing the output. When comparing in shell "-lt" etc are used.
2. Arithematics can be done in various ways. The one which you used has "let" before e.g let a=$b+$c
3. There is no space between $b, + and $c.

Corrected code ----
Code:
#======================================
# Hello World Programme
#=====================================
ch=1
while [ $ch -lt 12 ]
do
echo Welcome to My Maths Programme
echo 1.Addition
echo 2.Subtraction
echo 3.Multiplication
echo 4.Division
echo 5.Odd Even Check
echo 6.Positive Negative check
echo 7.Prime Number Check
echo 8.Greatest number
echo 9.Palindrome Check
echo 10.Factorial
echo 11.Exit
echo Enter your choice:
read ch
case "$ch" in
#==================================
# Addition Programme
#=================================
1)
echo Addition Programme
echo
echo "Enter value for A":
read a
echo "Enter value for B"
read b
echo
echo "First Number is: $a and Second Number is: $b"
let c=$a+$b
echo
echo Addition is $c
echo
echo
;;
#============================
# Subtraction Programme
#============================
2)
echo Subtraction Programme
echo
echo Enter value for A
read a
echo Enter value for B
read b
echo
echo First Number is: $a and Second Number is: $b
let c=$a-$b
echo
echo Subtraction is $c
echo
echo
;;

#===========================
# Multiplication
#===========================

3)
echo Multiplication Programme
echo
echo "Enter value for A"
read a
echo "Enter value for B"
read b
echo
echo "First Number is: $a and Second Number is: $b"
let c=$a*$b
echo
echo Multiplication is $c
echo
echo
;;
#===========================
# Division
#===========================
4)
echo Division Programme
echo
echo "Enter value for A"
read a
echo "Enter value for B"
read b
echo
echo "First Number is: $a and Second Number is: $b"
let c=$a/$b
echo
echo Division is $c
echo
;;
#===========================
# Odd Even check
#===========================
5)
echo Odd or Even Programme
echo
echo Enter a Value
read a
echo
echo Your Number is: $a
let b=$a%2
if [ $b = 0 ]
then echo Number is Even
else echo Number is Odd
fi
;;
#===========================
# + - check
#===========================
6)echo
echo Positive Negative check
echo
echo Enter a Value
read a
echo
echo Your Number is: $a
if [ $a > 0 ]
then echo Number is Positive
else echo Number is Negative
fi
;;
#===========================
# Prime Number Check
#===========================
7)echo
echo Prime Number check
echo
echo Enter a Value
read a
echo
echo Your Number is: $a
let r1=$a%2
let r2=$a%3
let r3=$a%5
let r4=$a%7
if [ $a = 2 ] || [ $a = 3 ] ||[ $a = 5 ] ||[ $a = 7 ]
then echo Number is Prime
elif [ $r1 -eq 0 ] || [ $r2 -eq 0 ] || [ $r3 -eq 0 ] || [ $r4 -eq 0 ]
then echo Not Prime Number
else echo Number is Prime
fi
;;

#===========================
# Greatest of 3
#===========================
8)echo
echo Greatest of 3 Numbers
echo
echo Enter First Number
read a
echo Enter Second Number
read b
echo Enter Third Number
read c
echo
echo Your Numbers are: $a , $b, $c
if [ $a -gt $b ] && [ $a -gt $c ]
then echo $a is Greatest
elif [ $b -gt $a ] && [ $b -gt $c ]
then echo $b is Greatest
else echo $c is Greatest
fi
;;

#===========================
# Palindrome
#===========================
9)echo yet to do
;;

#===========================
# Factorial
#===========================
10)echo
echo Factorial
echo
echo Enter Number
read a
echo
echo Your Numbers is: $a
res=1
for(( i=$a ; $i -ge0 ;i=$i-1 ))
do
(( res=$res*$i ))
done
echo Factorial is $res
;;


*)exit;;
esac
done
---Used in bash!
 
Top Bottom