1

How to get the array name from the below ?

Getting the name of the array from config :

jobcfgUniqName=`echo ${config_data} | awk -F "#" '{print $3}'`

Creating an array of it :

for ((xx = 0; xx <= ${#joblognameSearch[@]}; xx++))
do
    print $joblognameSearch[$xx]
    eval ($jobcfgUniqName)[$xx]=`grep -B 3 -i  error  $joblogPath/$joblognameSearch[$xx]`
    print jobcfgUniqName : ${jobcfgUniqName}
done

This line I tried changing many ways but did not work :

eval ($jobcfgUniqName)[$xx]
2
  • Its not clear what are you trying to do. There are ways to do this but eval is dangerous. Commented Feb 7, 2014 at 6:35
  • I am trying to get the array name from a configuration file and use it to store the error from the logs . I will be reading a lot of different logs so dont want to fetch everything in the same array Commented Feb 7, 2014 at 6:39

2 Answers 2

1

You can use declare bulletin of BASH to replace your eval by this:

declare arr_"$jobcfgUniqName"[$xx]=`grep -B 3 -i error $joblogPath/$joblognameSearch[$xx]`

Now you will have dynamic array create with prefix arr_ and some variable name $jobcfgUniqName.

TESTING:

# set the array
s='abc'
declare arr_"$s"[0]='foo'
declare arr_"$s"[1]='bar'

# retrieve the values
v1=arr_"$s"[0]
v2=arr_"$s"[1]

echo "${!v1}"
foo
echo "${!v2}"
bar
Sign up to request clarification or add additional context in comments.

Comments

1

Add echo.

Example:

#!/bin/bash
A="abcd dcba"
B=A
C='eval "echo \$$B"'
eval "$C"

$ bash 1.sh
abcd dcba

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.