从 Windows PowerShell 运行 bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1098786/
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
Run bash script from Windows PowerShell
提问by wsorenson
In cygwin, I could just do ./script.sh args, but this opens the script file in notepad in PowerShell.
在 cygwin 中,我可以只执行 ./script.sh args,但这会在 PowerShell 的记事本中打开脚本文件。
What do I need to do have it execute?
我需要做什么才能让它执行?
采纳答案by TheBonsai
You should put the script as argument for a *NIX shell you run, equivalent to the *NIXish
你应该把脚本作为你运行的 *NIX shell 的参数,相当于 *NIXish
sh myscriptfile
回答by Marius Tancredi
There is now a "native" solution on Windows 10, after enabling Bash on Windows, you can enter Bash shell by typing bash
:
现在在 Windows 10 上有一个“原生”解决方案,在 Windows 上启用 Bash 后,您可以输入以下命令进入 Bash shell bash
:
You can run Bash script like bash ./script.sh
, but keep in mind that C drive is located at /mnt/c
, and external hard drives are not mountable. So you might need to change your script a bit so it is compatible to Windows.
您可以像 一样运行 Bash 脚本bash ./script.sh
,但请记住,C 驱动器位于/mnt/c
,并且无法安装外部硬盘驱动器。因此,您可能需要稍微更改脚本以使其与 Windows 兼容。
Also, even as root
, you can still get permission denied when moving files around in /mnt
, but you have your full root
power in the /
file system.
此外,即使root
在 中移动文件时,您仍然可以获得权限被拒绝/mnt
,但您root
在/
文件系统中拥有全部权力。
Also make sure your shell script is formatted with Unix style, or there can be errors.
回答by Rynant
If you add the extension .SH
to the environment variable PATHEXT
, you will be able to run shell scripts from PowerShell by only using the script name with arguments:
如果您将扩展添加.SH
到环境变量PATHEXT
,您将能够仅使用带参数的脚本名称从 PowerShell 运行 shell 脚本:
PS> .\script.sh args
If you store your scripts in a directory that is included in your PATH environment variable, you can run it from anywhere, and omit the extension and path:
如果将脚本存储在包含在 PATH 环境变量中的目录中,则可以从任何地方运行它,并省略扩展名和路径:
PS> script args
Note: sh.exe or another *nix shell must be associated with the .sh extension.
注意:sh.exe 或其他 *nix shell 必须与 .sh 扩展名关联。
回答by GangaRam Dewasi
It also can be run by exporting the bash and sh
of the gitbash C:\Program Files\git\bin\
in the Advance section of the environment variable of the Windows Server.
也可以通过导出Windows Server 环境变量的 Advance 部分中的bash and sh
gitbash来运行C:\Program Files\git\bin\
。
In Advance section in the path var kindly add the C:\Program Files\git\bin\
which will make the bash
and the sh
of the git-bash to be executable from the window cmd
.
在路径VAR部前进亲切添加C:\Program Files\git\bin\
这将使bash
与sh
该混帐的bash的是从可执行文件window cmd
。
Then,
然后,
Run the shell file as
运行 shell 文件作为
bash shellscript.sh
or sh shellscript.sh
bash shellscript.sh
或者 sh shellscript.sh