LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-06-2012, 01:29 PM   #1
kross2100
LQ Newbie
 
Registered: Dec 2012
Posts: 9

Rep: Reputation: Disabled
User entered variable as counter in For Loop


So here is a question on my homework - Write a program to take two numbers from the user as num1 and num2 and show the result of num1 to the power of num2. You cannot use the "**" operator. You can only use the multiplication operator. (use a for loop that would repeat num2 times)

So I tried doing it with this code -
echo "give me a number"
read num1
echo "okay, give me another number"
read num2
for a in $num2
do
let a=$a*$num1
done
echo $a

But I must be missing something in the line that begins the for loop because it wont iterate num2 times, it will only loop once. So basically, how do I get the program to recognize the value of num2 as the counter for the loop?
 
Old 12-06-2012, 01:36 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
"for i in $var" syntax loops over words in the variable, and assigns each word to the variable i on each iteration of the loop. So if "var" looked like:
"one two three"

Then "for i in $var; do echo $i; done" would look like:
one
two
three

Three iterations of the loop, with the current word being assigned to the variable i on each iteration.

What you need is this style for loop:
Code:
for ((i=0; i<$num2; i++)); do
   echo $i
done
Which, if num2=5, would look like:
0
1
2
3
4

Last edited by suicidaleggroll; 12-06-2012 at 01:38 PM.
 
1 members found this post helpful.
Old 12-06-2012, 01:46 PM   #3
kross2100
LQ Newbie
 
Registered: Dec 2012
Posts: 9

Original Poster
Rep: Reputation: Disabled
thank you very much! Based on your help, here is the code that works:

echo "give me a number"
read num1
echo "okay, give me another number"
read num2
a=$num1
for ((i=1; i<$num2; i++))
do
let a=$a*$num1
done
echo $a

Last edited by kross2100; 12-06-2012 at 01:51 PM. Reason: Wanted to make sure others get the right answer in the future
 
Old 12-06-2012, 02:03 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
Please use code-tags when posting code, this will make your posts much more readable and preserve the formatting of the code.
http://www.linuxquestions.org/questi...do=bbcode#code
 
  


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
Bash - Creating Arrays using loop counter Mixiul Programming 4 02-08-2012 10:05 AM
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 01:44 AM
[SOLVED] why is this for loop not entered? bluethundr Linux - Newbie 4 10-07-2010 05:05 AM
PHP - POST variables named with counter: $_POST[${"variable$counter"} winairmvs Programming 1 08-06-2010 05:56 PM
BASH assistance - loop/counter rickenbacherus Programming 6 03-12-2007 06:34 PM


All times are GMT -5. The time now is 12:32 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration