bash - Unable to retrieve all array elements -
i have script have defined 2 arrays. depending on array name passed (as parameter), want process same in function below. earlier, wasn't getting element in variable 'array' until used ${!}
now problem when printing array content (or number of elements in array), getting first element.
any suggestions?
script:
#!/bin/bash app=$1 process_data() { array="${!1}" echo "no of array elements: ${#array[@]}" echo "array content: ${array[@]}" } oradata=( "oraserver/content:abcdaily/oraserver/" "oraserver/w3s-ix86:abcdaily/oraserver/" ) sqldata=( "sqlserver/content:abcdaily/sqlserver/" "sqlserver/w3s-ix86:abcdaily/sqlserver/" ) process_data ${app[@]} command:
-bash-2.05b$ ./testarray.sh oradata output:
no of array elements: 1 array content: oraserver/content:abcdaily/oraserver/
here's version without eval (so handles shell metacharacters in array values properly):
#!/bin/bash app=$1 process_data() { arrayvar="$1[@]" # textually appends "[@]" $1 array=("${!arrayvar}") # use indirect expansion contents of array, store new array echo "no of array elements: ${#array[@]}" echo "array content: ${array[@]}" } oradata=( "oraserver/content:abcdaily/oraserver/" "oraserver/w3s-ix86:abcdaily/oraserver/" ) sqldata=( "sqlserver/content:abcdaily/sqlserver/" "sqlserver/w3s-ix86:abcdaily/sqlserver/" ) process_data $app # note app not array, [@] bit irrelevant example:
$ ./testarray.sh oradata no of array elements: 2 array content: oraserver/content:abcdaily/oraserver/ oraserver/w3s-ix86:abcdaily/oraserver/
Comments
Post a Comment