Search code examples
bashshellbc

Bash script Error: (standard_in) 1: syntax error


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)
   }

}

Solution

  • 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;