0

I’m in an embedded environment where/bin/shdoesn’t exist so when I callposix.system()or os.system()it always returns-1.
Because of that environment, thesubprocessmodule isn’t available. I can’t create pipes too.

I’m using ɴᴀᴄʟ as glibc.

Setting theSHELLenvironment variable doesn’t seems to work (it still tries to open/bin/sh).

So is there a way to change the shell process invoked byos.system() ?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user2284570
  • 2,891
  • 3
  • 26
  • 74

1 Answers1

5

No, you can't change what shell os.system() uses, because that function makes a call to the system() C function, and that function has the shell hardcoded to /bin/sh.

Use the subprocess.call() function instead, and set the executable argument to the shell you want to use:

subprocess.call("command", shell=True, executable='/bin/bash')

From the Popen() documentation (which underlies all subprocess functionality):

On Unix with shell=True, the shell defaults to /bin/sh. If args is a string, the string specifies the command to execute through the shell.

and

If shell=True, on Unix the executable argument specifies a replacement shell for the default /bin/sh.

If you can't use subprocess and you can't use pipes, you'd be limited to the os.spawn*() functions; set mode to os.P_WAIT to wait for the exit code:

retval = os.spawnl(os.P_WAIT, '/bin/bash', '-c', 'command')
ymonad
  • 11,710
  • 1
  • 38
  • 49
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • except that I can use the`subprocess`module. I forgot to tell I use this as my libc https://chromium.googlesource.com/native_client/nacl-glibc – user2284570 Aug 10 '16 at 16:15
  • @user2284570: then you are entirely out of luck. – Martijn Pieters Aug 10 '16 at 16:15
  • Except if there’s a pythonic way to issue system calls directly *(that is, without using a third party module or library)*. Is doing this impossible too ? – user2284570 Aug 10 '16 at 16:20
  • 1
    @user2284570: added the `os.spawn*()` functions. Lets hope for your sake those are implemented on your system. :-) – Martijn Pieters Aug 10 '16 at 16:27
  • @user2284570: then you really are out of options entirely. – Martijn Pieters Aug 10 '16 at 16:29
  • @user2284570: Thanks, but you don't need to point me to the Python documentation. Unless you are trying to tell me you are trying to run this on Google App Engine; I'd not even bother trying to get shell access on those servers. And the correct link in that case is the one for the [`posix` module](http://gae-pydoc.appspot.com/posix) instead. – Martijn Pieters Aug 10 '16 at 16:31