如何在 Windows 命令提示符下运行 .sh?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26522789/
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
How to run .sh on Windows Command Prompt?
提问by laukok
How can I run .sh on Windows 7 Command Prompt? I always get this error when I try to run this line in it,
如何在 Windows 7 命令提示符上运行 .sh?当我尝试在其中运行此行时,我总是收到此错误,
app/build/build.sh
error,
错误,
'app' is not recognized...
or,
或者,
bash app/build/build.sh
error,
错误,
'bash' is not recognized...
Any ideas what have I missed?
任何想法我错过了什么?
Here the screen grab,
这里的屏幕抓取,
采纳答案by tripleee
The error message indicates that you have not installed bash
, or it is not in your PATH
.
该错误消息表明您尚未安装bash
,或者它不在您的PATH
.
The top Google hit is http://win-bash.sourceforge.net/but you also need to understand that most Bash scripts expect a Unix-like environment; so just installing Bash is probably unlikely to allow you to run a script you found on the net, unless it was specifically designed for this particular usage scenario. The usual solution to that is https://www.cygwin.com/but there are many possible alternatives, depending on what exactly it is that you want to accomplish.
Google 的热门搜索引擎是http://win-bash.sourceforge.net/,但您还需要了解大多数 Bash 脚本需要类 Unix 环境;所以仅仅安装 Bash 可能不太可能让你运行你在网上找到的脚本,除非它是专门为这个特定的使用场景设计的。通常的解决方案是https://www.cygwin.com/,但有许多可能的替代方案,具体取决于您想要完成的目标。
If Windows is not central to your usage scenario, installing a free OS (perhaps virtualized) might be the simplest way forward.
如果 Windows 不是您使用场景的核心,安装免费操作系统(可能是虚拟化的)可能是最简单的方法。
The second error message is due to the fact that Windows nominally accepts forward slash as a directory separator, but in this context, it is being interpreted as a switch separator. In other words, Windows parses your command line as app /build /build.sh
(or, to paraphrase with Unix option conventions, app --build --build.sh
). You could try app\build\build.sh
but it is unlikely to work, because of the circumstances outlined above.
第二条错误消息是由于 Windows 名义上接受正斜杠作为目录分隔符,但在这种情况下,它被解释为开关分隔符。换句话说,Windows 将您的命令行解析为app /build /build.sh
(或者,用 Unix 选项约定来解释,app --build --build.sh
)。您可以尝试,app\build\build.sh
但由于上述情况,它不太可能奏效。
回答by Faisal Mq
Install GIT. During installation of GIT, add GIT Bash to windows context menu by selecting its option. After installation right click in your folder select GIT Bash Here
(see attached pic) and use your sh command like for example:
安装git。在安装 GIT 期间,通过选择其选项将 GIT Bash 添加到 Windows 上下文菜单。安装后右键单击您的文件夹选择GIT Bash Here
(见附图)并使用您的 sh 命令,例如:
sh test.sh
回答by Jorgesys
The most common way to run a .shfile is using the shcommand:
运行.sh文件的最常见方法是使用sh命令:
C:\>sh my-script-test.sh
other good option is installing CygWin
另一个不错的选择是安装CygWin
in Windows
the home
is located in:
在Windows
该home
位于:
C:\cygwin64\home\[user]
for example i execute my my-script-test.sh
file using the bashcommand as:
例如,我my-script-test.sh
使用bash命令执行我的文件:
jorgesys@INT024P ~$ bash /home/[user]/my-script-test.sh
回答by vipmaa
you can use also cmder
你也可以使用cmder
Cmder is a software package created out of pure frustration over the absence of nice console emulators on Windows. It is based on amazing software, and spiced up with the Monokai color scheme and a custom prompt layout, looking sexy from the start
Cmder 是一个软件包,纯粹出于对 Windows 上缺少漂亮的控制台模拟器的挫败感而创建的。它基于令人惊叹的软件,并加入了 Monokai 配色方案和自定义提示布局,从一开始就看起来很性感
回答by SignedAdam
On Windows 10 Anniversary Update, it's even easier to run shell commands in/with bash on ubuntu on windows
在 Windows 10 周年更新中,在 Windows 上的 ubuntu 中/使用 bash 运行 shell 命令更加容易
I was trying to set my region for my x-wrt r7000 netgear router, I found the following worked for me, using bash on ubuntu on windows, you do have to enable subsystem found in windows features, and dev mode on
我试图为我的 x-wrt r7000 netgear 路由器设置我的区域,我发现以下对我有用,在 windows 上的 ubuntu 上使用 bash,你必须启用在 windows 功能中找到的子系统,并打开开发模式
ssh [email protected] < /mnt/c/ccode-eu.sh
回答by Anonymouse
Personally I used this batch file, but it does require CygWin installed (64-bit as shown). Just associate the file type .SH with this batchfile (ExecSH.BAT in my case) and you can double-click on the .SH and it runs.
我个人使用了这个批处理文件,但它确实需要安装 CygWin(如图所示的 64 位)。只需将文件类型 .SH 与此批处理文件(在我的情况下为 ExecSH.BAT)相关联,您就可以双击 .SH 并运行它。
@echo off
setlocal
if not exist "%~dpn1.sh" echo Script "%~dpn1.sh" not found & goto :eof
set _CYGBIN=C:\cygwin64\bin
if not exist "%_CYGBIN%" echo Couldn't find Cygwin at "%_CYGBIN%" & goto :eof
:: Resolve ___.sh to /cygdrive based *nix path and store in %_CYGSCRIPT%
for /f "delims=" %%A in ('%_CYGBIN%\cygpath.exe "%~dpn1.sh"') do set _CYGSCRIPT=%%A
for /f "delims=" %%A in ('%_CYGBIN%\cygpath.exe "%CD%"') do set _CYGPATH=%%A
:: Throw away temporary env vars and invoke script, passing any args that were passed to us
endlocal & %_CYGBIN%\mintty.exe -e /bin/bash -l -c 'cd %_CYGPATH%; %_CYGSCRIPT% %*'
Based on thisoriginal work.
基于此原创作品。
回答by claudiu.nicola
New feature in Windows - run bash on ubuntu on windows - available in Windows 10 "Insiders" builds after the Build conference:
Windows 中的新功能 - 在 Windows 上的 ubuntu 上运行 bash - 在 Build 会议之后的 Windows 10“Insiders”版本中可用:
https://blogs.windows.com/buildingapps/2016/03/30/run-bash-on-ubuntu-on-windows/
https://blogs.windows.com/buildingapps/2016/03/30/run-bash-on-ubuntu-on-windows/