Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Apple (Unix) equivalent to a Windows "bat" file

Good day,

I have just solved a problem (thanks to someone on this forum) and written a small file to start the application.

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

Thanks.

Kevin

Mac Pro 2.6 GHz Intel Core 2 Duo - 4 Go SDRAM, Mac OS X (10.5.8), SAFARI / Firefox

Posted on Mar 23, 2010 10:44 AM

Reply
10 replies

Mar 23, 2010 11:56 AM in response to Kevin GARDINER

If you do not need a Terminal window (the .command suffix will open Terminal), then you can create an Applicaitons -> Automator workflow using "Run Shell Script".

Or you can download Platypus (free download)
<http://www.versiontracker.com/dyn/moreinfo/macosx/19870>
which will allow you to create a double-clickable application that does not need the Terminal to run.

Mar 23, 2010 1:37 PM in response to Kevin GARDINER

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. This is how languages like PERL and Python work.

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, if your text file contains a script writtein the in the PERL language, it would look like this:


#!/usr/bin/perl
print "Hello World ";


... the UNIX shell is an intepreter too:


#!/bin/sh
echo Hello World


For your script to run from the UNIX command line (Terminal.app), you need to set the executable flag of the file. For example, if your script is called 'myscript', then you'd type:


chmod u+x myscript


... and you can execute it like so:


./myscript


In fact, there are several different scripting languages provided with OS X for you to use: PERL, Python, Ruby, PHP, Bash, etc. Each has it's own strengths and complexities.

Mar 23, 2010 5:09 PM in response to Kevin GARDINER

It there an instruction to type in the terminal to know the status of the flags?

Try using:

ls -dleO@ file.name

I tried the chmod +X on a "txt" file and also having removed the "txt". One time the file changed in the "finder" of Apple from "text file" to excutable unix, another time it didn't and I don't see why !

There is a difference between lowercase -x and capital -X. case is very important.

For more information about various Unix commands you can generally use

man chmod
man ls

Mar 23, 2010 5:21 PM in response to Kevin GARDINER

Is there a good site or book that I could get to understand a little more.


Take a look at : http://www.osxfaq.com/Tutorials/LearningCenter/

Simply "chmod +x myscript" (without the u) what does this do ?


Take a look at MODES in the man page. Just type man chmod in Terminal:

The symbolic mode is described by the following grammar:
mode ::= clause [, clause ...]
clause ::= [who ...] [action ...] action
action ::= op [perm ...]
who ::= a | u | g | o
op ::= + | - | =
perm ::= r | s | t | w | x | X | u | g | o
The who symbols ``u'', ``g'', and ``o'' specify the user, group, and
other parts of the mode bits, respectively. The who symbol ``a'' is
equivalent to ``ugo''.


Tony

Mar 23, 2010 5:22 PM in response to Kevin GARDINER

Simply "chmod +x myscript" (without the u) what does this do ?

See "man chmod"

Unix permissions apply to the "User", members of the user's group, and others (everyone not the user and not in the user's group). This permissions classifications were created back in the time-sharing days when a single computer is shared with dozens or hundreds of users.

So back to your question, "chmod u+x" applies execute permissions to the User (owner) of the file.

Just specifying "chmod +x" gives execute permission to the User, the Group, and others (everyone else).

Again, this is described in "man chmod".
Is there a good site or book that I could get to understand a little more.

For the most part, this is generic Unix stuff. Books about the Bash shell would cover most of this stuff.

There have been other posts in this forum giving suggestions for books and web sites with beginning Unix and Mac OS X Unix info.

Personally, I'm a bit old school, and I prefer the Kernighan and Pike "The Unix Programming Environment", which covers basic shell usage, shell scripting, and works its way up to C programming, which you could ignore. It packs a lot of Unix information into a small book.

Apr 6, 2010 6:54 AM in response to Kevin GARDINER

I have also been asked to build a web site, is any of this information useful for that ?


Most unlikely.

One difficulty is that "build a web site" has so many different meanings. I got involved during the early days of the web and I still prefer to hand craft my own HTML and perfect my own JavaScript. I dislike content management systems.

As you can see I have an Xserve but I also still run my first server (Quid Pro Quo). OS X comes with Apache which is one of the most popular web servers. I am unsure how much you know about servers and HTML but if you are an absolute beginner you could enable Apache on your Mac and try putting a simple HTML page on it. I have some (very old with a few dead links) web pages which may be useful:

http://links.open.ac.uk/www/start/
http://links.open.ac.uk/www/

Unless you have money to spare it is worth remembering that OS X can do most/all that OS X Server does although OS X Server may be less tedious to set up.

Apple (Unix) equivalent to a Windows "bat" file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.