10.3 Programming

We mention earlier that the shell is the system's command interpreter. It reads each command line you enter at your terminal and performs the operation that you call for. Your shell is chosen when your account is set up.

The shell is just an ordinary program that can be called by a Unix command. However, it contains some features (such as variables , control structures, and so on) that make it similar to a programming language. You can save a series of shell commands in a file, called a shell script , to accomplish specialized functions.

Programming the shell should be attempted only when you are reasonably confident in your ability to use Unix commands. Unix is quite a powerful tool, and its capabilities become more apparent when you try your hand at shell programming.

Take time to learn the basics. Then, when you're faced with a new task, take time to browse through references to find programs or options that will help you get the job done more easily. Once you've done that, learn how to build shell scripts so that you never have to type a complicated command sequence more than once.

Let's have a closer look at a shell script to give you some flavor of what can be done. First, to list all known user accounts on the system, you need to extract the information from the NetInfo database, which can be done by using nireport .

You can try this script, listusers , by entering the following few lines into vi, pico, or another editor of your choice. See Chapter 4 for additional information on editing files.

 #!/bin/sh echo "UID       NAME    FULLNAME        HOME            SHELL" nireport . /users uid name realname home shell  \   awk ' > 99 { print 
 #!/bin/sh echo "UID NAME FULLNAME HOME SHELL" nireport . /users uid name realname home shell  \ awk '$1 > 99 { print $0 }' 

The first line indicates what program should run the script, and, like most scripts, this is written for the Bourne Shell, /bin/sh . By using the awk utility to test for user IDs greater than 99, this script further screens out any account information for system accounts (which, by convention, have an ID value of less than 100).

To make a shell script act as if it's a new program rather than just a text file, you use chmod +x to make it executable, then you can run it by typing in its name if it's in your current PATH (see Chapter 1 for more information on setting and customizing your PATH), or with the ./ prefix to indicate that it's in the current directory, as shown here:

 $  chmod +x listusers  $  ./listusers  UID     NAME    FULLNAME        HOME            SHELL 501     taylor  Dave Taylor     /Users/taylor   /bin/bash 502     tintin  Mr. Tintin      /Users/tintin   /bin/bash 

This is really the tip of the iceberg with shell scripts. For more information, look at Unix in a Nutshell , by Arnold Robbins, and Unix Power Tools , by Shelley Powers, Jerry Peek, Tim O'Reilly, and Mike Loukides (both published by O'Reilly), or Wicked Cool Shell Scripts , by Dave Taylor (NoStarch Press).

10.3.1 Shell Scripts into Droplets

Another very cool trick with Mac OS X is to turn a shell script into a droplet, an application that can be have files dropped onto it from the Finder. To do this, you'll need to have a script to download and launch a copy of Fred Sanchez' DropScript utility.

Get DropScript by going to http://www.versiontracker.com/ and searching for "dropscript." VersionTracker is well worth exploring too, helping you keep up-to-date on system and application updates.

At its simplest, a droplet script accepts one or more files, which are given as command-line arguments, which are then processed in some manner. As a simple example, here's a droplet script that prints whatever files you give it:

 #!/bin/sh pr "$*"  lpr 

This can be turned into a droplet by dragging the shell script icon over the DropScript application in the Finder. It creates a new version called drop filename that's fully drag-and-drop-enabled. For example, if this script were called print-text , the droplet would be called dropprint-text .

Learning Unix for Mac OS X Panther
Learning Unix for Mac OS X Panther
ISBN: 0596006179
EAN: 2147483647
Year: 2003
Pages: 88

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net