1

I'm executing a set of commands that first require me to call bash. I am trying to automate these commands by writing a Python script to do this. My first command obviously needs to be bash, so I run

p = subprocess.call(['bash'])

and it launches the bash shell no problem.

Where I then have problems is trying to execute the remaining code in the bash environment. I thought perhaps there was a need for process communication (i.e. redirecting stdout as in

p0 = subprocess.Popen(cmd, stdout=subprocess.PIPE)
p1 = subprocess.Popen(['bash'], stdin=p0.stdout)
p1.communicate()

) but the piping doesn't seem to solve my problem.

How can I write this script so that it mimics the following sequential Linux commands?

$ bash
$ cmd1
$ cmd2
...

I'm working with Ubuntu 14.04 and Python 2.7.6.

Thanks in advance for the guidance!

5
  • Are those commands related at all? Do they need to be run in the same shell session? Do they need to be run under an interactive bash session? Commented Oct 8, 2015 at 20:15
  • 1
    saltycrane.com/blog/2011/04/… may be of some help Commented Oct 8, 2015 at 20:15
  • @Busturdust I've actually already gone through that blog--but ah! I missed the executable parameter. That may work! Commented Oct 8, 2015 at 20:19
  • @EtanReisner: The commands are all related. In general, what I have to do is open a terminal, execute bash and then execute cmd1. Then open another terminal, execute bash and then execute cmd2, and so on. In other words, each command runs a process that must be running in bash when I launch the subsequent process Commented Oct 8, 2015 at 20:21
  • So then the answer to my question is "no" and manually running bash only matters if your normal shell isn't already bash I would imagine. In which case you probably just want to subprocess.call (or call_check or whatever the safer version of that function is) the command bash -c cmd (or just cmd if the manual bash isn't actually necessary). Commented Oct 8, 2015 at 20:24

2 Answers 2

1
import subprocess

def bash_command(cmd):
    subprocess.Popen(cmd, shell=True, executable='/bin/bash')

bash_command('[your_command]')
Sign up to request clarification or add additional context in comments.

1 Comment

This is what I was looking for. Thanks! From the docs: "If shell=True, on Unix the executable argument specifies a replacement shell for the default /bin/sh"
1

You don't need to call run bash separately. You can run something like:

p1 = subprocess.call(['cmd1'])
p2 = subprocess.call(['cmd2'])

If you must run bash for some reason (the commands contain bash statements, for example), you can run bash -c "cmd1; cmd2" from subprocess.call().

Edit: As Busturdust pointed out, you can also try setting shell=True, but that uses sh, not bash. But that may be enough for you.

5 Comments

In fact, I think the key is not the shell parameter, but the executable parameter. From the docs: "On Unix with shell=True, the shell defaults to /bin/sh" so changing the executable parameter changes the shell
@marcman: I meant the default. Changing the executable does change the shell, but I generally find it more straightforward to specify the shell directly as a part of the command - on the rare occasion I need to change it.
Gotcha. Is there a particular reason for that, or is it just a personal choice? That is, is that a standard convention?
More of a personal choice. I (and anyone else reading the code) can see exactly what command is being run without having to look at the documentation for what the executable parameter means. You can choose whatever you feel more comfortable with, of course.
I understand. Thanks for the help!

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.