ProgrammingThis 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.
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.
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'
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..
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)...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.