LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   using bash to append a string to array (https://www.linuxquestions.org/questions/programming-9/using-bash-to-append-a-string-to-array-881657/)

nano2 05-19-2011 03:06 PM

using bash to append a string to array
 
I have the following function that does not iterate through the array I want to be able to do some manipulation on each element in the array[@].
it appears the below array has only one item in the array whereas i want the array to have 3 items hence the loop three times printing the message Any ideas why this is not happening ???


function foo() {
name =$1
array=( "$2" )
for i in `seq 0 $(( ${#array[@]} - 1 ))`; do
echo "$i: ${array}"
echo "./prog $name ${array}"

done
}
NAME ="BLAH"
arrayP+=("TEST") ;

arrayP+=("TESTB")
arrayP+=("TESTC")
foo $NAME "${arrayP[@]}"

catkin 05-19-2011 03:26 PM

Try echo "$i: ${array[i]}"

nano2 05-19-2011 04:43 PM

sorry that was a typo i was echo array[i]
Also the in the foo function i only get one element printed out $2 if i use $@ then i get $NAME and array elements

I think this is problem .

any ideas why $2 doesn't have all array elements ???

everToulouse 05-19-2011 05:50 PM

Code:

shouldBe()
{
  name=$1
  shift
  array=( "$@" )
  for i in "${array[@]}"; do echo "$i"; done
}
arrayP=( foo bar 'bar baz' )

shouldBe anyName "${arrayP[@]}"
foo
bar
bar baz


David the H. 05-19-2011 06:09 PM

1) Please use [code][/code] tags around your code, to preserve formatting and to improve readability.

2) When setting a variable, you can't have any spaces around the equals sign.

3)
Code:

array=( "$2" )
When you quote a variable/parameter, word-splitting does not occur and the contents are treated as a single item. Remove the quotes when you want the string to be split into individual "words", which in this case will place them into separate array elements (word splitting being defined by the IFS shell variable, space/tab/newline by default).

Exception: quoting "${array[@]}" will expand to each individual array element, with those elements being treated as if they were quoted separately. So when you call your function, you're not getting a $2, you're getting a "$2" "$3" "$4", et cetera.

A better way to handle your function then would be like this:
Code:

foo() {
  name=$1
  shift
  array=( $@ )
}

foo "$NAME" "${arrayP[@]}"

You might consider declaring your function variables to be local as well.

4) $(..) is recommended over `..`, and seq isn't usually necessary when you have brace expansion, although it's not easy to use variables with them.

A better way to iterate through the array elements though is to use this:
Code:

for i in ${!array[@]}; do
  echo "${array[i]}"
done

...which expands to a list of all the existing index numbers in the array.

5) Finally, why not simply set your arrayP like this?
Code:

arrayP=( TEST TESTB TESTC )
You can even use brace expansion here, especially if you have more items in the sequence.
Code:

arrayP=( TEST TEST{B..C} )

catkin 05-20-2011 12:40 AM

Quote:

Originally Posted by nano2 (Post 4361267)
sorry that was a typo i was echo array[i]

If possible it is best to copy and paste into posts to avoid such typos.


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