0

I have the following alias set up in ~/.bashrc. It prints data from some sensors connected to a server. I'd like to condense this into a for loop, but cannot get the loop to run successfully. The non-loop syntax I have working is below. vd is an executable on this server that prints data. Here I have arguments to print 5 lines of data -n5 from a specific sensor -s ${scylpres[n]}, then filter the output so it only prints lines with the "," delimiter cut -d',' -s -f1-.

scylpres=($(eproms | grep -i 'trtl' | grep -i 'cylinder pressure' | cut -d',' -f3));
alias cylpres='vd -n5 -s ${scylpres[0]} | cut -d',' -s -f1- &&
vd -n5 -s ${scylpres[1]} | cut -d',' -s -f1- && vd -n5 -s ${scylpres[2]} | cut -d',' -s -f1- && vd -n5 -s ${scylpres[3]} | cut -d',' -s -f1- &&
vd -n5 -s ${scylpres[4]} | cut -d',' -s -f1- && vd -n5 -s ${scylpres[5]} | cut -d, -s -f1-';

I am struggling to wrap this into a for loop. I've tried:

i=0;
function cpres {
for i in {0..${scylpres[@]}}
do
vd -n5 -s ${scylpres${i}} | cut -d',' -s -f1-
done
};

And:

start=0;
stop=${scylpres[@]};
function cpres {
for((i=$start;i<=$end;i++))
do
vd -n5 -s ${scylpres[$((i++))]} | cut -d',' -s -f1-
done
};

And have tried different versions of the i call in both of these. For example, ${scylpres[$i]},${scylpres[$(i)]}, ${scylpres$[((i++))]} But, all I'm seeing is my command line go to > .

  • Have you tried using the "-x" and / or "-v" flags to get the shell to tell you what it is doing? – Stephen C Jul 24 '16 at 04:53
  • The `{1..10}` [brace expansion](https://www.gnu.org/software/bash/manual/bash.html#Brace-Expansion) notation is extremely limited and can't take a variable like you're trying to use — variables aren't expanded until long after brace expansion is complete. Maybe you need to use `seq` instead? – Jonathan Leffler Jul 24 '16 at 04:59
  • Thanks, all. shellcheck.net helped tremendously. Solution involved declaring scylpres as an array before it showed up in the loop. That, and using double quotes around variables : `scylpres=($(eproms | grep -i 'ecse' | grep -i 'cylinder pressure'| cut -d, -s -f3)); declare -a scylpres start=0; stop="${#scylpres[@]}"; function ecsecylpres { for((i=start;i<=stop;i++)) do vd -n5 -s "${scylpres[${i}]}" | cut -d',' -s -f1- done };` – rightangle987 Jul 25 '16 at 13:38

0 Answers0