简体   繁体   English

如何从mac执行bat? 是否可以将其重写为 python 或 bash 脚本?

[英]How execute bat from mac? Is it possible to rewrite it to python or bash script?

I have a problem with.bat file.我对 .bat 文件有疑问。 There is a.bat file that starts the server.有一个启动服务器的.bat 文件。 But on a Mac OS system, this is impossible.但在 Mac OS 系统上,这是不可能的。 Are there any options to rewrite it to python or bash so that it would be possible to start from MacBook?是否有任何选项可以将其重写为 python 或 bash 以便可以从 MacBook 开始?

This is.bat file:这是.bat 文件:

echo start web server..
start cmd /k node webServer.js
echo start chrome..
start chrome.exe /k http://localhost:8080

Thanks for helping!感谢您的帮助!

Ok, so it would be better to use bash scripts.好的,所以最好使用 bash 脚本。 They are much more powerful than bat and they work on all Unix like OS-s (Linux, Mac..) and can work on windows with some modifications.它们比 bat 功能强大得多,它们可以在所有 Unix 上工作,如 OS-s(Linux、Mac..),并且可以在 windows 上进行一些修改。 This will show you how to run node:这将向您展示如何运行节点:

Running node from a bash script 从 bash 脚本运行节点

This will show you how to run the app:这将向您展示如何运行该应用程序:

https://askubuntu.com/questions/682913/how-to-write-shell-script-to-start-some-programs https://askubuntu.com/questions/682913/how-to-write-shell-script-to-start-some-programs

Also, look at this link for an introduction to bash, it is a good thing to know it:另外,看看这个链接对bash的介绍,知道它是件好事:

https://linuxconfig.org/bash-scripting-tutorial-for-beginners https://linuxconfig.org/bash-scripting-tutorial-for-beginners

Also on the https://www.mac-forums.com/forums/switcher-hangout/302162-execute-bat-file-mac.html you can see how to run it on mac but as they pointed out there it doesn't work 100%.同样在https://www.mac-forums.com/forums/switcher-hangout/302162-execute-bat-file-mac.html上,您可以看到如何在 mac 上运行它,但正如他们指出的那样,它没有不能 100% 工作。

Edit1: This is the code:编辑1:这是代码:

#!/bin/bash

echo "Star server .."
node webServer.js
echo "Open chrome"
open http://localhost:8080

For node just add the path to file, like you would usually run it.对于节点,只需添加文件路径,就像您通常运行它一样。 For last line it opens default browser with link.对于最后一行,它会打开带有链接的默认浏览器。

Here's a Python example that is cross-platform (unless you don't have node in PATH ) and using only the standard library:这是一个跨平台的 Python 示例(除非您在PATH中没有node )并且仅使用标准库:

# client.py

import subprocess
import webbrowser


if __name__ == '__main__':
    try:
        server_proc = subprocess.Popen(['node', 'webServer.js'])
        webbrowser.open('http://localhost:8080')
        server_proc.communicate()
    except KeyboardInterrupt:
        server_proc.terminate()

Note, however, that webbrowser.open will open the browser that is set as default, so it could be Safari or whatever.但是请注意, webbrowser.open将打开设置为默认浏览器的浏览器,因此它可能是 Safari 或其他。 If you want to specifically open Chrome, you will have to pass the full path to the executable (or modify your PATH env var).如果要专门打开 Chrome,则必须将完整路径传递给可执行文件(或修改PATH环境变量)。 Example:例子:

# client.py

import os
import subprocess


if __name__ == '__main__':
    try:
        server_proc = subprocess.Popen(['node', 'webServer.js'])
        chrome_exe = os.path.join('/', 'Applications', 'Google Chrome.app', 'Contents', 'MacOS', 'Google Chrome')
        subprocess.Popen([chrome_exe, 'http://localhost:8080'])
        server_proc.communicate()
    except KeyboardInterrupt:
        server_proc.terminate()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM