0

I've got the following bash script:

#!/bin/bash
while read line
do
    ORD=`echo $line | cut -c 7-21`
    if [[ -r ../FASTA_SEC/${ORD}.fa ]]
    then
            WCR=`fgrep -o N ../FASTA_SEC/$ORD.fa | wc -l`
            WCT=`wc -m < ../FASTA_SEC/$ORD.fa`
            PER1=`echo print $WCR/$WCT.*100 | python`
            WCTRIN=`fgrep -o N ../FASTA_SEC_EDITED/$ORD"_Trimmed.fa" | wc -l`
            WCTRI=`wc -m < ../FASTA_SEC_EDITED/$ORD"_Trimmed.fa"`
            PER2=`echo print $WCTRIN/$WCTRI.*100 | python`
            PER3=`echo print $PER1-$PER2 | python`
            echo $ORD $PER1 $PER2 $PER3 >> Log.txt
        if [ $PER2 -ge 30 -a $PER3 -lt 10 ]
        then
            mv ../FASTA_SEC/$ORD.fa ./TRASH/$ORD.fa
            mv ../FASTA_SEC_EDITED/$ORD"_Trimmed.fa" ./TRASH/$ORD"_Trimmed.fa"
        fi
    fi
done < ../READ/Data.txt

$PER variables are floating numbers as u might have noticed so I cannot use them normaly in the nested if conditional. I'd like to do this conditional iteration in python but I have no clue how do it whithin a bash script also I dont know how to import the value of the variables $PER2 and $PER3 into python. Could I write directly python code in the same bash script invvoking python somehow?

Thank you for your help, first time facing this.

3
  • Why don't you rewrite the whole script with python? Currently it looks more complicated than it would in python, anyway. For operations like mv, cp, etc. python has a shutil module, so you do not need to change that much. Commented Jan 3, 2017 at 11:35
  • Uhm.. cuz im a newbie using python sincerely, I know some theory but its my first time practicing with this code. Im more familiar with bash scripting but floating limitations made me think about python so im doing my first steps now. I'm sure it would be a great training doing this whole part in python :> Commented Jan 3, 2017 at 12:52
  • If you want to stick to bash, there are tools like bc, dc and so on. But bash is a hell because of lot of escaping and so on, layers of quotes, which are passed on or not ... everything longer is better done with another scripting language. But that's a bit off topic here. Commented Jan 3, 2017 at 13:12

3 Answers 3

1

You can use python -c CMD to execute a piece of python code from the command line. If you want bash to interpolate your environment variables, you should use double quotes around CMD.

You can return a value by calling sys.exit, but keep in mind that true and false in Python have the reverse meaning in bash.

So your code would be:

if python -c "import sys; sys.exit(not($PER2 > 30 and $PER3 < 10 ))"
Sign up to request clarification or add additional context in comments.

4 Comments

I did not know true and false in python had teh reverse meaning in bash! that's why u are using ..(not(.. in the code provided, aight? imma try yr suggestion.
Yes, in Python True==1, in bash the exit code 0 means true.
works like a charm. I'll take a view deeper to fully understadn whats going on there. Thank you
An exit code of 0 means success; bash itself has no notion of Boolean logic (outside of arithmetic expressions, anyway).
0

It is possible to feed Python code to the standard input of python executable with the help of here document syntax:

variable=$(date)
python2.7 <<SCRIPT
print "The current date: %s" % "${variable}"
SCRIPT

In order to avoid parameter substitution (interpretation within the block), quote the first limit string: <<'SCRIPT'.

If you want to assign the output to a variable, use command substitution:

output=$(python2.7 <<SCRIPT
print "The current date: %s" % "${variable}"
SCRIPT
)

Note, it is not recommended to use back quotes for command substitution, as it is impossible to nest them, and the form $(...) is more readable.

Comments

0

maybe this helps?

$ X=4; Y=7; Z=$(python -c "print($X * $Y)")
$ echo $Z
28

python -c "str" takes "str" as input and runs it.

but then why not rewrite all in python? bash commands can nicely be executed with subprocess which is included in python or (need to install that) sh.

2 Comments

It seems that basicly if write python -c (python code) i can run this code easly into bash script. would you call this embed* python?
that could indeed be called 'embed' python.

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.