Array in bash

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

Array in bash

#1 Post by RSH »

Hi.

Is there a short solution for this?

Code: Select all

DVArray=("203" "204" "205" "206" "207" "208" "208" "210" ... ... ... ... ... "300")
Maybe something like this?

Code: Select all

DVArray=("203" .. "300")
Thanks

RSH
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

Re: Array in bash

#2 Post by seaside »

RSH wrote:Hi.

Is there a short solution for this?

Code: Select all

DVArray=("203" "204" "205" "206" "207" "208" "208" "210" ... ... ... ... ... "300")
Maybe something like this?

Code: Select all

DVArray=("203" .. "300")
Thanks

RSH
Perhaps this-

Code: Select all

# DVArray=( `for i in {203..300};do echo "$i ";done` )
#  echo ${DVArray[2]}
205
Cheers,
s
(Note space here in "$i ")

User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

#3 Post by RSH »

Code: Select all

# DVArray=( `for i in {203..300};do echo "$i ";done` )
#  echo ${DVArray[2]}
205
Thank you, seaside!

This is exactly what I was looking for! :)

I need to have a lot more bash knowledge! :lol:

RSH
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#4 Post by amigo »

Even better:
DVArray=( $(seq 203 300) )

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#5 Post by sunburnt »

amigo; That`s a loop method I`ve never seen before.

I`ve got to go back and study the new Bash version docs.

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#6 Post by seaside »

sunburnt wrote:
I`ve got to go back and study the new Bash version docs.
sunburnt,

"seq" is a separate program and since bash 3+, practically anything done by seq is now possible in bash "for" loops.

Regards,
s

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#7 Post by sunburnt »

seaside; Thanks for the info. Now I really need to do a refresher...

I was surprised to find that the for loop will read a file directly!

This loop: echo "$List" |while read Line ;do ... ;done
Still has an advantage or two.
Last edited by sunburnt on Wed 26 Dec 2012, 01:35, edited 1 time in total.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#8 Post by technosaurus »

It looks like the question asked was solved, but it is possible that it may not have been the right question. There are often better ways of handling arrays of sequential info depending on the context, by just using a few variables. (arrays are bashisms btw)... so the shorter solution could be not to use an array.

Ex. use:
starting_point, end_point, step
or
offset, increment_by, max_value

Which often need to be setup in the loops in some hacky way around the array anyways ... usually with an offset and/or max and step value.
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Post Reply