PDA

View Full Version : Bash array text manipulation



DavidRLu
February 20th, 2012, 04:26 PM
Working in BASH
I have a requirement to take a number of command line arguments, append text to the end of each, and assign them to an array.
What it should do:
append.sh alpha beta gamma should result in:
server[1] = alphaAPPENDEDTEXT
server[2] = betaAPPENDEDTEXT
server[3] = gammaAPPENDEDTEXT

what I've got now:
--------------------------------------------
#!/bin/bash
dbnames=("$@")
for dbnames
do
eval server$[n]=${dbnames[n++]}"APPENDEDTEXT"
done

#The following lines will correctly echo the desired argument with appended text
# however, they are separate variables, not array elements
echo $server0
echo $server1
echo $server2
---------------------------------------------
What do I need to do to get it to assign the results of the "eval" line above to an array?
(Or - is there a simpler way to accomplish this?)
Thanks!

ofnuts
February 20th, 2012, 09:42 PM
Working in BASH
I have a requirement to take a number of command line arguments, append text to the end of each, and assign them to an array.
What it should do:
append.sh alpha beta gamma should result in:
server[1] = alphaAPPENDEDTEXT
server[2] = betaAPPENDEDTEXT
server[3] = gammaAPPENDEDTEXT

what I've got now:
--------------------------------------------
#!/bin/bash
dbnames=("$@")
for dbnames
do
eval server$[n]=${dbnames[n++]}"APPENDEDTEXT"
done

#The following lines will correctly echo the desired argument with appended text
# however, they are separate variables, not array elements
echo $server0
echo $server1
echo $server2
---------------------------------------------
What do I need to do to get it to assign the results of the "eval" line above to an array?
(Or - is there a simpler way to accomplish this?)
Thanks!
You don't need an "eval"...

The size of an array x is ${#x[@]}

There is a C-ish form of the for loop: "for (( x=0; x<limit;x++))"

The mere existence of an assignment such as "a[$x]=foo" declares "a" as an array.

Draw you conclusions.

DavidRLu
February 20th, 2012, 10:23 PM
Thanks, but...
Without "eval" bash4.1.10 (under cygwin) attempts to execute the statement rather than populating the variables.

Output of the exact same code without eval is:

apend.sh: line 5: server0=alphaAPPENDEDTEXT: command not found
apend.sh: line 5: server1=betaAPPENDEDTEXT: command not found

etc.

Also, I'm pretty sure that $server0 is not the same as ${server[0]}

ofnuts
February 21st, 2012, 12:42 AM
Because you have spaces around the "=" sign. Remove them.

DavidRLu
February 21st, 2012, 03:53 PM
Similar behavior with or without the spaces.
Without the eval statement the responses are:

With spaces:
apend.sh: line 5: server0: command not found
apend.sh: line 5: server1: command not found

Without spaces:
apend.sh: line 5: server0=alphaAPPENDEDTEXT: command not found
apend.sh: line 5: server1=betaAPPENDEDTEXT: command not found

ofnuts
February 21st, 2012, 09:33 PM
Similar behavior with or without the spaces.
Without the eval statement the responses are:

With spaces:
apend.sh: line 5: server0: command not found
apend.sh: line 5: server1: command not found

Without spaces:
apend.sh: line 5: server0=alphaAPPENDEDTEXT: command not found
apend.sh: line 5: server1=betaAPPENDEDTEXT: command not found


>>x[1]=foo
>>echo ${x[1]}
foo

>>sh
>>x[1]=foo
sh: x[1]=foo: not found


Are you really using bash (#! /bin/bash)?

DavidRLu
February 21st, 2012, 10:19 PM
Here's what I wound up with - which works:

#!/bin/bash

declare -i COUNT
COUNT=0
for name in $*
do
CURRENTDB=$(echo ${name}DBPVS | tr -s '[:lower:]'
'[:upper:]') #convert string to upper and assign to temporary variable
dbserver[$COUNT]=$CURRENTDB #insert temporary variable value into dbserver array
COUNT=COUNT+1
done


for output in "${dbserver[@]}"
do
echo $output
done

DavidRLu
February 21st, 2012, 10:20 PM
BTW - Thanks for your input!