LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Scripting--need to create a 2 loops in a loop--can someone help (https://www.linuxquestions.org/questions/linux-newbie-8/scripting-need-to-create-a-2-loops-in-a-loop-can-someone-help-4175436714/)

timonthy28 11-11-2012 08:28 PM

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

catkin 11-11-2012 09:21 PM

Please post your script and explain how it does not do what you want.

timonthy28 11-12-2012 04:33 AM

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

chrism01 11-12-2012 05:57 AM

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


pixellany 11-12-2012 06:09 AM

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.

timonthy28 11-12-2012 11:17 AM

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

pixellany 11-12-2012 12:38 PM

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.

timonthy28 11-12-2012 12:52 PM

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



thanks

suicidaleggroll 11-12-2012 12:56 PM

Quote:

Originally Posted by timonthy28 (Post 4827737)
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.

pixellany 11-12-2012 06:32 PM

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.

grail 11-13-2012 01:59 AM

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


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