在Mac OS中与.bat相当的是什么?

54

我目前使用一个.bat文件来调用Java文件。如果我想在Mac OS上使用相同的功能,我需要进行哪些格式更改?(除非在Mac OS上.bat等效于.sh格式?)

java -cp  ".;.\supportlibraries\Framework_Core.jar;.\supportlibraries\Framework_DataTable.jar;.\supportlibraries\Framework_Reporting.jar;.\supportlibraries\Framework_Utilities.jar;.\supportlibraries\poi-3.8-20120326.jar;D:\downloads\Selenium 2.0\selenium-server-standalone-2.19.0.jar" allocator.testTrack

任何协助将不胜感激。


1
请问您使用的是哪个版本的MAC操作系统?可能是10.1或更高版本吗? - Sankar Ganesh PMP
3个回答

39

也许你可以在这里找到答案?在 Mac 上双击可运行的 .sh 和 .bat 文件的等效方法是什么?

通常情况下,你可以为 Mac OS 创建 bash 脚本,在其中放置类似批处理文件中的命令。对于你的情况,创建一个 bash 文件,并放置相同的命令,但将反斜杠改为普通斜杠。

你的文件会像这样:

#! /bin/bash
java -cp  ".;./supportlibraries/Framework_Core.jar;./supportlibraries/Framework_DataTable.jar;./supportlibraries/Framework_Reporting.jar;./supportlibraries/Framework_Utilities.jar;./supportlibraries/poi-3.8-20120326.jar;PATH_TO_YOUR_SELENIUM_SERVER_FOLDER/selenium-server-standalone-2.19.0.jar" allocator.testTrack

将路径中的文件夹更改为相关的文件夹。

然后使此脚本可执行:打开终端并导航到包含您的脚本的文件夹。然后运行以下命令更改此文件的读取-写入-执行权限:

chmod 755 scriptname.sh

然后您可以像运行任何其他常规脚本一样运行它: ./scriptname.sh

或者您可以将文件传递给bash来运行它:

bash scriptname.sh

非常感谢您的快速回复,我已将我的.bat扩展名更改为.sh,这是我需要做的全部吗?我还将反斜杠(\supportlibraries\Framework)更改为正斜杠。 - Ganeshja
太棒了!非常感谢imslavko,现在我的疑惑已经清楚了,再次感谢。 - Ganeshja
2
这个答案没什么用,对于不知道在Mac中批处理文件等效的人来说,需要更多的细节,比如最佳位置放置bash脚本,如何添加到可执行路径等等... - Rabih Kodeih
2
@RabihKodeih 这个问题要求解决一个特定的问题:给出一个将调用某个路径传递给可执行文件的脚本转换的示例。如果问题涉及到有关批处理脚本的所有内容,那么你需要发布一本关于bash的书籍。 - imslavko
1
如果您不想更改读写权限,我们可以使用“chmod +x scriptname.sh”仅添加执行权限。 - Sathesh

19

通常惯例是把它放在一个看起来像这样的.sh文件中 -

#!/bin/bash
java -cp  ".;./supportlibraries/Framework_Core.jar;... etc
注意 '\' 会变成 '/'。
您可以执行以下操作:
sh myfile.sh

或在文件上设置 x 位

chmod +x myfile.sh

然后只需调用

myfile.sh

哇,感谢Brett Freer的快速回应,终于理清楚了关于.sh的问题,但是我这里还有另一个疑问,我已经提到了.\supportlibraaries\Frame...,那么.sh会如何遍历到确切的父文件夹? - Ganeshja
路径将相对于调用shell脚本的目录,因此如果这对您的上下文没有必要,您可以在java行上方放置一个“cd somedir”命令,或者放置绝对路径名。 - Brett Freer

2
我在论坛页面上发现了一些有用的信息,如下所引:
从这里,主要是粗体格式的句子,我的回答是:
  • Make a bash (shell) script version of your .bat file (like other
    answers, with \ changed to / in file paths). For example:

    # File "example.command":
    #!/bin/bash
    java -cp  ".;./supportlibraries/Framework_Core.jar; ...etc.
    
  • Then rename it to have the Mac OS file extension .command.
    That should make the script run using the Terminal app.

  • If the app user is going to use a bash script version of the file on Linux
    or run it from the command line, they need to add executable rights
    (change mode bits) using this command, in the folder that has the file:

    chmod +rx [filename].sh
    #or:# chmod +rx [filename].command
    
“论坛页面”问题:
问候,[...]我想知道是否有一些“简单”的规则来编写等效于 Windows(DOS)批处理文件。我只想点击一个文件然后让它运行。
一些答案提供的信息:

Write a shell script, and give it the extension ".command". For example:

#!/bin/bash 
printf "Hello World\n" 
- 2010年3月23日,Tony T1。

The DOS .BAT file was an attempt to bring to MS-DOS something like the idea of the UNIX script.
In general, UNIX permits you to make a text file with commands in it and run it by simply flagging
the text file as executable (rather than give it a specific suffix). This is how OS X does it.

However, OS X adds the feature that if you give the file the suffix .command, Finder
will run Terminal.app to execute it (similar to how BAT files work in Windows).

Unlike MS-DOS, however, UNIX (and OS X) permits you to specify what interpreter is used
for the script. An interpreter is a program that reads in text from a file and does something
with it. [...] In UNIX, you can specify which interpreter to use by making the first line in the
text file one that begins with "#!" followed by the path to the interpreter. For example [...]

#!/bin/sh
echo Hello World

2010年3月23日,J D McIninch。

此外,来自Mac上.sh和.bat文件的双击等效方法?的被接受答案中的信息:

在Mac上,有一个特定的扩展名可以通过双击执行Shell脚本:它是.command


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接