Dec-07-2016, 08:10 PM
"""#!/c/Anaconda2/Scripts/python"""
"""
Create a function called
representing a Linux shell command to be ran.
The function must:
1. Execute the shell command, as if it was done on the console command line
2. The stdout/stderr outputs of the shell command should go to stdout/stderr
of the Python program that calls the function.
3. Return True if the execution was successful (returncode 0), False otherwise
4. Have the following documentation:
Execute an external shell command showing its output on stdout
For example:
exec_external('echo foo') -> True
# Console output # foo
exec_external('cat nonexisting.file') -> False
# Console output # cat: nonexisting.file: No such file or directory
"""
"""
Create a function called
exec_external
, taking a single string argument,representing a Linux shell command to be ran.
The function must:
1. Execute the shell command, as if it was done on the console command line
2. The stdout/stderr outputs of the shell command should go to stdout/stderr
of the Python program that calls the function.
3. Return True if the execution was successful (returncode 0), False otherwise
4. Have the following documentation:
Execute an external shell command showing its output on stdout
For example:
exec_external('echo foo') -> True
# Console output # foo
exec_external('cat nonexisting.file') -> False
# Console output # cat: nonexisting.file: No such file or directory
"""
import subprocess from subprocess import Popen import os def exec_external(arg): """Execute an external shell command showing its output on stdout""" """ar = list(arg)""" proc = Popen(arg) proc.wait() n = proc.returncode if ( n == 0 ): print ("-> True\n # Console output #") else: print ("-> False\n # Console output #") arg = 'echo foo' """arg = 'cat fo.txt'""" exec_external(arg)This is printing the following output...
$ python exec_external.py foo -> True # Console output #I want just this output...
$ python exec_external.py -> True # Console output #fooIsnt there something similar to '$?' in bash in python, if so what is it.