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

}
2
  • 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). Commented Oct 29, 2020 at 19:29
  • @CharlesDuffy Thank you for pointing out the error and your advice! Commented Oct 29, 2020 at 21:49

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.