0

I'm pretty new to python and I'm trying to embed it in a bash script. I have the following bash shell script:

#!/bin/bash

while read line
do
    ORD=`echo $line | cut -c 1-7`
    if [[ -r ../FASTA_SEC/COMBI_RAW/${ORD}_COMBI_RAW.fa ]]
    then
        touch ../Results/Log_Files/Log_${ORD}.txt
        for (( win = 2; win < 20; win += 2 )); do
            printf 'The value of win is %d, now entereing inner loop\n' "$win"
            for (( per = 50; per < 100; per += 2 )); do
                printf 'Value of per is %d\n' "$per"
                WCNO=`grep -o "n\|N" ../FASTA_SEC/COMBI_RAW/${ORD}_COMBI_RAW.fa | wc -l`
                WCNF=`grep -o "n\|N" ../Results/COMBI/${ORD}_COMBI/${ORD}_w${win}_p${per}_COMBI.fa | wc -l`
                WCAO=`grep -o "a\|A\|g\|G\|t\|T\|c\|C" ../FASTA_SEC/COMBI_RAW/${ORD}_COMBI_RAW.fa | wc -l`
                WCAF=`grep -o "a\|A\|g\|G\|t\|T\|c\|C" ../Results/COMBI/${ORD}_COMBI/${ORD}_w${win}_p${per}_COMBI.fa | wc -l`
                PERN=`echo print ($WCNO-$WCNF)/$WCNO. | python`
                echo $ORD $PERN
            done
        done
    else
        echo ${ORD}"_COMBI_RAW.fa does not exist"
    fi
done < ./Data_ORD.txt

I would like to carry out this percentage calculation stored in a variable calling python

PERN=`echo print ($WCNO-$WCNF)/$WCNO. | python` 

Parenthesis are giving trouble so i get the following output when i run the bash script:

command substitution: line 17: syntax error near unexpected token `('
command substitution: line 17: `echo print ($WCNO-$WCNF)/$WCNO. | python'

Is it something wrong with bash? operation order are defined with parenthesis in python as far as I know.

Thank you,

1 Answer 1

1

bash is interpreting the ( when you don't want it to. You need to enclose the expression you're sending to python in quotes.

PERN=`echo "print ($WCNO-$WCNF)/$WCNO." | python`

If you're curious what bash thought you were doing...

The construct (...) indicates something to run inside a subshell. First, bash would interpolate the variables $WCNO and $WCNF. Then it would try to run the thing in the parens as a command, in a subshell. Unless that string coincidentally exists as a command, bash will complain because the command it tried to find does not exist.

See also this answer on Unix.SE.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Nice explanation

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.