bash Cygwin 从一个批处理文件运行脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8283668/
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-09 21:14:46  来源:igfitidea点击:

Cygwin running script from one batch file?

bashbatch-filecygwin

提问by Aaron

I know when Cygwin installs there is a batch file that is used to launch it in Windows. But if I want to run a script without launching Cygwin and doing "./script"

我知道 Cygwin 安装时有一个批处理文件用于在 Windows 中启动它。但是如果我想在不启动 Cygwin 并执行“./script”的情况下运行脚本

How do I create a batch file to do that?

我如何创建一个批处理文件来做到这一点?

As in... just have one batch file that when clicked, opens Cygwin and runs the shell script.

就像...只要有一个批处理文件,当点击它时,打开 Cygwin 并运行 shell 脚本。

回答by Jakob Buron

To run "foo.bsh", create a batch file with the following contents and run it:

要运行“foo.bsh”,请创建一个包含以下内容的批处理文件并运行它:

    set PATH=C:\cygwin\bin;%PATH%
    c:\cygwin\bin\bash.exe c:\path\to\foo.bsh

The "C:\cygwin\bin" part can differ depending on your chosen install location of cygwin.

"C:\cygwin\bin" 部分可能因您选择的 cygwin 安装位置而异。

回答by Austin Hastings

You might try something like this to trampoline from CMD to BASH (or whatever sh-variant):

你可以尝试这样的东西从 CMD 蹦床到 BASH(或任何 sh 变体):

    : <<TRAMPOLINE
    @echo off
    bash -c "exit 0" || (echo.No bash found in PATH! & exit /b 1)
    bash "%~f0" "%*" 
    goto :EOF 
    TRAMPOLINE
    #####################
    #!/bin/bash  -- it's traditional!
    echo "Hello from $SHELL"

回答by galmok

I'd like to expand on the answer given by Austin Hastings to allow it to work without Cygwin in the path and to work in the directory setup with the bat file (so even a shortcut with a different "Start in " path is used). The changes are to include full path to bash, use option -lto make bash setup properly with paths and environment and to pass the current directory to bash as the first argument which is popped of (using shift). Remember to use Unix newlines in the file or you will get lots of errors. Also use UTF8 instead of ANSI.

我想扩展 Austin Hastings 给出的答案,以允许它在路径中没有 Cygwin 的情况下工作,并在使用 bat 文件的目录设置中工作(因此即使使用具有不同“开始于”路径的快捷方式) . 更改包括 bash 的完整路径,使用选项-l使 bash 正确设置路径和环境,并将当前目录传递给 bash 作为弹出的第一个参数(使用shift)。记住在文件中使用 Unix 换行符,否则你会得到很多错误。也使用 UTF8 而不是 ANSI。

: <<TRAMPOLINE
@echo off
C:\cygwin\bin\bash -c "exit 0" || (echo.No bash found in PATH! & exit /b 1)
setlocal EnableDelayedExpansion
for %%i in (%*) do set _args= !_args! "%%~i"
C:\cygwin\bin\bash -l "%~f0" "%CD%" %_args%
goto :EOF 
TRAMPOLINE
#####################
#!/bin/bash  -- it's traditional!
cd ""
shift
echo Working from $PWD

Edit: Fixed the trampoline code to pass arguments properly to bash. BAT files do not have a bash equivalent of "$@" and a loop processing each argument is made and then passed to bash.exe. Now properly handles arguments with spaces.

编辑:修复了蹦床代码以正确地将参数传递给 bash。BAT 文件没有“$@”的 bash 等价物,并且循环处理每个参数,然后传递给 bash.exe。现在可以正确处理带空格的参数。

回答by Daniel Haley

What kind of shell script? Bash? If you need to run a bash script, just run it with bash.exe via a shortcut. The target would be something like:

什么样的shell脚本?重击?如果您需要运行 bash 脚本,只需通过快捷方式使用 bash.exe 运行它。目标将是这样的:

C:\cygwin\bin\bash.exe foo.bsh

The path to bash.exe will depend on where cygwin was installed. (You don't need the path at all if the bin directory is in your path environment variable.)

bash.exe 的路径取决于 cygwin 的安装位置。(如果 bin 目录在您的路径环境变量中,您根本不需要路径。)

You can do the same thing in a batch script if you want. The command line would be the same as "target" above.

如果需要,您可以在批处理脚本中执行相同的操作。命令行与上面的“目标”相同。



EDIT BASED ON COMMENTS(still assuming bash):

基于评论编辑(仍然假设 bash):

Ok, so in your batch script (.bat) just type:

好的,所以在您的批处理脚本 (.bat) 中只需键入:

bash.exe script.txt

You may need to add the path to "bash.exe". If you're not sure where "bash.exe" is, open the "Cygwin.bat" file you mentioned in your question. It should have the path in a chdircommand. (Note: Cygwin.bat is just running bash.exe in interactive mode. It's not really running anything named "cygwin".)

您可能需要将路径添加到“bash.exe”。如果您不确定“bash.exe”在哪里,请打开您在问题中提到的“Cygwin.bat”文件。它应该在chdir命令中包含路径。(注意:Cygwin.bat 只是在交互模式下运行 bash.exe。它并没有真正运行任何名为“cygwin”的东西。)

You will also need to add the path to "script.txt" if it isn't in the same directory as your batch script.

如果“script.txt”与批处理脚本不在同一目录中,则您还需要将其添加到“script.txt”的路径中。

Again this will also work in a shortcut instead of a batch script.

同样,这也适用于快捷方式而不是批处理脚本。

回答by Orwellophile

If you want to open the script in a mintty windows aswell, this (based on @galmok's answer) will do the trick.

如果您还想在薄荷窗口中打开脚本,这(基于@galmok 的回答)可以解决问题。

: <<TRAMPOLINE
@echo off
C:\Users\user\Downloads\cyg-packages\bin\bash -c "exit 0" || (echo.No bash found in PATH! & exit /b 1)
setlocal EnableDelayedExpansion
for %%i in (%*) do set _args= !_args! "%%~i"
C:\Users\user\Downloads\cyg-packages\bin\mintty.exe -i /Cygwin-Terminal.ico C:\Users\user\Downloads\cyg-packages\bin\bash -l "%~f0" "%CD%" %_args%
goto :EOF 
TRAMPOLINE
#####################
#!/bin/bash  -- it's traditional!
cd ""
shift
echo Working from $PWD
vim "$@"

Note: my use-case was the need to open a terminal vim from Visual Studio, which is why there is a call to vimat the end. The batch file will exit afterwards.

注意:我的用例是需要从 Visual Studio 打开一个终端 vim,这就是为什么最后有一个调用vim。批处理文件随后将退出。

回答by FatAn

To run "your_bash_file" from any .bat file, write following line to .bat:

要从任何 .bat 文件运行“your_bash_file”,请将以下行写入 .bat:

git-bash.exe (path_to_your_bash_file>)

git-bash.exe (path_to_your_bash_file>)

P.S. Windows Git is necessary installed

PS Windows Git 是必须安装的