LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris and OpenSolaris.
General Sun, SunOS and Sparc related questions also go here.

Notices

Reply
 
Search this Thread
Old 12-01-2011, 04:09 AM   #1
nano2
Member
 
Registered: May 2007
Posts: 100

Rep: Reputation: 15
Bash array manipulation on solaris


Hi ,

I have the following syntax which is fine when executed on linux but get syntax error on Solaris

Code:
arrayB=( aa bb cc dd ff ee)

for (( i = ${#arrayB[@]} ; i >= 0; i-- )); do

 arrayA+=( "${arrayB[i]}" )'
output :
Code:
line 2: syntax error near unexpected token `"${arrayB[i]}"'
Anyone Know another way i could do the above so it won't fall over ?

Last edited by nano2; 12-01-2011 at 03:05 PM.
 
Old 12-01-2011, 04:06 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,482

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Your sample code runs fine on Solaris 11.
On older Solaris releases, you need to install a recent enough bash or ksh93 to get this syntax supported.
 
Old 12-01-2011, 04:14 PM   #3
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
The issue is probably due to a difference in the version of bash, not whether it's running on linux or solaris.
 
Old 12-01-2011, 04:19 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,482

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
@kbp: s/probably/definitely/
 
Old 12-01-2011, 04:27 PM   #5
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Yeah .. I thought about it but played it safe .. lets just call it '%99.9 likely'
 
Old 12-02-2011, 02:46 AM   #6
nano2
Member
 
Registered: May 2007
Posts: 100

Original Poster
Rep: Reputation: 15
I understand but is there another way to append to array rather than using the "+=" syntax ?
 
Old 12-02-2011, 05:49 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,528
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by nano2 View Post
I understand but is there another way to append to array rather than using the "+=" syntax ?
Yes (the loop on arrayB members is adjusted to process them all):
Code:
#!/bin/bash

arrayB=( aa bb cc dd ff ee )

iA=0
for (( i = $(( ${#arrayB[@]} -1 )); i > -1; i-- ))
do
    arrayA[iA++]=${arrayB[i]}
done

for (( i = 0; i < ${#arrayB[@]}; i++ ))
do
    echo "\${arrayA[$i]}: ${arrayA[$i]}"
done
 
Old 12-02-2011, 07:43 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
I see a single stray quotemark at the end of the line that shouldn't be there.

Also, you can use this instead of +=:

Code:
arrayA=( "${arrayA[@]}" "${arrayB[i]}" )
The addition pattern was added way back in v3.1 though, so chances are that's not where your problem lies.

http://mywiki.wooledge.org/BashFAQ/061
 
Old 12-02-2011, 10:41 AM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,482

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Quote:
Originally Posted by David the H. View Post
The addition pattern was added way back in v3.1 though, so chances are that's not where your problem lies.
That actually confirm the problem lies here.

Solaris 10 design predates bash 3.1 so is bundling bash 3.0.
 
Old 12-04-2011, 05:05 AM   #10
nano2
Member
 
Registered: May 2007
Posts: 100

Original Poster
Rep: Reputation: 15
Question

Code:
arrayB=( aa bb cc dd ff ee)
arrayA=(bbb plh yuo uiv xcx bb yai ff aa trdcc ee uuu ff asd cc )
expected output :

[code ]
aa bb cc dd ff ee bbb plh yuo uiv xcx yai ff trdcc uuu asd

[/code]

if i use the following syntax

it overwrites the previous appended element from arrayB onto in the arrayA
Code:
arrayA=( "${arrayA[@]}" "${arrayB[i]}" )


I have something like this but the append to the newarray isn't supported on all bash versions so is there another way i can append to the newarray the sorting is not done alphabetical i just have to have the elemnts sorted based on arrayA and below works Fine par the append syntax!

Code:
list2="${arrayB[*]}"
cnt=${#arrayA[@]
for ((i=0; i <cnt ; i++ )); do
    pref=${arrayA[i]}

    if grep -w -q $pref <<< "$list2"; then
      newarray += "$pref "
      list2="${list2/$pref }"
     fi
done
which is not compatible with solaris bash

Last edited by nano2; 12-04-2011 at 07:56 AM.
 
Old 12-04-2011, 06:02 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Quote:
Originally Posted by nano2 View Post

if i use the following syntax

it overwrites the previous appended element from arrayB onto in the arrayA
Code:
arrayA=( "${arrayA[@]}" "${arrayB[i]}" )
You asked for a replacement for +=(), right? Well, the line above is exactly equivalent to:

Code:
arrayA+=( "${arrayB[i]}" )
It expands all the existing values in arrayA, adds arrayB[i] to the end of the list, and resets arrayA to the new set of values.

To do the same string concatenation with a scalar variable you simply use:
Code:
varA="$varA$varB"
Whether this does what you want depends on how you use it in the script.


But what you are asking about now appears to be going back to your previous thread on sorting the array order:

http://www.linuxquestions.org/questi...-array-911475/

My final reply there had some questions about clarifying the requirements that you never answered. Perhaps it's time for you to explain exactly, and in detail, what your script needs to do.
 
Old 12-04-2011, 06:54 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Regarding at the code above, does this do what you want?

Code:
cnt=${#arrayA[@]}
copyB=( "${arrayB[@]}" )

for (( i=0; i<$cnt ; i++ )); do

	pref=${arrayA[i]}

	if [[ "${copyB[*]}" =~ "$pref" ]]; then
		newarray=( "${newarray[@]}" "$pref" )
		copyB=( "${copyB[@]//$pref}" )
	fi

done

echo "${newarray[@]}"
I have no idea what ${#FAOrder[@]} is supposed to expand to, so I just simplified it to iterating through arrayA.

Also, as I mentioned in the previous thread, using a scalar variable for lists of things isn't all that safe, so I did away with list2 and replaced it with a simple copy of arrayB to use instead. Of course if you don't need to preserve the original, then you can just use arrayB directly. The cnt and pref variables aren't really necessary either when you can just use the raw values directly, but they can make the code a bit more readable so I left them in. Finally, I eliminated the pointless external call to grep.

Note though that I've confirmed that every technique use here appears in 3.0 or earlier, but I can't confirm that the behavior is entirely consistent with what I get in 4.2, as there may have been subtle changes since then.

Last edited by David the H.; 12-04-2011 at 08:25 AM. Reason: small addendum + code fix
 
  


Reply

Tags
bash scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Bash: Calculating in array and storing result in array ghantauke Linux - Newbie 6 12-02-2010 12:28 PM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 09:38 PM
Bash - Reading csv delimited file to array and for further manipulation BLWEGRZYN Programming 1 01-06-2010 09:38 PM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM
Bash Variable Array, Trying to add another value into the array helptonewbie Linux - Newbie 6 03-02-2009 11:18 PM


All times are GMT -5. The time now is 11:18 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration