How You Will Be Working with Unix


You are probably already using your Macintosh for a variety of tasks , working in applications that take advantage of the lovely Aqua interface. As you dig below the surface and start using the Darwin layer of Mac OS X, you will be performing operations that are either unique to Unix or better suited to the Unix environment.

Working from the command line

The command line is the primary user interface in Unix. Most Unix software packages are designed to be installed and configured from the command line.

It is from the command line that you will be installing software and manipulating files (copying, moving, renaming, and so on). You might even start editing files using the command-line tools.

One of the most powerful aspects of the command line is in how it allows you to connect a series of commands together to accomplish some task. Figure 1.4 shows an example of connecting three commands together in order to find all the files in a folder that contain the word success and e-mail the resulting list of filenames to yourself.

Figure 1.4. This command line shows how to connect commands togetherin this case, finding all the files in a folder that contain the word success and then e-mailing the resulting list of filenames to yourself. /System/Library/StartupItems/Apache/Apache handles the starting (and stopping) of the Apache Web server.
 find . -type f -print0  xargs -0 grep -l success  Mail  address@hostname.com  & 

The command line in Figure 1.4 is composed of three major parts separated by the vertical bar ( ) character, which is called a pipe . (Note: The first part uses the find command to produce a list of the names of all the files (not folders) in the current folder (by using the -type f option) and all those inside it. The output of that command is passed ( piped ) via the character to the next command, xargs ( arguments ). This second part applies the grep ( search ) command to each filename in turn , searching the file for the string success and producing a list of the filenames where the string was found. That second list is piped to the third part, the Mail command, which sends the list to the specified e-mail address. The ampersand ( & ) at the end tells Unix to do all this "in the background," which means that we do not have to wait for the processes to finish before issuing a new commandwe can go on with our work at the command line. Don't worry if every little thing doesn't make sense yetyou will learn about each aspect of that command in the following chapters. Understand that part of the power of Unix comes from the fact that many small parts can be combined to do bigger tasks, as seen above. See Chapter 2, "Using the Command Line," for more details.

Under Mac OS X, the most common way to get to the command line is through the Terminal application (found in the Utilities folder under Applications).

Editing files from the command line

In order to really harness the power of Unix, you will want to learn how to edit files using a command-line text editor. Unix is file-centric and uses text files to control almost every aspect of software configuration. Although it's difficult for most Mac users to learn at first, editing files from the command line lets you change files without leaving the command-line environment in which most of your Unix work will occur. Furthermore, the ability to edit files from the command line will make it easy for you to work on other Unix systems besides Mac OS X, something you are almost certain to do once you get further into Unix.

Unix Commands Have Strange Names

Unix commands often have very terse obscure and/or arbitrary-sounding names. Examples: awk , grep , and chmod .

This contributes to Unix's (justly earned) reputation as a difficult operating system to use, requiring users to memorize a great deal in order to become proficient.

Because you are probably itching to know how those three commands got their names, here's the story: awk , a text-processing system, got its name from the initials of the three people who created it. grep , a command for searching inside text, got its name from the commands used in an earlier program to "globally find a regular expression and print." chmod is a command to change the permissions associated with a file and means CHange MODe .


Programming and scripting

Developed by programmers for programming, Unix isnot surprisinglyan excellent programming environment, and many of its strengths (and some of its weaknesses) stem from that heritage.

Although you don't need Mac OS X or Unix to create software, if you're using Unix, you'll probably at least poke around with programmingperhaps first modifying existing programs and then moving on to create new ones. In addition to its terrific stability, Unix provides an environment in which it's easy to connect varying tools in an equally various number of ways. And when you need them, you can create new commands, extending your tool kit as you work.

You can also write simple scripts to automate tasksfor example, to perform backups , automate the transfer of files to other systems, calculate the rate of return on an investment, or search text for certain phrases and highlight them. You could write scripts to create a small database- backed Web site, or to convert batches of images for use on the Web, or to analyze voter -registration or campaign-contribution records. Some users never stop creating new applications: We call them programmers .

Mac OS X comes with tools to create and run programs in AppleScript, Perl, Bourne shell, and a couple of other Unix scripting languages. The Mac OS X developer tools include software that allows you to create programs in C, C++, Objective-C (the primary language for native Mac OS X software), Java, Ruby, and Python as well. (Throughout this book we assume that you have in fact installed the Developer Tools. If not, refer back to the Introduction.) With the exception of AppleScript, none of these programming languages were available to Mac users in the past unless they installed third-party software (such as MacPerl or the CodeWarrior compiler). The Mac OS X Developer Tools also include the Xcode and Interface Builder applications, which are graphical interfaces for developing software projects written in C, Objective-C, C++, and Java.

Shell scripts

The vast majority of Unix scripting is done using shell scripts . These are written using the language of a Unix shell . A Unix shell is the program that provides the command-line interface you will be using. A shell accepts typed commands and provides output in text form; it is a "shell" around the operating system. The Bourne shell is one of the oldest command-line interpreters for Unix (see Chapter 2). Bourne shell scripts are used as part of the system startup and shutdown process in virtually all versions of Unix, and Mac OS X is no exception (although it uses fewer than most versions of Unix; see "The Boot Sequence" in Chapter 11). Also, when you are using Mac OS X from the command line (which is what this book is all about), you will normally be using an advanced version of the Bourne shell, called bash (for Bourne again shell ).

If you are excited or impatient, you probably want to take a look at one of the Mac OS X system-startup scripts right now! Here's how to do it.

To view a system-startup script:

1.
Open a Mac OS X (not Classic) text editorfor example, the TextEdit application, which you can access through TextEdit in the Applications folder.

2.
Open the file /System/Library/ StartupItems/Apache/Apache .

The file will be opened in read-only mode, so you need not worry about damaging it.

You are looking at the script that starts up the Apache Web server when your machine starts up ( Figure 1.5 ).

Figure 1.5. The script /System/Library/StartupItems/Apache/Apache is used to start and stop the Apache Web Server.
 #!/bin/sh ## # Apache HTTP Server ## . /etc/rc.common StartService () {        if [ "${WEBSERVER:=-NO-}" = "-YES-" ]; then             echo "Starting Apache web server"             if [ ! -e /etc/httpd/httpd.conf ] ; then                       cp -p /etc/httpd/httpd.conf.default /etc/httpd/httpd.conf             fi             apachectl start             if [ "${WEBPERFCACHESERVER:=-NO-}" = "-YES-" ]; then                  if [ -x /usr/sbin/webperfcachectl ]; then                       echo "Starting web performance cache server"                       /usr/sbin/webperfcachectl start                  fi             fi        fi } StopService () {        if [ -x /usr/sbin/webperfcachectl ]; then             echo "Stopping web performance cache server"             /usr/sbin/webperfcachectl stop        fi        echo "Stopping Apache web server"        apachectl stop    } RestartService () {        if [ "${WEBSERVER:=-NO-}" = "-YES-" ]; then             echo "Restarting Apache web server"             apachectl restart             if [ "${WEBPERFCACHESERVER:=-NO-}" = "-YES-" ]; then                  if [ -x /usr/sbin/webperfcachectl ]; then                       echo "Restarting web performance cache server"                       /usr/sbin/webperfcachectl restart                  fi             fi        else             StopService        fi } RunService "" 

Figure 1.6 is an example of a script you might use in Mac OS X to make a group of files open in Photoshop when they're double-clicked from the Finder. Using this script and some additional Unix commands, you could instruct your machine to find every file ending in .jpg within a directory (folder) and have those files launch Photoshop when double-clicked from the Finder. And by altering the script, you could do the same thing for just those files that already have the Mac type code for JPEGs, GIFs, and others. (The type code is a four-character code that identifies the type of each file. It's a preMac OS X feature that many Mac applications still use.)

Figure 1.6. You might use this script in Mac OS X to make a group of files open in Photoshop when they're double-clicked from the Finder.
 #!/bin/sh # This a comment. Comments help make the code easier to read. # This script takes one or more filenames as arguments and # sets the Creator Code for each one to Photoshop. GETINFO="/Developer/Tools/GetFileInfo" SETFILE="/Developer/Tools/SetFile" #  8BIM is the Creator Code for Photoshop NEW_CREATOR="8BIM" changed_files=0 total_files=0 for file in "$@" ;   # All the command-line arguments are in $@ do        total_files=`expr $total_files + 1`;  # keep track of total        if [ ! -f "$file" ];   # if it is not a file        then             echo "skipping '$file' - it is not a file."        elif [ -w "$file" ];   # If it is writable...        then             creator=`$GETINFO -c "$file"`; # Get the Creator code of this file             if [ !  "$creator" = \"$NEW_CREATOR\" ] ; # If it is not already set...             then                  # Set the file to have the new creator code                  $SETFILE -c "$NEW_CREATOR" "$file"                  changed_files=`expr $changed_files + 1`             fi        else             echo "skipping '$file'  not writable"        fi done echo "Checked $total_files files" echo "Set $changed_files files to have creator $NEW_CREATOR" skipped=`expr $total_files - $changed_files` echo "Skipped $skipped files" 

