简体   繁体   English

如何在 Windows 命令提示符下运行 .sh?

[英]How to run .sh on Windows Command Prompt?

How can I run .sh on Windows 7 Command Prompt?如何在 Windows 7 命令提示符上运行 .sh? I always get this error when I try to run this line in it,当我尝试在其中运行此行时,我总是收到此错误,

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,这里的屏幕抓取,在此处输入图片说明

Install GIT .安装git During installation of GIT, add GIT Bash to windows context menu by selecting its option.在安装 GIT 期间,通过选择其选项将 GIT Bash 添加到 Windows 上下文菜单。 After installation right click in your folder select GIT Bash Here (see attached pic) and use your sh command like for example:安装后右键单击您的文件夹选择GIT Bash Here (见附图)并使用您的 sh 命令,例如:

sh test.sh

在此处输入图片说明

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; Google 的热门搜索引擎是http://win-bash.sourceforge.net/,但您还需要了解大多数 Bash 脚本需要类 Unix 环境; 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.所以仅仅安装 Bash 可能不太可能让你运行你在网上找到的脚本,除非它是专门为这个特定的使用场景设计的。 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.通常的解决方案是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.第二条错误消息是由于 Windows 名义上接受正斜杠作为目录分隔符,但在这种情况下,它被解释为开关分隔符。 In other words, Windows parses your command line as app /build /build.sh (or, to paraphrase with Unix option conventions, app --build --build.sh ).换句话说,Windows 将您的命令行解析为app /build /build.sh (或者,用 Unix 选项约定来解释, app --build --build.sh )。 You could try app\\build\\build.sh but it is unlikely to work, because of the circumstances outlined above.您可以尝试app\\build\\build.sh但由于上述情况,它不太可能奏效。

The most common way to run a .sh file is using the sh command:运行.sh文件的最常见方法是使用sh命令:

C:\>sh my-script-test.sh 

other good option is installing CygWin另一个不错的选择是安装CygWin

in Windows the home is located in:Windowshome位于:

C:\cygwin64\home\[user]

for example i execute my my-script-test.sh file using the bash command as:例如,我使用bash命令执行我的my-script-test.sh文件,如下所示:

jorgesys@INT024P ~$ bash /home/[user]/my-script-test.sh 

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. Cmder 是一个软件包,纯粹出于对 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它基于令人惊叹的软件,并加入了 Monokai 配色方案和自定义提示布局,从一开始就看起来很性感

截屏

cmder.net管理员网

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 admin@192.168.1.1 < /mnt/c/ccode-eu.sh

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/

Personally I used this batch file, but it does require CygWin installed (64-bit as shown).我个人使用了这个批处理文件,但它确实需要安装 CygWin(如图所示的 64 位)。 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.只需将文件类型 .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 this original work.基于原创作品。

Install the GitBash tool in the Windows OS.在 Windows 操作系统中安装GitBash工具。 Set the below Path in the environment variables of System for the Git installation.在 System 的环境变量中设置以下Path用于 Git 安装。

<Program Files in C:\\>\\Git\\bin <C:\\>\\Git\\bin 中的程序文​​件

<Program Files in C:\\>\\Git\\usr\\bin <C:\\>\\Git\\usr\\bin 中的程序文​​件

Type ' sh ' in cmd window to redirect into Bourne shell and run your commands in terminal.cmd窗口中键入“ sh ”以重定向到 Bourne shell 并在终端中运行您的命令。

Have you tried cd ing to the root directory where your .sh is located in order to execute it from there, instead of writing down a path to the file as you showed in your question?您是否尝试过cd.sh所在的根目录以便从那里执行它,而不是像您在问题中显示的那样写下文件的路径?

Like so:像这样:

$ cd app/build
$ build.sh

我按照此处的指导在 Windows 10 中使用 Windows 10 Bash shell aka Linux Subsystem aka Ubuntu

只需安装 git 并通过“bash <name.sh>”运行您的 .sh 文件。

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

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