1

I want to save each output filepath to a variable and then grep through them to find the timestamp. I want to lable each variable by adding the nodeId from the node list I am looping through. When I try this with the following code I get an error

output1_1: command not found

nodeList=('1_1' '1_6' '2_1' '2_6')
for i in "${nodeList[@]}"
do
   output${i}=$CWD/output/abc${i}.txt
   times${i}=$(grep  -m 1 '\"path\":' $output${i}| sed 's/.*timestampUtc\"://g' | sed 's/,.*//g')
done
Rakey
  • 11
  • 3

2 Answers2

0

As @muru suggested, try

declare -A output times
nodeList=('1_1' '1_6' '2_1' '2_6')
for i in "${nodeList[@]}"; do
   output[${i}]=${PWD}/output/abc${i}.txt
   times[${i}]=$(grep  -m 1 '\"path\":' ${output[${i}]} | sed 's/.*timestampUtc\"://g' | sed 's/,.*//g')
done
JGK
  • 3,710
  • 1
  • 21
  • 26
  • I am working with bash 3 so unfortunately the terminal wont allow me to do that – Rakey Sep 12 '18 at 13:38
  • Have a look at [Create associative array in bash 3](https://stackoverflow.com/questions/11776468/create-associative-array-in-bash-3) – JGK Sep 12 '18 at 13:57
0

One way to set a variable whose name is derived from another variable is to use the -v option to 'printf'. To get the value of a variable whose name is in another variable (e.g. varname) use ${!varname}.

See Creating a string variable name from the value of another string and How can I generate new variable names on the fly in a shell script?.

Using these a possible loop body is:

output_var=output${i}
times_var=times${i}

printf -v "$output_var" '%s' "$CWD/output/abc${i}.txt"
printf -v "$times_var" '%s' "$(grep  -m 1 '\"path\":' "${!output_var}" | sed 's/.*timestampUtc\"://g' | sed 's/,.*//g')"

Note that (unless CWD is set elsewhere in your program) you probably want $PWD instead of $CWD.

pjh
  • 6,388
  • 2
  • 16
  • 17