How to include python script inside a bash script

I need to include below python script inside a bash script.

If the bash script end success, I need to execute the below script:

#!/usr/bin/python    
from smtplib import SMTP
import datetime
debuglevel = 0

smtp = SMTP()
smtp.set_debuglevel(debuglevel)
smtp.connect('192.168.75.1', 25)
smtp.login('my_mail', 'mail_passwd')

from_addr = "My Name <<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f322600323e36331f6e666d716e696771686a716e">[email protected]</a>>"
to_addr = "<<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="91fce8cefcf0f8fdd1a0a8a3bfa0a7a9bfa6a4bfa0">[email protected]</a>"
subj = "Process completed"
date = datetime.datetime.now().strftime( "%d/%m/%Y %H:%M" )
#print (date)
message_text = "Hai..nnThe process completed."

msg = "From: %snTo: %snSubject: %snDate: %snn%s" % ( from_addr, to_addr, subj, date, message_text )

smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Just pass a HereDoc to python -.

From python help python -h:

- : program read from stdin

#!/bin/bash

MYSTRING="Do something in bash"
echo $MYSTRING

python - << EOF
myPyString = "Do something on python"
print myPyString

EOF

echo "Back to bash"

Method 2

You can use heredoc if you want to keep the source of both bash and python scripts together. For example, say the following are the contents of a file called pyinbash.sh:

#!/bin/bash

echo "Executing a bash statement"
export bashvar=100

cat << EOF > pyscript.py
#!/usr/bin/python
import subprocess

print 'Hello python'
subprocess.call(["echo","$bashvar"])

EOF

chmod 755 pyscript.py

./pyscript.py

Now running the pyinbash.sh will yield:

$ chmod 755 pyinbash.sh
$ ./pyinbash.sh
Exe

Method 3

The simplest approach is to just save the python script as, for example script.py and then either call it from the bash script, or call it after the bash script:

#!/usr/bin/env bash
echo "This is the bash script" &&
/path/to/script.py

Or

script.sh && script.py

Method 4

This is old question, but maybe useful for someone.
It’s a way to include Python script inside a Bash script and use sys.stdin.

Extract Python script and run it with -c. The trick is to use a function, that allows use ' and " in the script. Also sys.stdin is available.

#!/bin/bash

read_file()
{
  local name="${1//./[.]}"  # escape name for sed regex
  sed -En '/^#---=== '"$name"' ===---$/,$ {/^#---=== '"$name"' ===---$/ n; /^#---===/ q; p; }' "$0"
}

echo Johny | python3 -c "$(read_file script.py)"
exit

#---=== script.py ===---
import sys
print('Your name is', sys.stdin.readline().strip())
#---===---

Method 5

How about this for an example:

PYTHON_BIN=/usr/bin/python
if [ -x $PYTHON_BIN ]; then
$PYTHON_BIN -c "print 'Hello, world'"
else
echo 'Hello, world'
fi

VS

$ ./foobar.py
env: python: No such file or directory

Method 6

#/bin/bash
python3 -c "$(cat << EOF

a = input('?>')
print('you typed', a)
print('33[1;32mbye...33[m')

EOF
)

This works. Th $() pass the output of the command inside (in this case cat) as the argument to python. There is no pipelining so std input can be used in the script.

this also works:

#/bin/bash

python3 -c "
a = input('?>') 
print('you typed', a)
print('33[1;32mbye...33[m')"

only that " cant be used in the script. If you needed it you should escape the " like this:

#/bin/bash

python3 -c "
a = input('?>') 
print("you typed", a)
print("33[1;32mbye...33[m")"

Method 7

I know this post is old, but I thought that I would share my code that is a working example.

#!/bin/bash
printf "This is BASHn"
printf "Please enter some text: "; read ans
export ans

cat << EOF > pyscript.py
#!/usr/bin/python3 -tt
import subprocess

print('............This is Python')
subprocess.call(["echo","............$ans"])
print('............Done with Python')

EOF

chmod 770 pyscript.py

./pyscript.py

printf "This is BASH againn"
exit 0


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x