home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 19.1 Packing Up and Moving Chapter 19
Creating and Reading Archives
Next: 19.3 unshar: Unarchive a Shell Archive
 

19.2 Introduction to Shell Archives

A shell archive or shar file is a single file that contains one or more other files. Files are extracted from the archive with the standard UNIX Bourne shell (44.3 ) . A shell archive usually doesn't let you save and restore complete directory hierarchies like cpio (19.9 ) and tar (19.5 ) do, but it is completely portable and, as a result, is used extensively on Usenet, an international network with many UNIX systems.

shar
In the Bourne shell, the operator << (8.18 ) means to take the following lines, up to a specified string, as input to a command. (This is often called a here document .) Using this syntax and the cat (25.2 ) and echo (8.6 ) commands, you can write a simple shell archiver (shar ) like the one below. Incidentally, many systems already have shar programs in place; there are several freely available versions, including the one on the CD-ROM. Just about any of them are likely to be more sophisticated than the version shown here - but this version shows the essence of how they work:


for
 


<<
 


#!/bin/sh
for file
do
    echo "echo restoring $file"
    echo "cat > $file << 'XxXxXxXxXx-EOF-XxXxXxXxXx'"
    cat $file
    echo "XxXxXxXxXx-EOF-XxXxXxXxXx"
done

The string XxXxXxXxXx-EOF-XxXxXxXxXx is entirely arbitrary - it just needs to be a string that won't otherwise appear in the input and can be used by the shell to recognize when the here document is finished.

When you give shar a list of filenames, it will string those files together on standard output, separating them with that arbitrary string and the commands to split them up again. Simply redirect this output stream to a file to create the archive. For example, the command:

$ shar file1 file2 > archive.shar

will produce a file called archive.shar that contains the following data:


'...'
 










echo restoring file1
cat > file1 << 'XxXxXxXxXx-EOF-XxXxXxXxXx'
   ...
Text of file1 will be stored here

   ...
XxXxXxXxXx-EOF-XxXxXxXxXx
echo restoring file2

cat > file2 << 'XxXxXxXxXx-EOF-XxXxXxXxXx'
   ...
Text of file2 will be stored here

   ...
XxXxXxXxXx-EOF-XxXxXxXxXx

When this archive is run through sh , the commands it contains will be executed. Each here document (the lines from each cat up to the next XxXxXxXxXx-EOF-XxXxXxXxXx ) will be output to a file:

$ sh archive.shar


restoring file1
restoring file2
$ ls


archive.shar
file1
file2

The unshar (19.3 ) program does essentially the same thing.

NOTE: You should never blindly run a shell archive supplied by someone you don't know personally. An unscrupulous prankster could easily include a "Trojan horse" command (like rm * ) in the middle of a seemingly innocuous archive, and cause you a lot of trouble. An easy way to do this is by browsing through the archive with the search command in a program like more (25.3 ) . Use the search command (in more , the command is / ) to find each end-of-file string (like XxXxXxXxXx ); look carefully at the commands between it and the cat that starts the next file. Of course, if the files in the shell archive are programs themselves, you should also check them before they're executed.

- TOR


Previous: 19.1 Packing Up and Moving UNIX Power Tools Next: 19.3 unshar: Unarchive a Shell Archive
19.1 Packing Up and Moving Book Index 19.3 unshar: Unarchive a Shell Archive

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System