0

I am trying to run bash script.sh file in Python.
I tried running it through following code:

subprocess.call("script.sh", shell=True)

but it runs the script outside python and asks Windows application to run this file.

I want this script.sh to be run in Python command line.
I want Python to run this script.

Azam
  • 3
  • 5
  • How can Python run a shell script? – kindall Apr 24 '17 at 17:08
  • You are running on windows? This runs cmd.exe which runs your program. Set shell=False and it will run it directly... but either way, its got to call a program that understands sh syntax. That doesn't happen natively on windows so you need something like cygwin or gitbash. Alternately you can rewrite the script in python and run that. – tdelaney Apr 24 '17 at 17:13
  • Yes im on windows but im using python compiler pycharm and i have installed python for windows already.doesnt python itself supports bash? ^ – Azam Apr 24 '17 at 17:18
  • i want bash script outputs on pycharm commandline. thats it. – Azam Apr 24 '17 at 17:19
  • 1
    No, python itself knows nothing of bash. its a different syntax. – tdelaney Apr 24 '17 at 17:19
  • how does linux run it then?? Is there anyway that i can run this bash script inside python. installing some thirdparty app that support bash for windows?:s – Azam Apr 24 '17 at 17:20
  • [Cygwin](https://www.google.com/search?q=cygwin&ie=utf-8&oe=utf-8) or [WSL](https://en.wikipedia.org/wiki/Windows_Subsystem_for_Linux) – kindall Apr 24 '17 at 17:24
  • If you don't have a bash-like shell on windows yet, there are several choices. You may want to do some research. On windows 10 there is https://www.howtogeek.com/249966/how-to-install-and-use-the-linux-bash-shell-on-windows-10/. cygwin is popular https://www.cygwin.com/. Then there is git bash https://www.udemy.com/git-bash// – tdelaney Apr 24 '17 at 17:27
  • so i finally installed cygwin and it does works thanks @tdelaney – Azam Apr 24 '17 at 18:28
  • The duplicate apparently doesn't solve the actual problem you were attempting to resolve, but I'm hoping the duplicate will be useful for future visitors. – tripleee Apr 25 '17 at 04:31
  • @triplee im trying to access the commandline in python i did that using os.system("cmd.exe"). it showed me the commandline but at the starting it gives me these errors bash: cannot set terminal process group (-1): Inappropriate ioctl for device bash: no job control in this shell – Azam Apr 25 '17 at 09:41
  • This sounds like Windows craziness, rather than Bash or Python craziness. Your question sounds more and more like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) at every new turn. What are you *actually* trying to accomplish, and can you simplify the problem to one where coincidental problems are removed? See also how to create a [mcve]. – tripleee Apr 25 '17 at 10:55

1 Answers1

-1

Import the library OS and then use os.system:

import os

os.system('bash')
Unheilig
  • 16,196
  • 193
  • 68
  • 98
user39269
  • 44
  • 4
  • This does works but it asks for windows application to run the bash file. while what i am trying to do is run is bash file within pycharm command line. – Azam Apr 24 '17 at 17:16
  • that's right (I didn't read the whole question), this run in linux (I've no idea of windows), in linux I added the vte widget and then run scripts in my program: self.terminal.feed_child("sh test2\n",len("sh test2\n")) – user39269 Apr 25 '17 at 20:56