Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easily creating a bash script in python

Tags:

python

bash

I'm looking to write some tests that will create and execute a bash script. Bash itself has a nice way to do this:

 % cat > run.sh << EOF
 > echo "I ran this"
 > EOF
 % . run.sh
 I ran this

In Python I can do this:

 with open ('run.sh', 'w') as rsh:
    rsh.write('echo "I ran this"\n')
 -- etc ---

This is fine for a short script in Python, but I'm wondering if there is some technique I don't know about that let's me do something like what I can do in bash.

like image 990
Ray Salemi Avatar asked Oct 15 '25 15:10

Ray Salemi


1 Answers

You can do this

#! /usr/bin/env python

with open ('run.sh', 'w') as rsh:
    rsh.write('''\
#! /bin/bash
echo "I ran this"
echo "more lines"
''')
like image 147
Diego Torres Milano Avatar answered Oct 17 '25 06:10

Diego Torres Milano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!