Apple (Unix) equivalent to a Windows "bat…
apple Verified ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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
10 replies
Sign in or join Beachball Help to reply.
- 0
Tony T1 ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Write a shell script, and give it the extension ".command"
For example, with TextEdit, save as a plan-text file:
#!/bin/bash
printf "Hello Worldn"..and make it executable:
chmod u+x Hello World.command - 0
BobHarris ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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. - 0
J D McIninch ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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 WorldFor 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:
./myscriptIn 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.
- 0
Kevin GARDINER ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Thank you all.
I will have to study all of the this information.
Simply "chmod +x myscript" (without the u) what does this do ?
Is there a good site or book that I could get to understand a little more.
I have also been asked to build a web site, is any of this information useful for that ?
Thanks. Kevin
- 0
Kevin GARDINER ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
It there an instruction to type in the terminal to know the status of the flags? 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 !
Kevin
- 0
BobHarris ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
It there an instruction to type in the terminal to know the status of the flags?
Try using:
ls -dleO@ file.nameI 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 - 0
Tony T1 ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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
- 0
BobHarris ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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.
- 0
Kevin GARDINER ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
Thanks. I will look at this.
Kevin
- 0
Neville Hillyer ·
Imported from the Apple Support Forums via the Wayback Machine 🔗
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.