Scripting--need to create a 2 loops in a loop--can someone help
Linux - NewbieThis 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
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.
Scripting--need to create a 2 loops in a loop--can someone help
Explanation:
Write a script to be saved on our server as 'project2.sh' that does the following things:
• Asks the user how many times they want to loop
• Displays a growing line up to 50 “-” and then shrinks back to nothing
Sample output from script being run:
How many times do you want to loop?
1
Last edited by timonthy28; 11-12-2012 at 03:13 PM.
#!/bin/bash
#How many times do you want to loop?
outerCounter=0
while [$counter -lt "50"];do
echo counter
let counter=$counter+1
done
let outerCounter=$outerCounter+1
done
I set this up in CODE tags and did a bit of formatting--the first thing I see is that you do not have any nested loops---only an extra "done" statement. On your next post, please use the CODE tags and some kind of formatting.
You don't yet have anyway to get input from the user---take a look at the "read" command.
Once again, please use CODE tags and formatting. You can put in the tags by hand--or use the "#" button in the advanced editing mode. We want it to look something like this:
Code:
#!/bin/bash
#How many times do you want it to loop
outerCounter=0
#this is our OUTER Loop
while [ $counter -lt "50" ]; do
counter=0
#This is our INNER Loop
while [ $counter -lt "50" ]; do
#code goes here
echo counter
#increment couter
let counter=$counter+1
done
let outerCounter=$outerCounter+1
done
You don't say what errors you are receiving.
Here is what I see so far:
1. You don't do anything with "outerCounter"---should you be testing it in the outer loop?
2. To see the value of a variable, you need "$"---eg echo $counter
Did you read up on the "read" command?---you'll need that to get the user to input data.
The error I am now getting is line 7: [: -lt: unary operator expected
thanks
You're using counter in a conditional before it's declared. Your first while should be using $outerCounter, not $counter. Since counter has not been declared yet, $counter is expanding to nothingness, so "[ $counter -lt 50 ]" is being expanded to [ -lt "50" ], hence the error.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.