如何将 bash shell 脚本转换为 .bat 文件? [英] How do I convert a bash shell script to a .bat file?

查看:76
本文介绍了如何将 bash shell 脚本转换为 .bat 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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)?

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

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

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

推荐答案

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

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

  1. ~用户的主文件夹

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

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

cd %USERPROFILE%DocumentsDropFolder

  • 迭代一组文件

    for 命令在这里很有用:

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

    显然,您需要调整 JAR 文件的路径.

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

    而且 for 除了这个之外还有更多的用途,所以也看看 help for.

    And for has many more uses beyond this one, so take a look at help for as well.

    基本名称

    您需要在子例程或 for 循环中执行此操作,因为以下语法特定于循环变量或参数.它不会按原样使用环境变量.您可以使用 %%~ni where %%i 如果循环变量或 %~n1 如果 %1 是您拥有的子程序或批处理文件的参数.所以以下可能会做同样的事情:

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

    copy "%%~ni.xml" "%USERPROFILE%DocumentsReadyForServer\%%~ni"
    

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

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

    这篇关于如何将 bash shell 脚本转换为 .bat 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