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

Sending printable characters

If you send electronic mail to someone, the message you send must consist only of printable characters. If you wish to send other data you must encode it into a form containing only ordinary text. The reason for this is that some networks interpret some non-printing characters as instructions, which could cause messages to go astray or their contents to be changed. The command uuencode takes a file and writes to standard output a representation of that file containing only ASCII characters; the command uudecode takes a file and performs the reverse operation. Either one or two arguments are needed by uuencode - the second one is the name of the file as it will be known when decoded (which is not necessarily the same as the name of the file you are encoding). The first argument, if there are two, is the file to be encoded (standard input is encoded if there is only one argument). The format of the file after encoding is a sequence of lines commencing with a header line and terminating with end on a line of its own. The header line consists of three fields - the word begin, the access permissions the file should have after it is decoded, and the name of the file after decoding. For example, suppose we have a file A containing an 'alert' character (ctrl-G), and we wish to mail it to sam, and to be received with name chris_file. We can check what the file contains using od, which will confirm that \a (i.e. ctrl-G) is indeed included in chris_file

od -t c A
0000000  h   e   l   l   o  \a  \n
0000007

This file can now be coded using uuencode. Note that the output is sent to standard output:

uuencode A chris_file
begin u=rw,go= chris_file
':&5L;&\'"@CP

(line containing a single blank space)
end

So, to send the encoded file to sam, we merely pipe the output to mailx:

uuencode A chris_file | mailx -s "Binary file" sam

The resulting file can then be recreated by sam storing the mail message in (say) mailfile and typing:

uudecode mailfile

Any lines before begin and after end are ignored by uudecode, so you don't need to worry about any extra header lines the mailer inserts into your message. Try this yourself - choose a file, uuencode it, mail it to a friend, and get them to uudecode it. Have a look at the encoded version, and the final decoded file, and convince yourself that it does in fact work.

Try now encoding a large file, say /usr/dict/words:

uuencode /usr/dict/words tmpfile

Look at the output - it consists of lines of fixed width (61 characters) commencing with the letter M:

   ...
M=&4*87)B;E86P*87)B;W)E='5M"F%R8G5T=7,*87)C"F%R8V%D90I!<F-A
M9&EA"F%R8N80IA<F-A;F4*87)C8V]S"F%R8V-O<VEN90IA<F-H"F%R8VAA
M90IA<F-H8C"F%R8VAA:7-M"F%R8VAA;F=E; IA<F-H8FES:&]P"F%R8VAD
   ...

Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck