LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 11-11-2012, 08:28 PM   #1
timonthy28
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Rep: Reputation: Disabled
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.
 
Old 11-11-2012, 09:21 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Please post your script and explain how it does not do what you want.
 
Old 11-12-2012, 04:33 AM   #3
timonthy28
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
project sh

here is what I have so far

#!/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
 
Old 11-12-2012, 05:57 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
As per Catkin, what if any problems do you have with that.?

I would point out that this
Code:
while [$counter -lt "50"]
would produce a syntax error due to lack of spaces around the [] chars. Also, no need to quote a numeric constant.
I also recommend [[]] instead http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS

You may find these useful
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

For debug purposes,use 'set -xv' thus
Code:
#!/bin/bash
set -xv
 
Old 11-12-2012, 06:09 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Code:
#!/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.
 
Old 11-12-2012, 11:17 AM   #6
timonthy28
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Looping

Here is what I have but receiving errors:




#!/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
 
Old 11-12-2012, 12:38 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 11-12-2012, 12:52 PM   #8
timonthy28
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
sorry

The error I am now getting is line 7: [: -lt: unary operator expected



thanks
 
Old 11-12-2012, 12:56 PM   #9
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by timonthy28 View Post
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.
 
Old 11-12-2012, 06:32 PM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
tim*
I would encourage you to respond to ALL of the inputs you are getting----if someone gives you 3 suggestions, then I would respond to all three.
 
Old 11-13-2012, 01:59 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Might also pay you to look at the test command ([ && [[ are synonyms for this) and see what -lt is used for, ie. numbers or strings
 
  


Reply



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
[SOLVED] Bash Scripting (until loop) cryingthug Programming 10 06-30-2010 09:30 PM
Need help in bash scripting in while loop manya Programming 2 11-25-2009 09:51 AM
Loop iteration in Linux scripting. 151803 Linux - Newbie 3 03-19-2007 06:36 AM
bash scripting and loops phoeniks Programming 5 01-24-2005 04:00 PM
bash scripting - referring to external arguments into loops linsson Linux - General 2 07-23-2004 12:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:14 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
Open Source Consulting | Domain Registration