如何将 bash shell 脚本转换为 .bat 文件?

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

How do I convert a bash shell script to a .bat file?

bashshellscriptingbatch-file

提问by LOlliffe

I have a bash shell script on my Mac, that I need to convert to a .bat file (at least, that's what I've been told) for users that are on PCs. I was wondering the best way to go about this, or if anyone knows of any good references. I don't seem to be asking Google the right question to point me in the right direction.

我的 Mac 上有一个 bash shell 脚本,我需要为 PC 上的用户转换为 .bat 文件(至少,这是我被告知的)。我想知道解决这个问题的最佳方法,或者是否有人知道任何好的参考资料。我似乎没有向 Google 提出正确的问题来为我指明正确的方向。

Specifically, things like how would I do a...

具体来说,诸如我将如何做...

cd ~/Documents/DropFolder

(...where ~/ equals the root of the user's home directory, regardless of the user's name)?

(...其中 ~/ 等于用户主目录的根目录,无论用户名如何)?

Or, when working on variables and "do" statements...

或者,在处理变量和“do”语句时......

for i in *.xml
do
  java -Xss650K -Xms128m -Xmx2048m -jar /Applications...
done

And finally, identifying and using basenames...

最后,识别和使用基本名称......

  cp -p `basename $i .xml`.xml ~/Documents/ReadyForServer/`basename $i .xml`/

Thanks for any guidance, or suggestions for other solutions. LO

感谢您的任何指导,或对其他解决方案的建议。LO

采纳答案by Joey

Actually, the things you mention are trivial to port to a Windows batch file. While you certainly canuse Windows ports of all Unix tools (or even use an emulation layer for even more fun) this is not hard to do:

实际上,您提到的内容对于移植到 Windows 批处理文件来说是微不足道的。虽然您当然可以使用所有 Unix 工具的 Windows 端口(甚至可以使用仿真层以获得更多乐趣),但这并不难:

  1. ~ for the user's home folder

    The user's profile resides in the environment variable %USERPROFILE%, so the following should do it:

    cd %USERPROFILE%\Documents\DropFolder
    
  2. Iterating over a set of files

    The forcommand is helpful here:

    for %%i in (*.xml) do -Xss650K -Xms128m -Xmx2048m -jar ... %%i
    

    Obviously you need to adapt the path to the JAR file, though.

    And forhas many more uses beyond this one, so take a look at help foras well.

  3. basename

    You need to do this either in a subroutine or a forloop, as the following syntax is specific to loop variables or parameters. It won't work with environment variables as is. You can get what basenameis giving you by using %%~niwhere %%iif the loop variable or %~n1if %1is the argument to a subroutine or batch file you have. So the following would probably do the same:

    copy "%%~ni.xml" "%USERPROFILE%\Documents\ReadyForServer\%%~ni\"
    

    The help on forhas more information over those things near the end.

  1. ~ 用于用户的主文件夹

    用户的配置文件驻留在环境变量中%USERPROFILE%,因此应该执行以下操作:

    cd %USERPROFILE%\Documents\DropFolder
    
  2. 迭代一组文件

    for命令在这里很有用:

    for %%i in (*.xml) do -Xss650K -Xms128m -Xmx2048m -jar ... %%i
    

    不过,显然您需要调整 JAR 文件的路径。

    for具有超出了一个很多用处,所以来看看help for为好。

  3. 基名

    您需要在子例程或for循环中执行此操作,因为以下语法特定于循环变量或参数。它不会按原样使用环境变量。您可以basename通过使用%%~niwhere %%iif 循环变量或者%~n1if%1是您拥有的子例程或批处理文件的参数来获取给您的信息。所以以下可能会做同样的事情:

    copy "%%~ni.xml" "%USERPROFILE%\Documents\ReadyForServer\%%~ni\"
    

    帮助在for接近尾声时提供了有关这些内容的更多信息。

回答by scottyp

You can also use Git for Windows, which already has a Bash shell built-in (GitBash)

你也可以使用 Git for Windows,它已经内置了 Bash shell (GitBash)

回答by Maz

The windows shell is a different scripting language than the bash shell. You will need to port it. Alternatively, you could use something like cygwin or mingw to run the bash shell on windows.

windows shell 是一种不同于 bash shell 的脚本语言。您将需要移植它。或者,您可以使用诸如 cygwin 或 mingw 之类的东西在 Windows 上运行 bash shell。

回答by tonyj

F1 help on windows is surprisingly useful when writing DOS style batch files. From the windows desktop, hit F1 and search for batch. The link for Using Batch Parameters is pretty helpful for doing odd tricks with DOS. The 'Using Batch Files' page is also quite good. As other posters mention, you're better off avoiding batch as your implementation language, but if you must, you must.

在编写 DOS 样式的批处理文件时,Windows 上的 F1 帮助非常有用。在 Windows 桌面上,按 F1 并搜索批处理。Using Batch Parameters 的链接对于使用 DOS 做一些奇怪的技巧非常有帮助。“使用批处理文件”页面也很不错。正如其他海报所提到的,您最好避免将批处理作为您的实现语言,但如果您必须这样做,您也必须这样做。

回答by ghostdog74

You should convert by hand. Learn batch commands, then do the equivalent. Alternatively, you can use GNU win32 unixtools.

您应该手动转换。学习批处理命令,然后执行等效操作。或者,您可以使用GNU win32 unix工具。

回答by buckbova

Take a look at powershell. This site has many examples:

看看powershell。这个网站有很多例子:

http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx

http://technet.microsoft.com/en-us/scriptcenter/powershell.aspx