LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-19-2011, 03:06 PM   #1
nano2
Member
 
Registered: May 2007
Posts: 100

Rep: Reputation: 15
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[@]}"
 
Old 05-19-2011, 03:26 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Try echo "$i: ${array[i]}"
 
Old 05-19-2011, 04:43 PM   #3
nano2
Member
 
Registered: May 2007
Posts: 100

Original Poster
Rep: Reputation: 15
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 ???
 
Old 05-19-2011, 05:50 PM   #4
everToulouse
LQ Newbie
 
Registered: Apr 2010
Posts: 18

Rep: Reputation: 5
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
 
Old 05-19-2011, 06:09 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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} )

Last edited by David the H.; 05-19-2011 at 06:11 PM. Reason: fixed a couple of mistakes
 
Old 05-20-2011, 12:40 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by nano2 View Post
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.
 
  


Reply

Tags
bash scripting



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] split a string into array in bash xeon123 Linux - Newbie 8 03-17-2011 11:16 AM
Bash string splitting into array praveenhm Programming 14 10-26-2010 02:51 PM
[SOLVED] Find a substring/member in a string/array using bash someshpr Programming 4 10-14-2010 02:17 AM
bash string to array patolfo Programming 16 05-17-2010 10:12 AM
bash: append string to end of line khairil Programming 6 02-27-2007 05:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration