54

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.

Machavity
  • 30,841
  • 27
  • 92
  • 100
Ganeshja
  • 2,675
  • 12
  • 36
  • 57

3 Answers3

39

May be you can find answer here? 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
Community
  • 1
  • 1
imslavko
  • 6,596
  • 2
  • 34
  • 43
  • thanks a lot for u r quick responses ,i have chaged my .bat extention to .sh , is it all i need to do ? i ve also changed the back-slah (\supportlibraries\Framework ) to forward ones – Ganeshja Dec 28 '12 at 05:52
  • Great ! Thanks a lot imslavko , now i got clear with my doubts thanks once a again – Ganeshja Dec 28 '12 at 06:02
  • 2
    This answer is useless, someone how doesn't know the equivalent of a batch file in mac will need more details, like where is the best place to put the bash script, how to add to the executable path, etc... – Rabih Kodeih Aug 17 '16 at 12:56
  • 2
    @RabihKodeih the question is asking to solve a particular problem: give an example of converting a script that calls an executable with some path passed in. If the question was on everything about a bat script might have, you would need to post a book about bash – imslavko Aug 17 '16 at 23:20
  • 1
    If you don't want to change permissions for read write we can use, "chmod +x scriptname.sh" to just add the execute permission alone. – Sathesh Dec 24 '17 at 17:18
19

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
Brett Freer
  • 266
  • 1
  • 3
  • wow thanks Brett Freer for u r quick response, finally go clear with .sh , but i ve a another query here i have mentioned .\supportlibraaries\Frame ... will .sh traverse to the exact parent folder – Ganeshja Dec 28 '12 at 05:55
  • The paths will be relative to the directory from which the shell script is called, so if that does not necessarily make sense for your context, you could put in a 'cd somedir' command above the java line, or put in an absolute path name. – Brett Freer Dec 28 '12 at 05:58
2

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 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.

Edward
  • 1,062
  • 1
  • 17
  • 39