Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Linux Timesaving Techniques For Dummies.pdf
Скачиваний:
59
Добавлен:
15.03.2015
Размер:
15.98 Mб
Скачать

Monitoring Your System in a Snap

59

two-step: configure (in this case, with all the default options) and make. When loadcode completes, just give yourself superuser privileges and do a make install.

loadcode is just a name that you’ve given to a sequence of commands. The commands execute one after the other, and if something goes wrong, the script just keeps going. You can improve this function by adding a bit of error checking:

function loadcode ()

{

if ( wget -q -O - $1 | tar -zxvf - ) then

cd $(basename $1 .tar.gz) || return 1

./configure && make else

echo “Can’t download $1”

fi

}

Now if something goes wrong, loadcode fails instead of continuing on its merry (and misleading) way. The new version of loadcode shows three ways to check for error conditions:

The first command is now wrapped inside an if statement. If the wget or tar commands fail (that is, if they exit with a result code of 0), loadcode jumps to the else clause and displays a friendly error message. If the wget and tar commands succeed, loadcode jumps into the then clause.

The cd command can fail if the tarball that you’re unpacking doesn’t follow the usual naming convention. To catch that sort of problem, loadcode uses the || (logical or) operator to exit if the cd command fails. You can read that command as “either cd successfully or return 1.”

The configure command can also fail if you don’t have all the prerequisites for the package you’re installing. In this case, loadcode uses the && operator to catch a configuration failure. You can read that command as “configure and, if that succeeds, make.”

Adding a timer to loadcode

Aliases and functions can work together. Here’s an alias that makes your computer beep when loadcode is finished running (assuming you have a properly configured audio card). It’s kind of like the timer on a microwave.

alias beep=’tput bel’

Invoke beep inside the loadcode module, or just use it on the command line, like this:

$loadcode ftp://ftp.gnu.org/gnu/barcode/barcode- 0.98.tar.gz ; beep

You’ll hear a beep when loadcode has finished its work (loadcode can take a while to run if you have a slow network connection).

Monitoring Your System

in a Snap

So far, you’ve seen functions and aliases designed to work with the command line, but you can also spawn graphical programs from the command line. Here is an alias that spawns a new xterm window that displays the output of the top command (sort of a build-it-yourself system monitor):

alias xtop=”xterm -e top &”

After defining this alias, just type xtop to open a new window that runs the top command (see Figure 10-1).

If you want to run a program that has a more complex command line, just define a function. For example, the following function opens a new window that displays the last few lines of a file and continues to display new text as it’s added to the file:

function xtail ()

{

xterm -e “tail -f $1” &

}

60 Technique 10: Keeping Your Life Simple with Aliases and Functions

• Figure 10-1: Running top in its own window.

If you have superuser privileges, you can use xtail to watch the system log file:

# xtail /var/log/messages

You know that the tar command (at least the GNU version of tar) can handle uncompressed, gzipcompressed, and bzip2-compressed archives. It’s easy to create a wrapper function that invokes tar with the right set of flags based on the archive that you give it. The tarls function (see Listing 10-1) uses the file command to determine whether the given archive is compressed and, if so, which compression method was used. tarls is much longer than the other functions in this technique, so we’ve included two timesaving features in this listing:

Local variables that hold intermediate results:

You could rewrite this function without the local variables, but it would be much more timeconsuming to maintain.

Comments that help future maintainers understand our rationale: bash syntax can get awfully cryptic, and you’ll save yourself a lot of time in the future by commenting your code now.

Un-tarring the Easy Way

You can also save time by creating self-adjusting functions that adapt to command line arguments.

LISTING 10-1: CHOOSING THE RIGHT PROGRAM BASED ON FILE TYPE

function tarls ()

{

# Figure what type of file we are working with.

local filetype

# Output from the

file command

local tartype

# $filetype without filename

local

compresstype

# First

word from

$tartype

local

tarflags

# Flags

given to the tar command

#Given an argument like:

#icons.tgz

#filetype will contain

#icons.tgz: gzip compressed data

filetype=$(file “$1” )

#Now strip the leading filename

#from $filetype, leaving

#gzip compressed data

Un-tarring the Easy Way

61

tartype=${filetype#$1:}

#Finally, grab the first word

#from $tartype, leaving

#gzip

compresstype=$(echo $tartype | cut -d ‘ ‘ -f 1 )

case $compresstype in

gzip)

tarflags=-ztvf;;

bzip2)

tarflags=-jtvf;;

POSIX)

tarflags=-tvf;;

*)

echo “Unknown archive type”; return 1;;

esac;

 

tar $tarflags $1

}

Part II

Getting the Most from Your File System

Соседние файлы в предмете Операционные системы