| Author |
Message |
RSH

Joined: 05 Sep 2011 Posts: 1564 Location: Germany
|
Posted: Thu 20 Dec 2012, 19:06 Post_subject:
Array in bash |
|
Hi.
Is there a short solution for this?
| Code: | | DVArray=("203" "204" "205" "206" "207" "208" "208" "210" ... ... ... ... ... "300") |
Maybe something like this?
| Code: | | DVArray=("203" .. "300") |
Thanks
RSH
_________________ Useful Software for Puppy!
LazY Puppy - a Paradise Puppy! - DE & EN ISO 2.0.2-0.0.5 available
|
|
Back to top
|
|
 |
seaside
Joined: 11 Apr 2007 Posts: 841
|
Posted: Thu 20 Dec 2012, 20:17 Post_subject:
Re: Array in bash |
|
| RSH wrote: | Hi.
Is there a short solution for this?
| Code: | | DVArray=("203" "204" "205" "206" "207" "208" "208" "210" ... ... ... ... ... "300") |
Maybe something like this?
| Code: | | DVArray=("203" .. "300") |
Thanks
RSH |
Perhaps this-
| Code: | # DVArray=( `for i in {203..300};do echo "$i ";done` )
# echo ${DVArray[2]}
205
|
Cheers,
s
(Note space here in "$i ")
|
|
Back to top
|
|
 |
RSH

Joined: 05 Sep 2011 Posts: 1564 Location: Germany
|
Posted: Thu 20 Dec 2012, 20:33 Post_subject:
|
|
| Code: | # 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!
RSH
_________________ Useful Software for Puppy!
LazY Puppy - a Paradise Puppy! - DE & EN ISO 2.0.2-0.0.5 available
|
|
Back to top
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 1776
|
Posted: Fri 21 Dec 2012, 02:36 Post_subject:
|
|
Even better:
DVArray=( $(seq 203 300) )
|
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 4015 Location: Arizona, U.S.A.
|
Posted: Fri 21 Dec 2012, 19:11 Post_subject:
|
|
amigo; That`s a loop method I`ve never seen before.
I`ve got to go back and study the new Bash version docs.
|
|
Back to top
|
|
 |
seaside
Joined: 11 Apr 2007 Posts: 841
|
Posted: Sat 22 Dec 2012, 11:24 Post_subject:
|
|
| 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
|
|
Back to top
|
|
 |
sunburnt

Joined: 08 Jun 2005 Posts: 4015 Location: Arizona, U.S.A.
|
Posted: Sat 22 Dec 2012, 22:52 Post_subject:
|
|
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.
Edited_time_total
|
|
Back to top
|
|
 |
technosaurus

Joined: 18 May 2008 Posts: 3845
|
Posted: Tue 25 Dec 2012, 16:52 Post_subject:
|
|
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.
_________________ Puppy Web Desktop Now with pet packages - Pet Packaging 100 & 101
|
|
Back to top
|
|
 |
|