[Answer]-Running series of interactive shell commands in bash/python/perl script

1👍

You need a script like this one:

#!/bin/bash

# kill all foo.bar's instances
for pid in $(ps -aux | grep foo.bar | grep -v grep | awk '{print $2;}'); do
    kill $pid
done

# start virtualenv
cd {required_folder}
...

# Start the manage.py in shell mode
cd {required folder}
cat << EOF | sudo python manage.py shell
from core import *
foo.bar.bz.clear.state()
exit
EOF

# Execute a script 
/baz/maz/foo

The key point of the script is HEREDOC python snippet. Take a look at the example I’ve just tried in a console:

[alex@galene ~]$ cat <<EOF_MARK | python -
> import sys
> print "Hello, world from python %s" % sys.version
> exit
> EOF_MARK
Hello, world from python 2.7.6 (default, Nov 22 2013, 22:57:56)
[GCC 4.7.2 20121109 (ALT Linux 4.7.2-alt7)]
[alex@galene ~]$ _

Leave a comment