I have a script where i have defined two arrays. Now depending on the array name passed (as parameter), i want to process the same in the function below. Earlier, i wasn’t even getting any element in variable ‘ARRAY’ until i used ${!}
Now the problem is that when i am printing the array content (or the number of elements in that array), i am getting only the 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 a version without eval (so it handles shell metacharacters in the array values properly):
Example: