Выбрать главу

Although this has been a mere taste of screen, we hope you can see how useful it can be.

Date and Time

The Fedora installer will query you during installation for default time zone settings, and whether your computer's hardware clock is set to Greenwich mean time (GMT) — more properly known as UTC or coordinated universal time.

Linux provides a system date and time; your computer hardware provides a hardware clock-based time. In many cases, it is possible for the two times to drift apart. Linux system time is based on the number of seconds elapsed since January 1, 1970. Your computer's hardware time depends on the type of clock chips installed on your PC's motherboard, and many motherboard chipsets are notoriously subject to drift.

Keeping accurate time is not only important on a single workstation, but also critically important in a network environment. Backups, scheduled downtimes, and other network-wide actions need to be accurately coordinated.

Fedora provides two date and time utilities you can use at the command line:

► date — Used to display, set, or adjust the system date and time from the command line

► hwclock — A root command to display, set, adjust, and synchronize hardware and system clocks

Using the date Command

Use the date command to display or set your Linux system time. This command requires you to use a specific sequence of numbers to represent the desired date and time. To see your Linux system's idea of the current date and time, use the date command like this:

# date

Wed Jan 10 14:17:01 EDT 2005

To adjust your system's time (say, to January 27, 2006 at 8 a.m.), use a command line with the month, day, hour, minute, and year, as follows:

# date 012606002003

Fri Jan 27 08:00:00 EDT 2003

Using the hwclock Command

Use the hwclock command to display or set your Linux system time, display or set your PC's hardware clock, or to synchronize the system and hardware times. To see your hardware date and time, use hwclock with its --show option like so:

# hwclock --show

Fri 27 Jan 2006 02:17:53 PM GMT -0.193809 seconds

Use hwclock with its --set and --date options to manually set the hardware clock like so:

# hwclock --set --date "01/27/06 08:00:00"

# hwclock --show

Tue 27 Jan 2006 08:00:08 AM GMT -0.151718 seconds

In these examples, the hardware clock has been set with hwclock, which is then used again to verify the new hardware date and time. You can also hwclock to set the Linux system date and time date, using your hardware clock's values with the Linux system date and time.

For example, to set the system time from your PC's hardware clock, use the --hctosys option as follows:

# hwclock --hctosys

Capturing Screen Images

Although you're working at the command line rather than a swish GUI, you can still grab images from your graphical desktop through the import command, and that's exactly what we did to capture the images for this book.

First, we ran xhost + on the remote computer to allow another computer access to the X server on the remote machine; the command to take the screenshot is run on our local machine like this:

$ import -window root -display 192.168.2.4:0 12fig07.jpg

This utility made a difficult task easy because the publisher required the screenshots be done from an 800×600 screen (too small to comfortably work in) to accommodate its printing and production equipment.

We could also have used the classic UNIX tool xwd to take screenshots. The command would have looked similar to that of import:

$ xwd -root -display 192.168.2.4:0 -out 12fig07.jpg

Reference

► http://www.gnu.org/ — The website of the GNU project, it contains manuals and downloads for lots of command-line software.

► http://www.linuxdevcenter.com/linux/cmd/ — A wide selection of Linux commands and explanations of what they do.

► http://www.tuxfiles.org/linuxhelp/cli.html — Several short command-line tutorials, courtesy of tuXfiles.

► http://www.linuxcommand.org/ — Describes itself as "your one-stop command line shop!" It contains a wealth of useful information about the console.

► http://www.tripwire.org/ — Information and download links for the open source version of Tripwire.

Books

Understanding the way UNIX works at the nuts-and-bolts level can be both challenging and rewarding, and there are several good books that will help guide you on your way. Perhaps the best is The Art of UNIX Programming, by Eric Raymond (Addison-Wesley, ISBN: 0-13-142901-9), which focuses on the philosophy behind UNIX and manages to mix in much about the command line.

O'Reilly's UNIX CD Bookshelf (ISBN: 0-596-00392-7) contains seven highly respected books in one, although it retails for more than $120 as a result! That said, it is incomparable in its depth and breadth of coverage.

CHAPTER 33

Writing and Executing a Shell Script

Why should you write and use shell scripts? Shell scripts can save you time and typing, especially if you routinely use the same command lines multiple times every day. Although you could also use the history function (press the Up or Down keys while using bash or use the history command), a shell script can add flexibility with command-line argument substitution and built-in help.

Although a shell script doesn't execute faster than a program written in a computer language such as C, a shell program can be smaller in size than a compiled program. The shell program does not require any additional library support other than the shell or, if used, existing commands installed on your system. The process of creating and testing shell scripts is also generally simpler and faster than the development process for equivalent C language commands.

NOTE

Hundreds of commands included with Fedora are actually shell scripts, and many other good shell script examples are available over the Internet — a quick search yields numerous links to online tutorials and scripting guides from fellow Linux users and developers. For example, the startx command, used to start an X Window session from the text console, is a shell script used every day by most users. To learn more about shell scripting with bash, see the Advanced Bash-Scripting Guide, listed in the "Reference" section at the end of this chapter. You will also find Sams Teach Yourself Shell Programming in 24 Hours a helpful guide to learning more about using the shell to build your own commands.