1

What I want is to have a desktop shortcut that will run a script. I've been trying to use batch and Python, but I don't really care what language if it works. I need the script to open a bash shell (Windows Subsystem for Linux) and execute simple commands while keeping the shell open. At first, I thought this would simple, but now I'm questioning if it's even possible. I made a simple batch file that would get as far as opening bash and keep it open, but the test command I put in didn't make it to the shell. (I didn't really expect it to but I can't think of a good way to do this, so I've just been trying random stuff). Here is the batch file I used:

bash
cowsay test
PAUSE

After this, I tried using a Python script to open bash and run a shell script that would keep the shell open and execute commands. Here is the Python script:

import os
import time

os.system("start /wait bash /c {./test.sh}")
while 1:
    time.sleep(2) 

For some reason, this gives an error saying it can't find bash. This isn't really for a project or anything. It's actually for a friend's computer as kind of a joke, but I would really like to know if this is possible. If anyone has any ideas how this could work, I would appreciate it because I'm out of ideas and can't find any other similar questions.

3
  • Not sure what you mean, but try bash -c ‘cowsay test’ Commented Nov 21, 2017 at 18:58
  • 1
    WSL bash.exe is only a 64-bit build that's installed in the real System32. I assume your Python is 32-bit, so "System32" gets redirected to "SysWOW64". A WOW64 app (only a WOW64 app) can refer to the real "System32" directory as "SysNative", e.g. bash_path = os.path.join(os.environ['SystemRoot'], 'SysNative', 'bash.exe'). Commented Nov 22, 2017 at 8:17
  • You'll need to use subprocess.Popen, with stdin, stdout, and stderr redirected to pipes and two dedicated reader threads, one for stdout and the other for stderr. Do not use shell=True or a batch script; CMD doesn't need to be involved with this in any way. Commented Nov 22, 2017 at 8:21

1 Answer 1

1

Yes, you can do something like this

import subprocess

from subprocess import Popen
p = Popen("batch.bat", cwd=r"C:\Path\to\batchfolder")
stdout, stderr = p.communicate()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.