LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 02-06-2010, 09:52 PM   #1
studywell09
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Rep: Reputation: 0
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?
 
Old 02-07-2010, 12:53 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
"if" needs a "then" for the "fi" to work
 
Old 02-07-2010, 01:01 AM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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.
 
Old 02-07-2010, 04:02 AM   #4
studywell09
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Original Poster
Rep: Reputation: 0
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..
 
Old 02-07-2010, 04:44 AM   #5
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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?
 
Old 02-07-2010, 05:11 AM   #6
studywell09
LQ Newbie
 
Registered: Aug 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Yes, i am entering each number on a new line..
 
Old 02-07-2010, 02:32 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I compare floating number in a shell script? abefroman Programming 34 10-12-2016 08:09 AM
how to find given number is integer or not in shell script kvmreddy Linux - Newbie 2 08-15-2009 08:05 AM
square root of number using sqrt and/or awk skuz_ball Programming 4 08-04-2008 04:18 AM
shell script (bind related) OTIM Programming 6 07-10-2008 04:26 PM
Shell-script question about getting a number as a variable stormrider_may Programming 9 03-14-2006 08:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration