Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 04-11-2011, 01:50 PM   #1 (permalink)
Designer Here
 
techking_dinesh's Avatar
 
Join Date: Aug 2008
Location: Nashik
Posts: 396
Default Stuck with Shell prog in linux


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

Spoiler:



#======================================
# 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
techking_dinesh is offline   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 04-11-2011, 03:12 PM   #2 (permalink)
Mmmph!!!
 
doomgiver's Avatar
 
Join Date: Nov 2010
Location: Mmmphhmph Mmphph
Posts: 1,408
Default Re: Stuck with Shell prog in linux

i think case should have brackets/parentheses of some sort.
check the man page, its usually the best source to check for errors
__________________
Mmmphh-mphhhh-mmphh mhh!!!

Steam : doomgiver
doomgiver is offline   Reply With Quote
Old 04-11-2011, 04:20 PM   #3 (permalink)
String Phreak
 
mediator's Avatar
 
Join Date: Mar 2005
Location: In ur Evil Mind!
Posts: 2,457
Default Re: Stuck with Shell prog in linux

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!
__________________
Bad Bad server.....No candy for u!
mediator is offline   Reply With Quote
Old 04-11-2011, 07:22 PM   #4 (permalink)
Designer Here
 
techking_dinesh's Avatar
 
Join Date: Aug 2008
Location: Nashik
Posts: 396
Default Re: Stuck with Shell prog in linux

^ thnx..
I did try the -lt before.. it was not working..
Nw it is..
May be the space prob..

pro solved.. thnx
techking_dinesh is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:36 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2