Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
02-06-2010, 09:52 PM
|
#1
|
LQ Newbie
Registered: Aug 2009
Posts: 3
Rep:
|
shell script related to square of number question
Hi,
Im trying to write a shell script to input few numbers and print every number that is a square of atleast one number given as input.The code i have written is as follows,
echo "Enter total no. of numbers"
read tot
no=0
i=0
num=0
echo "Enter $tot Numbers"
while test $i -lt $tot
do
read no
a[i]=$no
i=`expr $i + 1`
done
echo "DISPLAY NOS"
i=0
while test $i -lt $tot
do
echo ${a[$i]}
i=`expr $i + 1`
done
echo "SQUARE"
i=0
while test $i -lt $tot
do
num=${a[$i]}
for (( j = 0 ; j < $tot ; j++ ))
do
x=${a[$j]}
if test `expr $num \* $num` -eq $x
echo "$x is the square of $num"
fi
j=`expr $j + 1`
done
i=`expr $i + 1`
done
It accepts the nos and displays them too.But later on this error appears,
SQUARE
./m10: line 33: syntax error near unexpected token `fi'
./m10: line 33: ` fi'
What is my mistake?
|
|
|
02-07-2010, 12:53 AM
|
#2
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep:
|
"if" needs a "then" for the "fi" to work
|
|
|
02-07-2010, 01:01 AM
|
#3
|
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
|
Once you put the 'then' back, you will discover you have incremented j twice in the for loop, which means it skips every second number.
|
|
|
02-07-2010, 04:02 AM
|
#4
|
LQ Newbie
Registered: Aug 2009
Posts: 3
Original Poster
Rep:
|
Thanks for your reply.I have added the then and removed the extra incrementation of j.But now it gives me the error ,
'integer expression expected' when i multiply num using expr.
Is my way of storing the array value in the variable wrong??Should it be,
num={${a[$i]}} instead??
Please reply..
|
|
|
02-07-2010, 04:44 AM
|
#5
|
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
|
Quote:
But now it gives me the error, 'integer expression expected' when i multiply num using expr.
|
I suspect it is a problem with the input data. Are you entering each number on a new line?
|
|
|
02-07-2010, 05:11 AM
|
#6
|
LQ Newbie
Registered: Aug 2009
Posts: 3
Original Poster
Rep:
|
Yes, i am entering each number on a new line..
|
|
|
02-07-2010, 02:32 PM
|
#7
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep:
|
studywell09 -
To understand what a variable really looks like (even though you *think* it it should be an integer, it might be a letter like "a", or a blank space)...
... then "echo" is your friend:
http://dollhousewiki.fox.com/page/Echo
Last edited by paulsm4; 02-07-2010 at 02:34 PM.
|
|
|
All times are GMT -5. The time now is 08:59 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|