LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Other *NIX
User Name
Password
Other *NIX This forum is for the discussion of any UNIX platform that does not have its own forum. Examples would include HP-UX, IRIX, Darwin, Tru64 and OS X.

Notices


Reply
  Search this Thread
Old 08-03-2021, 10:21 AM   #1
stewate4
LQ Newbie
 
Registered: Aug 2021
Posts: 3

Rep: Reputation: Disabled
Is there in bash scripts a way of using variables to specify different bash arrays


For example in this simple script

#!/bin/bash

declare -a MYARRAY1
declare -a MYARRAY2

function updatearray {
ARRAY=$1
VALUE=$2

$ARRAY+=$VALUE

}

updatearray MYARRAY1 "yyyy "
updatearray MYARRAY2 "bbbb "

echo "${MYARRAY1[@]}"
echo "${MYARRAY2[@]}"

exit 0

Just results in "line 10: MYARRAY1+=yyyy: command not found" rather than updating the array. This is actually for a more complex bash script I'm working on that has more arrays I want to update, so I'm hoping to avoid 'case' statements which would mean a big function body
 
Old 08-03-2021, 10:35 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
I guess you could
Code:
eval $ARRAY+=$VALUE
But I wouldn't recommend doing this. I believe you should rethink/rewrite the script to avoid this level of indirecton.

Last edited by shruggy; 08-03-2021 at 10:36 AM.
 
1 members found this post helpful.
Old 08-03-2021, 11:18 AM   #3
stewate4
LQ Newbie
 
Registered: Aug 2021
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks for the quick reply shruggy
 
Old 08-03-2021, 11:31 AM   #4
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,598

Rep: Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691
You have overlooked a couple of things.
Frankly, passing by reference should fix those things painlessly.
Try something (untested) like this
Code:
#!/bin/bash

declare -a MYARRAY1
declare -a MYARRAY2

function updatearray () {

   local -n  arr=$1 
   local VALUE="$2"

  arr+="${VALUE}"
}

updatearray MYARRAY1 "yyyy "
updatearray MYARRAY2 "bbbb "

echo "${MYARRAY1[@]}"
echo "${MYARRAY2[@]}"

exit 0
See if that works better, or gives a more useful error at least.
 
2 members found this post helpful.
Old 08-03-2021, 12:14 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Thanks wpeckham, (near) perfect!

Code:
#!/bin/bash

declare -a MYARRAY1
declare -a MYARRAY2

function updatearray () {

   local -n  arr=$1 
   local VALUE="$2"

  arr+=("${VALUE}")
}

updatearray MYARRAY1 "yyyy" <- trailing space not req'd for array assignment
updatearray MYARRAY1 "zzzz"
updatearray MYARRAY2 "bbbb"

echo "${#MYARRAY1[@]}"
echo "${MYARRAY1[@]}"

echo "${#MYARRAY2[@]}"
echo "${MYARRAY2[@]}"

exit 0
My bash is v4.3.048

Last edited by astrogeek; 08-03-2021 at 12:45 PM.
 
Old 08-03-2021, 12:54 PM   #6
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,598

Rep: Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691
FYI: My bash is 5.1.8 but I pulled this mostly off the top of my head. (Which mostly runs BASH 4.1!) ;-)
 
Old 08-03-2021, 01:05 PM   #7
stewate4
LQ Newbie
 
Registered: Aug 2021
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks everyone, you've been a great help

I used a hybrid solution in the end. Unfortunately my bash is pretty ancient (unfortunately not under my control) at
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)

So the "local -n" errored and it failed

I'm using this at the moment.

#!/bin/bash

declare -a MYARRAY1
declare -a MYARRAY2

function updatearray {
ARRAY=$1
VALUE=$2

eval $ARRAY+="${VALUE}"

}

updatearray MYARRAY1 "yyyy"
updatearray MYARRAY2 "bbbb"

echo "array 1"
echo "${MYARRAY1[@]}"
echo "array 2"
echo "${MYARRAY2[@]}"

exit 0

Which works fine.
 
Old 08-17-2021, 01:56 PM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,781

Rep: Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199Reputation: 1199
I doubt that.
You need the parentheses in the array assignment, as it was mentioned ealier.
Here comes a fix, and a more comprehensive test:

Code:
declare -a MYARRAY1
declare -a MYARRAY2

function updatearray {
ARRAY=$1
VALUE=$2

eval $ARRAY+='("${VALUE}")'

}

updatearray MYARRAY1 "yyyy"
updatearray MYARRAY1 "zz z"
updatearray MYARRAY2 "bbbb"

echo "array 1"
printf "(%s)\n" "${MYARRAY1[@]}"
echo "array 2"
printf "(%s)\n" "${MYARRAY2[@]}"
 
Old 08-25-2021, 07:43 AM   #9
Ida11
LQ Newbie
 
Registered: Aug 2021
Posts: 2

Rep: Reputation: 0
thanks
 
Old 08-25-2021, 07:44 AM   #10
Ida11
LQ Newbie
 
Registered: Aug 2021
Posts: 2

Rep: Reputation: 0
thank you
 
Old 08-25-2021, 11:21 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Quote:
Originally Posted by Ida11 View Post
thanks
reported
 
  


Reply

Tags
bash script, variable



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
specify root block device for boot, but specify where? Siljrath Gentoo 2 08-05-2011 10:01 PM
Arrays of Structures to Arrays of Classes knobby67 Programming 1 01-01-2008 01:39 PM
error: new : cannot specify initializer for arrays powah Programming 2 02-01-2007 04:15 PM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 05:40 AM
How to specify different authority to different user in VSFTPD luxxing2000 Linux - Software 0 11-04-2005 05:25 AM

LinuxQuestions.org > Forums > Other *NIX Forums > Other *NIX

All times are GMT -5. The time now is 08:15 PM.

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