Main index

Introducing UNIX and Linux


Advanced shell programming

Overview
Sending and trapping signals
      Signal names
Functions
Aliases
The 'exec' mechanism
The 'eval' mechanism
Sending data across networks
      Sending printable characters
      Splitting files
Makefiles
Safe programming
Setting up a terminal
More on files
Miscellaneous utilities
Summary
Exercises

Miscellaneous utilities

Although the structure of files on a UNIX machine is uncomplicated, other operating systems may impose a more complex structure on how their files are represented. If you need to convert a file, either to export it to, or to import it from, a non-UNIX system, use dd ('disk-to-disk'). For example, some systems require that files be structured as having a sequence of fixed-size blocks, or might use a different character set to ASCII. This command can also perform simple translation of characters - for instance, if you received a file funny, which contains only upper-case letters, then dd can create a file with lower-case letters in place of the upper-case ones:

dd conv=lcase < funny

You can try this. More seriously, if you do need to read from or write to a file to be used on a non-UNIX system, you should examine the manual page for dd carefully.

Suppose you wished to run a utility with the arguments to that utility piped to it. This might be the case if the arguments were to be split over several lines. A simple example might be if you had a file list containing filenames, and you wished to ls -l each of them. Using the mechanisms so far discussed, the resultant script would be inelegant:

X=$( cat list ); eval ls -l $X

By use of the $( ... ) mechanism, we have concatenated all the lines of list into a single string, and have passed that string to ls -l. The utility xargs can help: it takes as its arguments a command, and then reading from its standard input appends options to that command, and then runs it. The above example would then become:

xargs ls -l <list

A more serious, and frequently quoted example of the use of xargs is in conjunction with find, where with the -exec argument find might create a large number of processes. Suppose your home directory contains a large number of subdirectories, and you wished to perform ls -ld on each of them. One possibility would be:

find ~ -type d -exec ls -ld {} \;

but this would create as many processes as directories - it is inefficient. More effective - and quicker - would be:

find ~ -type d -print | xargs ls -ld

The final three commands that are mentioned in this chapter are introduced for completeness; they are included in the POSIX standard, and you should know of their existence.

There is a command logger ('log error message') which can be used to save a message for reading later on by the system administrator. It might be used (say) to inform the administrator if a batch job failed to read a system file correctly; the user would not be in a position easily to forward the message, and the action to be taken would definitely be for the administrator to perform. Unless your administrator has given you specific instructions on how it should be used, you are probably advised not to use logger.

Although you are likely to be using UNIX where English is a normal medium of communication, the concept of locale is supported whereby both messages from commands and the character set used can be customised to other languages. The command locale allows you to examine the current locale, and localedef to define a new locale.


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck