Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A loop that adds 1 every time 3

Status
Not open for further replies.

Acr111

Technical User
Sep 8, 2011
2
AU
Hey guys, I'm relatively new to Unix Scripting hence the straight forward question.

I'm trying to write a loop that will output a number each time you want it too. All I seem to able to do is create a loop that will output an infinite stream of numbers that doesn't stop. For example I have a function insertlinefunction(), and when I call that function I want it to output a number '1:' , then the next time I call it, it will output '2:' etc.. So what i'm saying is that I want to be able to display a single number starting from one and increase by one each time I call the function. At the moment when I call the function it will output all of the numbers at the same time:
1:
2:
3:
4:
etc..
Hope that makes sense.

Thanks
 
Hi

Acr111 said:
All I seem to able to do is create a loop that will output an infinite stream of numbers that doesn't stop.
Please post your code so we can suggest fix(es) to the syntactical and/or conceptual mistakes.

In meantime this is what I would do :
Code:
[COLOR=darkgoldenrod]insertlinefunction()[/color] { echo [green][i]"$((nr++)):"[/i][/green][teal];[/teal] }
Tested with [tt]bash[/tt] and [tt]mksh[/tt].


Feherke.
 
Thanks! That worked. However is it possible for it to start at 1 instead of 0? Also once I call the function, I'm trying to make it possible to insert text onto the line. Eg if I am up to line '5:' I'm trying to be able to insert text onto that line through using another function.

Cheers
 
Hi

Acr111 said:
However is it possible for it to start at 1 instead of 0?
[ul]
[li]The simplest way is to initialize the $nr variable with whatever number you want, before first calling the function.
Code:
[highlight][navy]nr[/navy][teal]=[/teal][purple]1[/purple][teal];[/teal][/highlight] [COLOR=darkgoldenrod]insertlinefunction()[/color] { echo [green][i]"$((nr++)):"[/i][/green][teal];[/teal] }
[/li]
[li]The compact way is to use pre-increment operator instead.
Code:
[COLOR=darkgoldenrod]insertlinefunction()[/color] { echo [green][i]"$(([highlight]++[/highlight]nr)):"[/i][/green][teal];[/teal] }
[/li]
[/ul]
Acr111 said:
I'm trying to make it possible to insert text onto the line.
[ul]
[li]Then simple way is to tell [tt]echo[/tt] to spare the newline character at the end of string.
Code:
[COLOR=darkgoldenrod]insertlinefunction()[/color] { echo [highlight]-n[/highlight] [green][i]"$((nr++)):"[/i][/green][teal];[/teal] }
Code:
[COLOR=darkgoldenrod]insertlinefunction()[/color] { echo [green][i]"$((nr++)):[/i][/green][lime][i][highlight]\c[/highlight][/i][/lime][green][i]"[/i][/green][teal];[/teal] }
[/li]
[li]The compact way is to pass the additional text as parameter to the function.
Code:
[COLOR=darkgoldenrod]insertlinefunction()[/color] { echo -n [green][i]"$((nr++)): [highlight]$@[/highlight]"[/i][/green][teal];[/teal] }
[/li]
[/ul]


Feherke.
 
Star to Feherke for going the extra couple of miles/kilometres!

The internet - allowing those who don't know what they're talking about to have their say.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top