|< Day Day Up >|
The first order of business when exploring a new flavor of Unix is to find the command prompt. In Mac OS X, you won't find the command prompt in the Dock or on a Finder menu. Instead, you'll need to use the Terminal application, located in /Applications/Utilities. Don't open it just yet, though. First, drag Terminal's application icon to the Dock so you'll have quick access to it when you need to access the command line. To launch the Terminal, click its icon in the Dock once, or double-click on its icon in the Finder.
After the Terminal starts, you are greeted by the banner message from /etc/motd and a bash prompt, as shown in Figure 1-1.
Figure 1-1. The Terminal window
1.3.1. Launching Terminals
One difference xterm users will notice is that there is no obvious way to launch a new Terminal window from the command line. For example, the Mac OS X Terminal has no equivalent to the following commands:
xterm & xterm -e -fg green -bg black -e pine -name pine -title pine &
Instead, you create a new Terminal window by pressing -N or selecting File New Shell from the menu bar.
You can customize startup options for new Terminal windows by creating .term and .command files.
188.8.131.52. .term files
You can launch a customized Terminal window from the command line by saving some prototypical Terminal settings to a .term file and then using the open command to launch the .term file. (For more information on open, see "The open Command section, later in this chapter.) To create a .term file, open a new Terminal window, and then open the Terminal Inspector (File Show Info or -I) and set the desired attributes, such as window size, fonts, and colors. When the Terminal's attributes have been set, save the Terminal session (File Save or -S) to a .term file, such as proto.term . If you save this file to ~/Library/Application Support/Terminal, you'll be able to launch a new Terminal window with the proto.term file's special attributes from the File Library menu.
Alternatively, you can launch such a Terminal window from the command line by issuing the following command (depending on where you saved proto.term):
open ~/Library/Application\ Support/Terminal/proto.term open ~/Documents/proto.term
The .term file is an XML property list (plist) that you can edit with a text editor like vim (it can be invoked with vi, which is a symbolic link to vim) or with the Property List Editor application (/Developer/Applications/Utilities).[*] By default, opening the .term file creates a new Terminal window. You can configure the window so it executes a command by adding an execution string to the .term file. When you launch the Terminal, this string is echoed to standard output before it is executed. Example 1-1 shows an execution string that connects to a remote host via ssh and exits when you log out.
Example 1-1. An execution string to connect to a remote host
<key>ExecutionString</key> <string>ssh xyzzy.oreilly.com; exit</string>
184.108.40.206. .command files
Adding the .command extension to any executable shell script turns it into a double-clickable executable. The effect is similar to that of a .term file, except that you can't control the Terminal's characteristics in the same way. (A .command file uses the Terminal's default settings.) However, you can stuff the shell script full of osascript commands to set the Terminal characteristics after it launches. The osascript utility lets you run AppleScript from the command line.[*] Example 1-2 is a shell script that sets the size and title of the Terminal, and then launches the pico editor.
Example 1-2. Launching the pico editor
#!/bin/sh # Script RunPico.command osascript <<EOF tell app "Terminal" set number of rows of first window to 34 set number of columns of first window to 96 set custom title of first window to "PICO Editor" end tell EOF pico $@
If you don't want to give the shell a .command extension, you could also use the Finder's Get Info option (File Get Info or -I) to choose which application opens the executable. To do this, perform the following steps:
You can assign a custom-made icon to your shell scripts, and place them in the right section of the Dock. You can also drag the executable's icon to the lower section of the Finder's Sidebar, although this section of the Finder is intended primarily for quick access to frequently visited folders. To change an icon, use the following procedure:
To add the shell script application to the Dock, locate the application in the Finder and drag its icon to the Dock. Now you can click on the script's Dock icon to invoke the script.
1.3.2. Split Screen Terminal Feature
You can split a Terminal window (see Figure 1-2) into upper and lower sessions by clicking on the small broken rectangle located just above the Terminal's scroll bar. This feature is useful, for example, if you need to edit a file and copy and paste output from earlier in the Terminal session. The upper window contains the buffer (i.e., what you would see if you scrolled up in a non-split window), while the lower window contains your current Terminal section.
1.3.3. Contextual Menu
Users familiar with the X Window System know that right-clicking an xterm window opens a terminal-related contextual menu . Mac OS X's Terminal also has a contextual menu that can be accessed with Control-clicking (or
Figure 1-2. The Terminal's split screen
right-clicking if you have a two- or three-button mouse). The Terminal contextual menu includes the following choices: Copy, Paste, Paste Selection, Paste Escaped Text, Select All, Clear Scrollback, Send Break (equivalent to Control-C), Send Hard Reset, Send Reset, and Windows Settings. Each of these items also has a keyboard shortcut.
|< Day Day Up >|