os.system command to call shell script with arguments

13.1k Views Asked by At

I am writing a python script which uses os.system command to call a shell script.I need help understanding how I can pass the arguments to the shell script? Below is what I am trying..but it wouldn't work.

os.system('./script.sh arg1 arg2 arg3')

I do not want to use subprocess for calling the shell script. Any help is appreciated.

2

There are 2 best solutions below

0
On

If you insert the following line before os.system (...), you will likely see your problem.

print './script.sh arg1 arg2 arg3'

When developing this type of thing, it usually is useful to make sure the command really is what you expect, before you actually try it.

example:

def Cmd():
    return "something"

print Cmd()

when you are satisfied, comment out the print Cmd() line and use os.system (Cmd ()) or subprocess version.

0
On

Place your script and it's args into a string, see example below.

HTH

#!/usr/bin/env python

import os

arg3 = 'arg3'
cmd = '/bin/echo arg1 arg2 %s' % arg3

print 'running "%s"' % cmd

os.system(cmd)