O'Reilly NetworkO'Reilly.com
BooksAll ArticlesSafari BookshelfO'Reilly GearFree NewslettersSearch
advertisement

 O'Reilly Emerging Telephony Conference January 24-26 2006, San Francisco, CA

Content:
All Articles
Apache and Web Serving
AppleScript
Cocoa Programming
Database and Sys Admin
Designing for Aqua
Developing for Mac OS X
Digital Photography
Email and Related
Java on Mac OS X
Mac Weblogs
Mac OS X Conference
Mac OS X Innovators
Music and iTunes
Networking and Wireless
Product Reviews
QuickTime and iMovie
Terminal App and Unix
Switch to OS X




O'Reilly Macintosh books

 

Running Mac OS X Tiger

Mac OS X Tiger in a Nutshell

FileMaker Pro 8: The Missing Manual

iWork '05: The Missing Manual

Switching to the Mac: The Missing Manual, Tiger Edition

iLife '05: The Missing Manual

iPod Shuffle Fan Book

GarageBand 2: The Missing Manual

Podcasting Hacks

Mac OS X: The Missing Manual, Tiger Ed

Degunking Your Mac, Tiger Edition

Mac OS X Tiger Pocket Guide

Learning Unix for Mac OS X Tiger

Mac OS X Tiger for Unix Geeks

Essential Mac OS X Panther Server Administration

Making Music on the Apple Mac

iMovie HD & iDVD 5: The Missing Manual

iPod and iTunes: The Missing Manual

iPhoto 5: The Missing Manual

Office 2004 for Macintosh: The Missing Manual

AppleScript: The Missing Manual

All About Your iPod Photo (PDF)

Revolution in The Valley

Mac Annoyances

iBook Fan Book

PowerBook Fan Book

Cult of Mac

iPod and iTunes Hacks

Modding Mac OS X

Mac OS X Power Hound

 





Print.Print
    Add to Project.Add to Project
 
Tags: unix tiger macosx terminal mac
Bookmark with del.icio.us
 

An Introduction to Tiger Terminal, Part 4

by Mary Norbury-Glaser
09/09/2005

In parts one, two, and three of this series, we covered basic commands that can be executed through the OS X Tiger Terminal app. Today, I'll take a bit of a leap and introduce shell scripting.

What are shell scripts, and why would you want to create them? Well, if you begin to use the Terminal with regularity, you'll find that you are repeating many of the same commands, often in sequence, in order to accomplish some task. You can automate these tasks by writing a shell script file.

What You Need To Know--And Do

In part one of this series, we ran the cat /etc/shells command to see a list of shells available in Mac OS X Tiger. Each shell has a standard scripting language associated with it. Since our default shell in Mac OS X Tiger is bash, we'll stick with that for this scripting tutorial.

You'll also need a text editor to write your scripts. In part one, I introduced Mac OS X Tiger's new default text editor, nano, which we'll use here to write and save our new shell scripts. You can use any text editor, whether it's a command-line editor or a graphical editor like SubEthaEdit (www.codingmonkeys.de/subethaedit) or BBEdit (www.barebones.com/products/bbedit/index.shtml). If you choose a graphical editor, make sure you select the option to use Unix line breaks when saving files.

Let's assume you'll be creating shell scripts on a regular basis, so it's the best practice to create a directory where you'll save all your scripts. Create a new folder in your /Documents directory and name it scripts. From the Terminal command line, now!

norburym15:~ norburym$ cd Documents
norburym15:~/Documents norburym$ mkdir scripts
norburym15:~/Documents norburym$

Remember that your default location when you open the Terminal app is your home directory. Here, you've changed into your Documents directory and then created a new directory (folder) called scripts, using the mkdir (make directory) command. The Terminal doesn't tell you it's been created nor does it move you into the directory automatically. Run the ls command at the last prompt above to see that the new directory has, indeed, been created:

figure
Figure 1. ~/Documents/scripts folder

The next thing you'll want to do is tell the shell where to find this directory. You'll do that by adding the location of your new scripts directory to the PATH variable in your .bash_profile file. Huh? OK, good time for an aside! When you open the Terminal app, the default shell, bash, starts up and reads some files in order to get configuration information. The primary files are bashrc and profile, which live in the system /etc directory (partial listing shown below):

figure
Figure 2. bash files

However, to set user-specific configuration variables, it's best to create a file in your own home directory that bash will read when it starts up. bash will read the system-wide files first, and then the user-specific ones. The user-specific file settings will override those in the system-wide files.

You can see your current PATH by executing the echo $PATH command at the shell prompt:

norburym15:~ norburym$ echo $PATH
/bin:/sbin:/usr/bin:/usr/sbin
norburym15:~ norburym$
Mac OS X Tiger for Unix Geeks

Related Reading

Mac OS X Tiger for Unix Geeks
By Brian Jepson, Ernest E. Rothman

Table of Contents
Index
Sample Chapter

Read Online--Safari
Search this book on Safari:
 

Code Fragments only

Pages: 1, 2, 3, 4, 5

Next Pagearrow







Sponsored by:


Contact UsMedia KitPrivacy PolicyPress NewsJobs @ O'Reilly
Copyright © 2000-2005 O’Reilly Media, Inc. All Rights Reserved.
All trademarks and registered trademarks appearing on the O'Reilly Network are the property of their respective owners.
For problems or assistance with this site, email