Equivalent of .bat in mac os - macos

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.

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

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

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.

Related

How do I create new unix executable (.sh?) files for the terminal to use as commands?

I'd like to make some files to put in my /usr/bin folder (Mac OS) and be able to run them by typing the name to the terminal, like the commands gcc, cd, or vim (those are all in that folder). When I open such a file as text, it appears encrypted, so I'm not sure how to create one or what extension to use. Thank you.
The files you see in /usr/bin are not encrypted - they're compiled code in machine language different from bash scripts. You can however have scripts also in the /usr/bin location and have them run exactly as you are expecting.
In order to do that, you will have to create an executable script file. In unix, scripts are not identified by file extension but by 2 things:
executable bit set on the file permission level
Script interpreter header, such as #!/bin/bash
For a bash script, you can do the following:
Make a new file with the following content:
#!/bin/bash
echo "Hello world - My first bash script."
Save the file as hello.sh (the .sh is just convention, it could be any file name).
Then run chmod +x hello.sh and you will be able to run this file as an executable.
Move this file to /usr/local/bin and you should be able to run hello.sh from command line and it should execute your program.
You can create scripts, for example in bash, make the file executable with chmod and put the file path in your $PATH.
Example:
Create a new file called myscript.sh, you can use vi or any editor you prefer.
Add the content below on it:
#!/bin/bash
echo "Hello world!"
Now make it executable:
chmod u+x my script.sh
You can run your script like this ./myscript.sh or add it to your path:
export PATH=$PATH:$PWD
Now you can just run with the name, like myscript.sh
Programs such as gcc and cd are not encrypted, but compiled; however, if you create a shell script (without .sh as suffix; the .sh file extension for a shell script is optional) and place it in /usr/bin or any PATH location, and you chmod +x <script-path> (to give execute permission) then you can directly use that as a command.
Note: use shebang to mention the script interpreter, e.g. #!/usr/bin/env bash or equivalent for korn shell, python, etc.

Bash: Scripts under the same dir

I have in my $PATH my own path to my scripts.
I do that adding on my .bashrc
PATH=$PATH:~/home/user/myownscripts
In that directory I have two scripts, but only can use one of them, the first one I did create. And if created another script doesn't work neither.
Only can call the first script created.
The ls command return the following output
first_script second_script third_script
And first_script is bold and green
Why? and how fix this problem?
The second and third scripts are not executable. Use ls -l (provides more details about the files) to see the permissions of the files and run the following command to make these scripts executable.
chmod +x second_script third_script
If you run ls -l again, you should notice that they now have the x bit set in their file permissions.
This is a basic and fundamental aspect to Unix systems and I’d suggest that you read a book or tutorial on shell programming on a Unix-like system.

Run a bash script in cygwin using ./

I have a simple bash script that throw errors on a Windows machine in the Cygwin xterm terminal when I call it like so: ./myscript.bat. It runs fine when I call it like this: /cygdrive/c/cygwin/bin/bash.exe myscript.bat. I am thinking that my shell is not using bash by default. How can I set it to bash so that the next time I open the shell, I can execute my script using ./myscript.bat?
When you execute a file, Windows (or some component within Windows) decides how to execute it based on the extension part of the file name.
Cygwin inherits this functionality, letting you run Windows commands from within Cygwin. Cygwin also implements most of the usual UNIX functionality (running commands based on their content), but the combination of UNIX and Windows semantics can't always be perfectly clean.
The .bat suffix refers to a Windows batch file, so when you try to execute myscript.bat, the system treats it that way rather than as a bash script.
Change the file name from myscript.bat to myscript.bash or myscript.sh -- or just drop the extension altogether (since someone running your script shouldn't need to care how it's written).
There are several other filename extensions you should avoid (like .cmd), depending on how Windows is configured. A few quick experiments show that a .sh extension is safe, but really you don't need to use an extension at all for a shell script.
And, as R Sahu's answer says, you also need to make sure the script has execute permission if you haven't already done so:
mv myscript.bat myscript
chmod +x myscript
You'll probably need to change permissions of the file to make it an executable.
Try
chmod +x myscript.bat
./myscript.bat

Converting a shell script to a dmg

I have a shell script with some functionalities. I want to convert it to an executable file. Any idea if this is possible?
Thanks
Add the following line at the very top of your script:
#!/bin/sh
This is known as a shebang. It indicates which program to invoke the shell script with when you execute it. You could change it to anything. Eg, to run a zsh script you would use #!/bin/zsh, which is the path to the zsh interpreter on my machine.
Then you need to mark the file as executable using chmod. You can do this as follows:
chmod +x myscript
You can now run the script like this:
/full/path/to/myscript
Or, if you're in the directory the script is in:
./myscript
The '.' expands to the path of your current working directory.
If you want to be able to run the script from anywhere, stick it somewhere in your path. Eg.
mv myscript /usr/bin
You can now run the script from anywhere by typing in just the name.

How to create and run a bash file?

How to create a bash file?
I found following bash script.
#!/bin/bash
for f in *.wav; do
echo "Processing $f file..."
afconvert -f caff -d LEI16#44100 -c 1 "$f" "${f/wav/caf}"
done
I have Mac and Windows.
But I don't know where I have to put this and how I can run this script?
Just save the text into a file called my_bash_script a file, make it executable and run it from the command line like this:
chmod u+x my_bash_script
./my_bash_script
Judging by the file it will need to be in a directory containing *.wav files.
As you have two different OS setup, I will split my answer in two parts.
First: Windows
Windows does not have a Bash interpreter, nor the afconvert program the code above is trying tu run. Your best bet will be to use Cygwin to install a Unix console on your Windows. Also I don't know, where you could get afconvert from.
OSX
OSX does have a console and the afconvert software (at least my OSX does). You can simply drop the file in a folder and give it a name ending in .sh. Then you should be able to run it.
Just paste the text into a plaint text file and then mark it as executable:
chmod +x yourScript
To run it:
./yourScript
Place the script anywhere you have your .wav files. When fallow instructions given here:
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_01.html#sect_02_01_03

Resources