Equivalent of .bat in mac os

MacosBatch FileSh

Macos Problem Overview


I currently use a .bat file that is utilized to invoke a java file. If I wanted to utilize the same functionality on Mac OS what format changes would I make? (unless the .bat equivalent on Mac OS is the .sh format?)

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

Any assistance would be appreciated.

Macos Solutions


Solution 1 - Macos

May be you can find answer here? https://stackoverflow.com/questions/589905/equivalent-of-double-clickable-sh-and-bat-on-mac

Usually you can create bash script for Mac OS, where you put similar commands as in batch file. For your case create bash file and put same command, but change back-slashes with regular ones.

Your file will look something like:

#! /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

Change folders in path above to relevant one.

Then make this script executable: open terminal and navigate to folder with your script. Then change read-write-execute rights for this file running command:

chmod 755 scriptname.sh

Then you can run it like any other regular script: ./scriptname.sh

or you can run it passing file to bash:

bash scriptname.sh

Solution 2 - Macos

The common convention would be to put it in a .sh file that looks like this -

#!/bin/bash
java -cp  ".;./supportlibraries/Framework_Core.jar;... etc

Note that '' become '/'.

You could execute as

sh myfile.sh

or set the x bit on the file

chmod +x myfile.sh

and then just call

myfile.sh

Solution 3 - Macos

I found some useful information in a forum page, quoted below.
From this, mainly the sentences in bold formatting, my answer is:

  • 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
    

The [forum page](https://discussions.apple.com/thread/2375724 "Apple (Unix) equivalent to a Windows 'bat' file") question:

> Good day, [...] I wondering if there are some "simple" rules to write an equivalent
> of the Windows (DOS) bat file. I would like just to click on a file and let it run.

Info from some answers after the question:

> Write a shell script, and give it the extension ".command". > For example: > > #!/bin/bash > printf "Hello World\n"

- Mar 23, 2010, 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

- Mar 23, 2010, J D McIninch.

Also, info from an accepted answer for Equivalent of double-clickable .sh and .bat on Mac?:

> On mac, there is a specific extension for executing shell
> scripts by double clicking them: this is .command.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGaneshjaView Question on Stackoverflow
Solution 1 - MacosimslavkoView Answer on Stackoverflow
Solution 2 - MacosBrett FreerView Answer on Stackoverflow
Solution 3 - MacosEdwardView Answer on Stackoverflow