0

I try to plot several curves on a same plot using variables from a shell script. My problem is that I do not succeed in collecting the variables and use them in gnuplot.

Here is my code:

#!/bin/bash

for elem in AMD WALE SIM;
do
  echo $elem
  Utau_$elem=$(awk 'FNR==5{print $1}' file_$elem)
done
gnuplot -persist  <<-EOFMarker
list= "AMD WALE SIM"
plot for [i in list] "stat_".i u 1:($1/Utau_.i) @ title_i  
EOFMarker

This script aim at avoiding to write all the plot sentences but should have the same effect that the following :

 plot "stat_AMD" u 1:($1/Utau_AMD}) @title_AMD,\
"stat_WALE" u 1:($1/Utau_WALE}) @title_WALE,\
"stat_SIM" u 1:($1/Utau_SIM})  @title_SIM

I did not succeed in plotting anything and I am not sure that the link can be done between the value Utau_$elem for the bash script and Utau_.i from the gnuplot script. Does someone have an idea if it is possible and how can I code it ?

Thanks a lot !

Martin7
  • 93
  • 8
  • You didn't write how you invoke your script. Perhaps `$1` is empty? – user1934428 Aug 28 '20 at 12:35
  • What made you think that `Utau_$elem=` works the way you think? – U. Windl Aug 28 '20 at 13:35
  • @user1934428. I run my script on a terminal with ./file.sh. $1 refers to the first column of the files stat_AMD, stat_WALE and stat_SIM. It is not empty. – Martin7 Aug 28 '20 at 13:49
  • @U.Windl. Thank you for your answer. I did not really understand what do you mean. Utau_$elem=$(awk 'FNR==5{print $1}' file_$elem) should take the value of awk 'FNR==5{print $1}' file_$elem which is a decimal number – Martin7 Aug 28 '20 at 13:52
  • @Martin7 If I try (in bash): `N=1;X$N=2`, then I get `If 'X1=2' is not ...`. So it does not set variable `X1`, and I guess that is what you tried (in other words). – U. Windl Aug 28 '20 at 13:54
  • @U.Windl. Yes I would like to have Utau_AMD, Utau_WALE and Utau_AMD. How can I do this ? – Martin7 Aug 28 '20 at 14:04
  • for dynamic variable names take a look at these Q&As: [#1](https://stackoverflow.com/q/16553089/7366100), [#2](https://stackoverflow.com/q/9714902/7366100), [#3](https://unix.stackexchange.com/q/222487/234539); alternatively, consider using an array, eg, `Utau[${elem}]=$(awk ...); "${Utau[${i}]}"` – markp-fuso Aug 28 '20 at 14:42

1 Answers1

0

Thank @markp-fuso, it works !

I just had to replace "($1/${Utau_fr[${i}]})" by "(column(1)/${Utau_fr[${i}]})" because the dollar signs are interpreted as starting a shell variable.

Martin7
  • 93
  • 8