title: Extract the error message from the shell script using Python?
body: <p>I have a simple Python script which will execute a shell script using <code>subprocess</code> mdoule in Python.</p>
  
  <p>Below is my Python shell script which is calling <code>testing.sh</code> shell script and it works fine.</p>
  
  <pre><code>import os
  import json
  import subprocess
  
  jsonData = '{"pp": [0,3,5,7,9], "sp": [1,2,4,6,8]}'
  jj = json.loads(jsonData)
  
  print jj['pp']
  print jj['sp']
  
  os.putenv( 'jj1',  'Hello World 1')
  os.putenv( 'jj2',  'Hello World 2')
  os.putenv( 'jj3', ' '.join( str(v) for v in jj['pp']  ) )
  os.putenv( 'jj4', ' '.join( str(v) for v in jj['sp']  ) )
  
  print "start"
  proc = subprocess.Popen('testing.sh', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  (stdout, stderr) = proc.communicate()
  if stderr:
     print "Shell script gave some error"
  else:
     print "end" # Shell script ran fine.
  </code></pre>
  
  <p>And below is my <code>testing.sh</code> shell script - </p>
  
  <pre><code>#!/bin/bash
  
  for el1 in $jj3
  do
      echo "$el1"
  done
  
  for el2 in $jj4
  do
      echo "$el2"
  done
  
  for i in $( david ); do
      echo item: $i
  done
  </code></pre>
  
  <p>Now the question I have is - </p>
  
  <p>If you see my Python script, I am printing out <code>start</code> and then executing my shell script, if there are any errors in executing my shell script, then it won't print out <code>end</code>. In the above example, it won't print out <code>end</code> as there is an error.</p>
  
  <ol>
  <li>Firstly, So is this the right way to do this kind of problem?
  Basically, I want to print <code>start</code> first and if the shell script got
  executed successfully, then I want to print <code>end</code>. But if for
  whatever reason, my shell script doesn't got executed successfully,
  then I won't print out <code>end</code>.</li>
  <li>Secondly, with this approach, I cannot see my echo statement getting printed out on the console from the shell script? Does it mean, my shell script are getting executed but it is not getting printed out on the console somehow?</li>
  <li>Thirdly, Is there any possibility of extracting the error message from the shell script, meaning whatever was the reason of shell script getting failed?</li>
  </ol>