bash 如何在 Windows 上执行 .sh 文件?(学习Python)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32640903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:35:02  来源:igfitidea点击:

How Do I execute .sh files on Windows? (Learning Python)

pythonlinuxbashpowershell

提问by BananaRama

I am currently working through "Learn More Python the Hard Way" by Zed Shaw (continuation of learnpythonthehardway.org/book). In it, he creates a test .sh file that he uses throughout the series. I am on a Windows machine, and I understand bash commands are not compatible with Powershell so I'm wondering what command will allow me to run the file given that "bash" does not work.

我目前正在学习 Zed Shaw 的“Learn More Python the Hard Way”(learnpythonthehardway.org/book 的延续)。在其中,他创建了一个在整个系列中使用的测试 .sh 文件。我在 Windows 机器上,我知道 bash 命令与 Powershell 不兼容,所以我想知道什么命令可以让我运行文件,因为“bash”不起作用。

I also understand that .sh is associated with Linux, but usually he offers up the Windows version as well, yet this time he did not give a Windows-based explanation. The course also insists on using bash, otherwise I would have simple made the file with a .ps1 extension. I have also read about utilizing Cygwin, but I feel the instructor would have instructed us to do so if necessary.

我也明白.sh是和Linux有关的,不过一般他也会提供Windows版本的,但是这次他没有给出基于Windows的解释。该课程还坚持使用 bash,否则我会简单地制作带有 .ps1 扩展名的文件。我还阅读了有关使用 Cygwin 的文章,但我觉得讲师会在必要时指示我们这样做。

TL;DR: How does Zed Shaw intend for me to execute: bash tests/find_tests.shWhen I'm on a Windows machine and there are not hints to use the .ps1 extension or Cygwin?

TL;DR:Zed Shaw 打算如何让我执行: bash tests/find_tests.sh当我在 Windows 机器上并且没有使用 .ps1 扩展名或 Cygwin 的提示时?

回答by David Ferenczy Rogo?an

Well, it depends on what these scripts are doing.

嗯,这取决于这些脚本在做什么。

I think that the best option for you would be to use MSYS("Minimal SYStem"). It contains Bash.

我认为对您来说最好的选择是使用MSYS("Minimal SYStem")。它包含 Bash。

Cygwinis awesome and I'm using it every day, but it's probably too complex for what you need. It basically gives you Linux command-line environment integrated to the Windows system. You can install various compilers, interpreters and tons of other Linux tools, compiled particularly for Cygwin and distributed in Cygwin repositories.

Cygwin很棒,我每天都在使用它,但它可能太复杂了,无法满足您的需求。它基本上为您提供了集成到 Windows 系统的 Linux 命令行环境。您可以安装各种编译器、解释器和大量其他 Linux 工具,这些工具专门为 Cygwin 编译并分布在 Cygwin 存储库中。

MSYS is a part of MinWG("Minimalist GNU for Windows") and MinGW was originally forked from Cygwin. MSYS is just a subset of Cygwin/MinGW. I would say, that it will be easier for you to use MSYS.

MSYS 是MinWG(“Minimalist GNU for Windows”)的一部分,MinGW 最初是从 Cygwin 派生出来的。MSYS 只是 Cygwin/MinGW 的一个子集。我会说,您使用 MSYS 会更容易。

Install Cygwin if you plan to learn or further play with Linux shells and command-line tools. Or if you just want to have useful command-line environment on Windows.

如果您打算学习或进一步使用 Linux shell 和命令行工具,请安装 Cygwin。或者,如果您只想在 Windows 上拥有有用的命令行环境。

回答by asdf

Windows does not have the ability to run bash scripts built in by default. What you'll need to do is install Cygwinor something similar to handle execution of bash scripts.

默认情况下,Windows 无法运行内置的 bash 脚本。您需要做的是安装Cygwin或类似的东西来处理 bash 脚本的执行。

Once you have Cygwininstalled you'll have access to most unix commands, including sh.

一旦你已经Cygwin安装了,你将有机会获得大多数UNIX命令,包括sh

It is a little bit of overhead to install, but if you plan on using a Windows machine to code from here on out it is highly beneficial to have.

安装会有一点开销,但是如果您打算从现在开始使用 Windows 机器进行编码,那么拥有它是非常有益的。