⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unx31.htm

📁 Linux Unix揭密.高质量电子书籍.对学习Linux有大帮助,欢迎下载学习.
💻 HTM
📖 第 1 页 / 共 2 页
字号:

<PRE>&quot;Buried how long?&quot;

The answer was always the same: &quot;Almost eighteen years.&quot;

&quot;You had abandoned all hope of being dug out?&quot;

&quot;Long ago.&quot;

&quot;You know that you are recalled to life?&quot;

&quot;They tell me so.&quot;

&quot;I hope that you care to live?&quot;

&quot;I can't say.&quot;

Charles Dickens, A Tale of Two Cities</PRE>

<P>and use uuencode on it, it looks like this:

<BR></P>

<PRE>begin 644 dickens

MU&lt;FEE9&quot;!H;W&lt;@;&amp;]N9S\B&quot;E1H92!A;G-W97(@=V%S(&amp;%L=V%Y&lt;R!T:&amp;4@

M&lt;V%M93H@(D%L;6]S=&quot;!E:6=H=&amp;5E;B!Y96%R&lt;RXB&quot;B)9;W4@:&amp;%D(&amp;%B86YD

M;VYE9&quot;!A;&amp;P@:&amp;]P92!O9B!B96EN9R!D=6&lt;@;W5T/R(*(DQO;F&lt;@86=O+B(*

M(EEO=2!K;F]W('1H870@&gt;6]U(&amp;%R92!R96-A;&amp;QE9&quot;!T;R!L:69E/R(*(E1H

M97D@=&amp;5L;&quot;!M92!S;RXB&quot;B))(&amp;AO&lt;&amp;4@=&amp;AA=&quot;!Y;W4@8V%R92!T;R!L:79E

M/R(*(DD@8V%N)W0@&lt;V%Y+B(*&quot;D-H87)L97,@1&amp;EC:V5N&lt;RP@02!486QE(&amp;]F

,(%1W;R!#:71I97,*

 

end</PRE>

<P>The first line of the uuencode file lists the permissions that the file will have and the name it will have once it's extracted (see Figure 31.3).

<BR></P>

<P>

<BR><B><A HREF="31unx03.gif">Figure 31.3. First line of file created by </B><B><I>uuencode</I></B><B>.</A></B>

<BR></P>

<P>If you receive in the mail a file on which uudecode has been used, you can retrieve the original file using the reverse process of what has been described. Strip the mail header off until you get to just the file on which uudecode has been used (so that 

it looks like what is shown in the example). Then use the uudecode file to extract the original file.

<BR></P>

<PRE>boson% uudecode dickens.uu

boson% ls dickens*

dickens

dickens.uu</PRE>

<P>Notice that the file on which uuencode was used will still be there. The uudecode command does not decode &quot;in place&quot; the way uncompress decompresses in place. Instead, it extracts to whatever filename you included in the uuencode process.

<BR></P>

<H3 ALIGN="CENTER">

<CENTER><A ID="I5" NAME="I5">

<FONT SIZE=4><B>Shell Archives</B>

<BR></FONT></A></CENTER></H3>

<P>Another common format for archives is the shell archive. Shell archives are also called shar files and have, by convention, the extension .shar. They are very different from tar archives in that they do not allow inclusion of executables. They also do 
not include any of the file descriptive information, such as permissions and ownership. Shar files are just text files with the shell commands for extracting the original files embedded between the text of the files themselves.

<BR></P>

<P>Shar files are extracted using the Bourne shell command, sh. It is easy to create a shar file and it is easy to extract from one. It is likewise easy to create a script that creates shell archives.

<BR></P>

<P>The basic &quot;trick&quot; in creating shar files is knowing how to use what is known as the &quot;here&quot; document. In the Bourne shell, the operator &lt;&lt; instructs the shell to accept input until it encounters a given string (which you 
provide) and uses this input as input to a command.

<BR></P>

<P>Type these commands on your system:

<BR></P>

<PRE>echo Extracting File from Shell Archive

cat &gt; dickens &lt;&lt; TheEnd

&quot;Buried how long?&quot;

The answer was always the same: &quot;Almost eighteen years.&quot;

&quot;You had abandoned all hope of being dug out?&quot;

&quot;Long ago.&quot;

&quot;You know that you are recalled to life?&quot;

&quot;They tell me so.&quot;

&quot;I hope that you care to live?&quot;

&quot;I can't say.&quot;

Charles Dickens, A Tale of Two Cities

TheEnd</PRE>

<P>You get a file dickens with the content specified between the cat command and the TheEnd marker. If you imbed these same commands in an executable file and invoke it, you get the same thing. You can, therefore, create files including such sequences and 

provide them to other people so that they can extract your original files using a command like this:

<BR></P>

<PRE>myhost% /bin/sh anyname.shar</PRE>

<P>Better still, you can create a script which takes any file you want to share and wraps it in the appropriate here document commands. To create such a script, you first need to include the here document commands. You can easily modify the commands you 
entered above to read:

<BR></P>

<PRE>echo &quot;echo Extracting File from Shell Archive&quot;

echo &quot;cat &gt; dickens &lt;&lt; TheEnd&quot;

cat dickens

echo TheEnd</PRE>

<P>You can then insert them into your shell script.

<BR></P>

<P>This command sequence looks a little peculiar, but you need to examine it closely to understand what it is doing. First, it creates the line echo Extracting File from Shell Archive. Next it adds the line cat &gt; dickens &lt;&lt; 'TheEnd' to the file. 
This is the command that is going to create the file dickens when the extraction is done. It will cause data following this line to be read until the line TheEnd is encountered. Then you actually use cat to add the file to the archive, followed by the end 

marker you selected, 'TheEnd'.

<BR></P>

<P>To make this script general-purpose, you should replace the specified filename with an argument.

<BR></P>

<PRE>echo &quot;echo Extracting File from Shell Archive&quot;

echo &quot;cat &gt; $1 &lt;&lt; TheEnd&quot;

cat $1

echo TheEnd</PRE>

<P>You can then use this script like this to create a shar archive from any file. Make sure that your script is executable and redirect its output to the file that you will share.

<BR></P>

<PRE>mk_shar dickens &gt; dickens.shar</PRE>

<P>So, here's what the archive, dickens.shar, will look like when you're done:

<BR></P>

<PRE>echo Extracting File from Shell Archive

cat &gt; dickens &lt;&lt; 'TheEnd'

&quot;Buried how long?&quot;

The answer was always the same: &quot;Almost eighteen years.&quot;

&quot;You had abandoned all hope of being dug out?&quot;

&quot;Long ago.&quot;

&quot;You know that you are recalled to life?&quot;

&quot;They tell me so.&quot;

&quot;I hope that you care to live?&quot;

&quot;I can't say.&quot;

Charles Dickens, A Tale of Two Cities

TheEnd</PRE>

<P>When you extract this file, you will get a file dickens.

<BR></P>

<PRE>boson% sh dickens.shar</PRE>

<P>Notice that you can include multiple files in the same shar archive by using the append operator, &gt;&gt;.

<BR></P>

<PRE>mk_shar dickens2 &gt;&gt; dickens.shar</PRE>

<P>Clearly, you can string together multiple files in this way, creating a very useful archiving method since you can group together related files in a text-only format that clearly remembers the filenames and marks their beginnings and endings. Shell 
archives can also be read on just about any UNIX system. It would be surprising if you found any UNIX system without the Bourne shell.

<BR></P>

<P>Shar files, obviously, do not save any space. Since you have the original files plus some overhead for packing them in the simple structure of extract commands, text, and end-of-file markers, the resultant archive is somewhat larger than the original 
files. Generally, the extra length is considerably less than the extra space taken by using uuencode.

<BR></P>

<P>Shar files are nice because it is obvious what you're getting. You can easily examine them before extracting from them to be sure that this is what you want. You can check out the filenames and look for extraneous commands that you might not want to 
execute. Keep in mind that &quot;stray&quot; commands included in an archive when you extract from it will also be executed, provided that they are not within the beginning and end markers of a here document.

<BR></P>

<P>In any case, you should always examine shar files before extracting them, even if they're from someone you trust (that person may have gotten them from somewhere else). The following simple awk script could be used to quickly scan through a shell 
archive, looking for commands that are extraneous and possibly sinister. It looks for the beginning and the end of each here document and prints anything not enclosed within these documents. If used against the dickens.shar file presented in this chapter, 

it would print the string echo Extracting File from Shell Archive.

<BR></P>

<HR ALIGN=CENTER>

<NOTE>

<IMG SRC="note.gif" WIDTH = 35 HEIGHT = 35><B>NOTE:</B> Note that this particular awk script expects the filenames of the extracted files to contain only alphabetic and numeric characters. You can expand this expression if necessary.

<BR></NOTE>

<HR ALIGN=CENTER>

<PRE>#

BEGIN {OK = &quot;OFF&quot;}

$0 ~ /^cat &gt; [A-Za-z0-9]+ &lt;&lt;/ { OK = &quot;ON&quot;;TERMINATOR = $5 }

{

if (OK == &quot;OFF&quot;)

     print $0

if ($0 == TERMINATOR) {

     OK = &quot;OFF&quot;

}

}</PRE>

<H3 ALIGN="CENTER">

<CENTER><A ID="I6" NAME="I6">

<FONT SIZE=4><B>Summary</B>

<BR></FONT></A></CENTER></H3>

<P>The commands that UNIX provides for archiving your files allow you to recover from disastrous mistakes, as well as conveniently share files with strangers who will not need to know anything about your systems (except how to access them) to make use of 
them.

<BR></P>

<P>Almost no one archives files too often. Regular use of the commands described in this chapter will help you manage your systems.

<BR></P>

<P><A HREF="unx30.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>

<A HREF="index.htm"><IMG SRC="blutoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>

<A HREF="unx32.htm"><IMG SRC="blunext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>

<A HREF="index.htm"><IMG SRC="bluprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Home"></A>

</P></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -