Monday, April 25, 2016

multiple shell commands in same process

http://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess


# Working multiple command in one process
# Write multiple commands with p.write: all will be executed in the same process by: p.close()
def test_bash():
    bash_path = runcmd('which bash').strip() # strip() used to trim line feed
    p = os.popen('%s' % bash_path, 'w')
    p.write('%s\n' % 'ls')
    p.write('%s\n' % 'ls -al')
    p.write('%s\n' % 'export hihi=\"no hi hi\"')
    p.write('%s\n' % 'echo =====' )
    p.write('%s\n' % 'echo $hihi')
    status = p.close()
    if status is None:
        print 'Commands executed OK'
    else:
        print 'One or more command failed to run'


No comments:

Post a Comment