This example may look scary now, but don't worryonce you learn some Unix, it will make more sense. For now, just let it wash over you, and understand that when you've read this book (and thus know a bit of Unix), you'll be able to create this sort of script fairly easily. (See Chapter 9, "Creating and Using Scripts," for more on creating shell scripts.)

Perl

Perl is one of the most popular programming languages in the world. Although you can use it to build large, complex programs, it is easy enough to learn that most people begin using it to write small utility programs or CGI programs for Web sites.

Because Perl excels at text processing and can easily interact with SQL databases, it's ideal for building Web pages as well as other data-manipulation projects.

Figure 1.7 is a code listing of a Perl script that outputs plain-text files in reversethe last line comes out first. This would be difficult, if not impossible , using traditional Macintosh applications.

Figure 1.7. This Perl script code listing outputs plain-text files in reverse, with the last line first.
 #!/usr/bin/perl # This a comment. Always use comments. # # This script takes one or more file names as arguments and # outputs the files one line at a time, in reverse order. # I.e. The last line of the last file comes out first. while ( $file = pop(@ARGV) ) {   open FILE, "$file"; # Open the file for reading   @lines = <FILE>;   # Read the entire file into @lines   close FILE;   while ( $line = pop(@lines) ) {       print  $line;   } } 

Java

Although newer than many other programming languages, Java has already spread far and widepartly because it is powerful and partly because its creator, Sun Microsystems, has promoted it very well.

Programs written in Java can run on only one kind of machine, but that machine is a virtual machine a piece of software. Because a virtual machine is software, it can be written for different hardware platforms. Java virtual machines exist for every major operating system, and an increasing number of small hardware devices (such as cell phones) are able to run Java code. Programs written in Java can often run without changes on many different platforms. The Mac OS X Developer Tools come with a Java compiler and a Java virtual machine. The Java programming language has a large set of tools for creating graphical user interfaces and for communicating across networks.

C

The C programming language is to programming what Greek is to literaturethe language of heroes. C is the language in which Unix as we know it was written, and most of the utility programs used with Unix were written in C. In the Unix world, the people who invented Unix could be thought of as heroes, and they wrote their great works in C.

The core of every Unix operating system (the kernel ) is written almost entirely in C, as is virtually every common Unix utility program, such as ls , pwd , and grep . Many important Unix applications, such as Sendmail and the Apache Web server, are also written in C. In addition, C++, Objective-C, and a number of other important languages stem from or are related to C.

Because so much Unix software is written in C, you're likely to at least modify existing C code if you spend much time working on the Unix platform.

Interacting with other Unix machines

Much of what people do with their Unix systems involves connecting to other Unix systemsfor example, logging in to a machine that hosts a Web site to edit files and install software, or arranging to automatically transfer files between two Unix machines.

To ease this process, Mac OS X comes with a widely used program called ssh ( Secure Shell ) that facilitates secure (encrypted) connections to other machines over the Internet. With ssh you can connect from the command-line interface to other Unix machines, and the information exchanged is protected from being read if intercepted. Other programs also use ssh to work over encrypted connections.

Running servers

One of the biggest differences between Mac OS 9 and Mac OS X is that the latter allows you to reliably run servers (such as a Web server or an e-mail server) on your computer. You could run these types of servers on Mac OS 9, but because Mac OS 9 was much more likely to crash, you probably wouldn't consider it for serious use. Also, most of the software available for these kinds of servers on Mac OS 9 was closed-source proprietary software, so if the vendor changed its business plan or went out of business, you were left with unsupported software. With Mac OS X, you can use the widely installed, open-source applications that most servers on the Internet use.

