<<

. 26
( 69 .)



>>




‹ Type echo Today is `date ˇ Type cat message | sed “f ° Type echo "s/go to
USING SED IN A SCRIPT
+%a` and press Return. changes to replace portions of work/stay home/" > changes
„ Start the Pico editor to the message text and press and press Return, then type
› Type if ( `date +%a` ==
create a file named what2do.
Return, then type else and echo "s/take your/make me
"Sat" || `date +%a` == "Sun" )
¤ Type #!/bin/tcsh and press press Return again. some/" >> changes and press
then and press Return. Return again.
Return twice.
Á Type cat message and
· Type tcsh ./what2do and
press Return, then type endif
and press Return again. press Return.
– The script tells you to stay
‡ Save your file, and exit
Pico. home if it is a weekend day.

119
UNIX FOR MAC


SCHEDULE SCRIPTS TO
RUN AUTOMATICALLY
Time fields can include a string of comma-separated values

Y
ou can schedule scripts or other Unix commands to run
or an asterisk (*). An asterisk indicates that every possible
automatically at a time you specify. This can keep you
value for the field is valid. In the day-of-week field, for
from forgetting to run routine scripts and can allow you
example, * tells cron to run the command every day of the
to schedule big jobs to run when the system is not busy.
week. Typing 0,30 in the minutes field tells cron to run the
command on the hour and half hour. You can also specify
Use the cron command to schedule the execution of
values such as */10 in the minutes field to tell cron to run
commands or scripts. Cron makes use of files called
the command every ten minutes.
crontab files that specify what time commands and scripts
are to be run. All crontab files have the following format:
A job scheduled to run via cron only runs if all of the
timing criteria are met. For example, the line 5 17 * * 5
min hr day month day-of-week command-to-run
/Users/user/bin/send_weekly runs the
The first field represents the minutes, and the second send_weekly script every Friday at 5:05 p.m.
represents the hours. If you want a script to run at 2:15 p.m.
To schedule jobs to run automatically, use the crontab -e
every day, enter 15 14 for these fields. The entry 8 1 in the
day and month fields means January 8th. The day-of-week command and add a line for each command or script you
want to schedule.
field must be a number between 0 and 6; Sunday is 0,
Monday is 1, and so on.

SCHEDULE SCRIPTS TO RUN AUTOMATICALLY




„ Start the Pico editor to › Type set diskspace = Á Type crontab -e and press
create a file named `/usr/bin/du “sk ˜$USER` and Return.
check_space. press Return, then type echo
$when $diskspace >>
¤ Type #!/bin/tcsh and press ˜$USER/dulog.
Return twice.
ˇ Save your file, and exit
‹ Type set when = `date Pico.
+%H:%M` and press Return.

120
8
AUTOMATE SHELL TASKS

