1

My bash script is the following

#!/bin/bash
wc=(5000 10000 20000 50000);
for r2 in {0.01 0.1 0.4 0.7 0.95};
do
  for i in {0..3};
  do

    rr=$(echo "${r2} * 100" | bc)
    ww=$(echo "${wc[$i]} / ${rr}" | bc)
    echo $ww

  done
done

However, I get the error message (standard_in) 1: syntax error.

Do you have any idea to address this issue? Thank you in advance!

The equivalent R code is the following

wc <- c(50,100,200,500)
for(k in c(0.01,0.1,0.4,0.7,0.95)){
   for(i in 1:4){
      
       ww <- floor(wc[i]/k)
       print(ww)
   }

}
cheng
  • 85
  • 1
  • 11
  • 1
    FWIW, this is a `bc` error, not a bash error -- different languages with different syntax. If you want to know what operations bash is telling `bc` to do, using `set -x` to enable tracing would be a good place to start, and would make the bug very clear (you'd see bash telling bc to calculate with `{0.01` as a number, with the `{` as part of its value, f/e). – Charles Duffy Oct 29 '20 at 19:29
  • @CharlesDuffy Thank you for pointing out the error and your advice! – cheng Oct 29 '20 at 21:49

1 Answers1

1

You need commas in the brace expression:

for r2 in {0.01,0.1,0.4,0.7,0.95};

Otherwise, you are simply iterating over the sequence {0.01, 0.1, ..., 0.95}.

There's really no need for a brace expression anyway:

for r2 in 0.01 0.1 0.4 0.7 0.95;
chepner
  • 497,756
  • 71
  • 530
  • 681