-1

I have a very simple bash script test.sh as shown below

#!/usr/bin/env bash
mkdir "/c/AAA"

I want to execute this code in python. When I call os.system(r"Y:\test.sh") in python, a window pops up and asks me which program I want to open the test.sh with. Then python will end with output 0 and no folder is created in my C drive. I can't find any solution online. Any help will be appreciated. :)

enter image description here

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Stack_Protégé
  • 302
  • 1
  • 15
  • What OS is this? `#!/usr/bin/env bash` looks like *nix, but `Y:\\ ` looks like windows... – Tom Dalton Oct 28 '19 at 17:26
  • @TomDalton Windows 10 if that's what you are asking – Stack_Protégé Oct 28 '19 at 17:27
  • 1
    What's the expected behaviour of `#!/usr/bin/env bash` in the context of Windows? – Tom Dalton Oct 28 '19 at 17:29
  • 2
    If that is the whole script, then even if you were able to start it (which isn't trivial on Windows), it wouldn't do anything useful. What are you really trying to do? Why don't you use python to create a directory? – zvone Oct 28 '19 at 17:31
  • `os.system()` uses *your current operating system's shell*. On Windows, your current operating system's shell doesn't know how to run bash scripts. Do you actually *have* any version of bash (from cygwin, mingw, or anything else) installed? – Charles Duffy Oct 28 '19 at 17:32
  • 1
    @TomDalton, nothing at all; Windows doesn't read or honor shebangs -- it uses a list of file extensions mapped to handlers in the registry instead. – Charles Duffy Oct 28 '19 at 17:32
  • @CharlesDuffy Ohh that's right. I use mingw. – Stack_Protégé Oct 28 '19 at 17:34
  • 1
    Is the mingw-provided bash executable in your Windows PATH? If so, you might be able to make it `os.system('bash test.sh')` (though please don't; even `subprocess.call(['bash', 'test.sh'])` is better). – Charles Duffy Oct 28 '19 at 17:36

2 Answers2

3

os.system() will invoke your command the same as windows cmd would, in this case, the windows doesn't know how to execute *.sh files, so it opens it's default dialog so you can pick one program that you know can ran it.

The same will happen if you open windows terminal and try to invoke such file.

If your windows have a bash interpreter try invoking it like this:

os.system("bash Y:\test.sh")
H_DANILO
  • 321
  • 1
  • 9
  • Incidentally, this is one of the other things that creates friction between people from UNIX and Windows traditions -- in the UNIX world, *executables aren't supposed to have extensions*, so only scripts built to be sourced rather than executed (and are POSIX-compliant, not bash scripts) should end in `.sh` (a bash script intended to be sourced would end in `.bash`, a ksh script in `.ksh`, etc). – Charles Duffy Oct 28 '19 at 17:40
  • (On UNIX, you run `ls`, not `ls.elf`; that way, even if the language or binary format changes, callers don't need to be updated to know the new name -- and you don't have someone try to use `sh` to run a script that needs `bash` if you stay away from the `.sh` reflexive naming). – Charles Duffy Oct 28 '19 at 17:42
  • Yep, very true. Windows still has a very long path on nailing down the terminal, and making the terminal experience better. – H_DANILO Oct 28 '19 at 18:14
0

Instead of running this with a native-Windows Python interpreter, run it with a Cygwin copy of Python, which has an os.system() that will be invoked with the Cygwin /bin/sh.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441