Reputation: 2361
In the following code snippet, how do I find the exit code of make? Specifically, I need to know if make has failed or succeeded. Thanks for any inputs.
process = pexpect.spawn("/bin/bash")
process.expect("make\r")
Upvotes: 9
Views: 8445
Reputation: 844
I had to use pexpect in my latest project and wanted to get the exit code, couldn't find the solution easily, as this is the top result in google I'm adding my solution to this.
process = pexpect.spawn(command, cwd=work_dir)
process.expect(pexpect.EOF)
output = process.before
process.close()
exit_code = process.exitstatus
I have the output saved as well, because I am running bash scripts and the exit code is saved in the exit_code variable.
Upvotes: 10
Reputation: 40390
pexpect doesn't know about the make
command - it is just sending text to bash. So you need to use bash's mechanism for determining exit code - the value of $?
. So you want something like this:
process.sendline("make") # Note: issue commands with send, not expect
process.expect(prompt)
process.sendline("echo $?")
process.expect(prompt)
exitcode = process.before.strip()
Upvotes: 8