You might run a server to provide a service to the rest of the world. Or you might run one because you're developing a system that uses itfor example, a shared calendar/event-planning systemand you want to test it on your local machine and/or network before deploying it. There are all kinds of servers; the following are just two of those available to you as a Mac OS X user.

Apache Web server

Apache is the most popular Web server in the worldthat is, more Web sites use Apache servers than any other. Apache is highly configurable, so it can be adapted to many different situations and specific requirements. Apache comes installed in Mac OS X.

Darwin Streaming Server

The Darwin Streaming Server (http://developer.apple.com/darwin/projects/streaming/) is an open-source version of Apple's QuickTime Streaming Server, allowing you to "broadcast" audio and video content on the Internet.

Using other Unix applications

In addition to the specific applications mentioned above, there are thousands of Unix applications available. Most are free, some are commercial packages, some are open source. With Mac OS X you can use many of these existing applications to monitor network status, analyze data such as Web-server log files, run mailing lists, create Web publishing systems, and more.

Because there are so many, we can't list even a tenth of them. Below are a few that give some sense of the variety available, plus links to places where you can find more.

Ruby on Rails

Ruby is a relatively new programming language, and Ruby on Rails (www.rubyonrails.org) is a framework for building Web applications. While not a server itself, Ruby on Rails provides a comprehensive and tightly integrated collection of tools for use with a Web server and SQL database server. RubyForge offers a software package called RubyGems that simplifies obtaining and installing Ruby software (http://rubyforge.org/projects/rubygems).

Samba Windows file-sharing software

Samba lets you share files from your Macintosh with Windows users over a network. The name Samba comes from SMB (Server Message Block), which is the Windows file-sharing protocol. Mac OS X has included an SMB server (Windows File Sharing) since version 10.2.

SQL database engines

If you are a Mac database user, you have heard of FileMaker Pro, which is a great database with a great user interface. But FileMaker Pro doesn't understand SQL (Structured Query Language), which is what all the big serious databases use. Most database-backed Web sites use SQL databases.

A number of SQL database engines are available for Mac OS X, including MySQL, PostgreSQL, ProSQL, Oracle, Sybase, and others.

Image manipulation with GIMP

GIMP (GNU Image Manipulation Program) (www.gimp.org) is a no-cost Unix version of Photoshop. Even though GIMP is not as powerful as the main commercial alternative, it is free of charge and open source, and it runs on many Unix platforms. Using GIMP requires that you install X Windows.

X Windows

X Windows is the underlying mechanism for providing a graphical user interface on most Unix systems. The graphical interface for Mac OS X uses a different method, Apple's proprietary Aqua interface, but many Unix programs were built to use X Windows. Mac OS X comes with a version of X Windows already installed (as /Applications/X11 ).

A powerful feature of X Windows is that it provides a graphical display on your computer for Unix programs that are running on other machines over the Internet. That is, if you are running X Windows on your Mac, and you have an account on another Unix machine somewhere on the Internet, you may be able to run software on the remote machine and see the graphical display on your Mac. When you run X Windows on your computer, you are running an X Windows server that can provide graphical display services to software running on other machines. For more on Unix programs that use X Windows see www.macgimp.org

Blogs and content-management systems

You can install and run many different software packages for managing online content such as intranets and blogs (as in weblogs , easily editable Web-based journals). For example, the Zope-based Plone content-management system (www.zope.org and http://plone.org) will run on Mac OS X, as can the Movable Type blog publishing system (www.sixapart.com/moveabletype) and many others (http://dmoz.org/Computers/Internet/On_the_Web/Weblogs/Tools/Publishers).

E-mail list management with Mailman or Majordomo

Mailman (www.gnu.org/software/mailman/) and Majordomo (www.greatcircle.com/majordomo) are free, open-source application for managing multiple e-mail lists. To use either, you must set up your Macintosh as an e-mail server. The Mac OS X Server has Mailman already installed. Using either Mailman or Majordomo, you can run dozens of mailing lists, with different configurations for each one. For example, one list may require that new subscribers be added by the list owner, while another may allow anyone to self-subscribe via e-mail. Both applications support list archives and digests as well as many other features.



Unix for Mac OS X 10. 4 Tiger. Visual QuickPro Guide
Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
ISBN: 0321246683
EAN: 2147483647
Year: 2004
Pages: 161
Authors: Matisse Enzer

Similar book on Amazon

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