Cron stores files in /var/cron/tabs,
and each file is named after the user
who owns it. The cron process starts
when the system starts up and is one TYPE THIS IN YOUR CRONTAB FILE:
of the processes that runs all of the
*/5 * * * 7 /bin/echo hello > /dev/ttyp1
time. Such processes are referred to as
daemons. The format of crontab
entries is fairly rigid.
If you do not include the required five RESULT:
timing fields, the system does not set
up your cron jobs and asks when you Cron does not let you save the file. The 7 in the day-of-week
try to save the changes if you want to field is invalid. Only a value between 0 (Sunday) and 6
retry your edits. If you do not retry (Saturday) is allowed in this field. Change it to the proper
your changes, cron leaves your digit for the current day of the week and "hello" messages
crontab file unchanged. You should should appear in your Terminal window after a few minutes.
never edit cron files except through To inactivate the line without removing it, use the crontab
the crontab -e command, and the “e command again and type a pound sign (#) in column 1.
system protects against this by setting
the permissions of the cron files so
that you can only edit them as root
and, of course, the crontab process.




‡ Type */5 * * * * ° Save your cron file, and · Type chmod a+x ‚ Type crontab “l and press
exit the editor. check_space and press Return.
/Users/user/check_space >
/dev/null, replacing user Return.
– Your scheduled job
with your username.
appears.




121
UNIX FOR MAC



OPEN AQUA APPLICATIONS
FROM THE SHELL
from the period (.) on. For example, the filename my-dog.jpg

Y
ou can use the shell command open to open a file
has an extension of .jpg. Mac OS X uses the default
in an Aqua application such as TextEdit, Preview,
application for opening .JPG files. This is the same method
Photoshop, or Microsoft Word. The file opens as if
the Mac OS X Finder uses to determine which application to
you had double-clicked it in the Mac OS X Finder. There
use when you double-click the icon of a document.
are three ways to use the open command:
Not all Unix text files end with the .txt extension. An HTML
open filename
file may be named about-cat.html. If you use open, the file
open -e filename
opens in a Web browser, but if you want to edit it, you may
open -a Application-name filename
want to use TextEdit. The second form of the open
command, with the -e option, opens the file in TextEdit,
The first version of this command launches an Aqua
which lets you edit text files not ending in the .txt
application to open the designated filename. Mac OS X
extension, such as HTML pages or configuration files.
associates each document with information known as
metadata. This metadata includes information such as
The third version of the command lets you specify which
which Aqua application created the file. The open
application to use. You can name any application located in
command launches that application.
the /Applications directory or its subdirectories. You can
start the application with a blank file by omitting the
If the application that created the file is not listed in the
filename argument.
metadata, then Mac OS X looks at the extension of the file.
The extension is the last part of the filename that appears


OPEN AQUA APPLICATIONS FROM THE SHELL




– Aqua starts the appropriate
¤ Type the name of an
OPEN AN IMAGE FILE
image file and press Return. viewer program and displays
„ Type open and a space. the image.


122
9
COMBINE UNIX AND AQUA




The open command can also open Web URLs in
The open command is not limited to only
the default Web browser you have specified in your
opening files. If you provide other arguments,
System Preferences on the Internet panel. To open,
the open command tries to display those
type your URL as the argument.
arguments in the appropriate viewer or editor.
One of the most useful ways to employ open is [ferro:˜] user% open http://cssin24hours.com/
to open directories in the Mac OS X Finder. This
You may need to enclose the Web URL in
command creates a Finder folder window to
quotation marks to prevent the shell from
open the current directory.
interpreting question marks and other special
characters.
[ferro:˜/Pictures] user% open

You can also send e-mail to an address by using a
mailto: URL. This command starts your default
mail program and sends mail to the specified
address.

[ferro:˜] user% open mailto:kynn@idyllmtn.com




– The TextEdit application
¤ Type -e and a space.
OPEN A TEXT FILE IN TEXTEDIT
launches and opens the text
„ Type open and a space. ‹ Type the name of a text file.
file and press Return.


123
UNIX FOR MAC




CAPTURE A SCREENSHOT FROM UNIX
The screencapture shell command gives you more

Y
ou can create a screenshot of your current desktop and
control over your screenshots, allowing you to save them
application windows by using the screencapture
wherever you wish and with any filename. The syntax for
command. A screenshot is simply a graphics file that
this command is
shows the state of your display at any given time. You can use
a screenshot for troubleshooting or sharing what you see
screencapture options filename.pdf
with someone else who does not have direct access to your
computer. Screenshots are also useful for step-by-step If you use the -m option, the screenshot captures the entire
instructions, such as those used extensively in this book. main screen. The -i option puts the screencapture
command into Interactive mode. In Interactive mode, you
You can create a screenshot at any time in Mac OS X by
can define an area of the screen to capture by dragging the
holding down the Shift + Command (”) keys while typing
mouse. Typing a space in Interactive mode switches the
the number 3. This key sequence captures whatever is
selection from mouse selection to window selection; if you
currently on the entire screen and saves it to a file in your
click a window in Window Selection mode, that entire
Desktop directory. The file is named Picture 1.pdf and is in
window is captured.
the Portable Document Format (PDF) developed by Adobe.
The Mac OS X Preview application or the Adobe Acrobat
Reader program can read PDF files. If you create more
screenshots in this manner, they receive successive
filenames, such as Picture 2.pdf and Picture 3.pdf.



CAPTURE A SCREENSHOT FROM UNIX




„ Type screencapture and a ‹ Type a filename ending in ¤ Type a filename ending in
CAPTURE A SECTION
OF THE SCREEN
space. .pdf and press Return. .pdf and press Return.
„ Type screencapture -i and
– Your screenshot is saved in – The cursor becomes a
¤ Type -m and a space.
a space.
the PDF file. crosshair pointer.

<<

. 26
( 69 .)



>>