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

📄 zeros.html

📁 Shall高级编程
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Of Zeros and Nulls</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Advanced Topics"HREF="part5.html"><LINKREL="PREVIOUS"TITLE="/proc"HREF="procref1.html"><LINKREL="NEXT"TITLE="Debugging"HREF="debugging.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="procref1.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="debugging.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="ZEROS"></A>Chapter 28. Of Zeros and Nulls</H1><P><ANAME="ZEROSREF"></A></P><DIVCLASS="VARIABLELIST"><P><B><ANAME="ZERONULL1"></A><TTCLASS="FILENAME">/dev/zero</TT>          and <TTCLASS="FILENAME">/dev/null</TT></B></P><DL><DT><ANAME="DEVNULLREF"></A>Uses of	    <TTCLASS="FILENAME">/dev/null</TT></DT><DD><P>Think of <TTCLASS="FILENAME">/dev/null</TT> as a <SPANCLASS="QUOTE">"black		hole."</SPAN> It is the nearest equivalent to a		write-only file. Everything written to it disappears		forever. Attempts to read or output from it result in		nothing. Nevertheless, <TTCLASS="FILENAME">/dev/null</TT>		can be quite useful from both the command line and in		scripts.</P><P>Suppressing <TTCLASS="FILENAME">stdout</TT>.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;cat $filename &#62;/dev/null   2&nbsp;# Contents of the file will not list to stdout.</PRE></TD></TR></TABLE>            </P><P>Suppressing <TTCLASS="FILENAME">stderr</TT>	      (from <AHREF="moreadv.html#EX57">Example 15-3</A>).	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;rm $badname 2&#62;/dev/null   2&nbsp;#           So error messages [stderr] deep-sixed.</PRE></TD></TR></TABLE>	    </P><P>Suppressing output from <SPANCLASS="emphasis"><ICLASS="EMPHASIS">both</I></SPAN>	      <TTCLASS="FILENAME">stdout</TT> and <TTCLASS="FILENAME">stderr</TT>.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;cat $filename 2&#62;/dev/null &#62;/dev/null   2&nbsp;# If "$filename" does not exist, there will be no error message output.   3&nbsp;# If "$filename" does exist, the contents of the file will not list to stdout.   4&nbsp;# Therefore, no output at all will result from the above line of code.   5&nbsp;#   6&nbsp;#  This can be useful in situations where the return code from a command   7&nbsp;#+ needs to be tested, but no output is desired.   8&nbsp;#   9&nbsp;# cat $filename &#38;&#62;/dev/null  10&nbsp;#     also works, as Baris Cicek points out.</PRE></TD></TR></TABLE>	    </P><P>Deleting contents of a file, but preserving the file itself, with	      all attendant permissions (from <AHREF="sha-bang.html#EX1">Example 2-1</A> and <AHREF="sha-bang.html#EX2">Example 2-3</A>):	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;cat /dev/null &#62; /var/log/messages   2&nbsp;#  : &#62; /var/log/messages   has same effect, but does not spawn a new process.   3&nbsp;   4&nbsp;cat /dev/null &#62; /var/log/wtmp</PRE></TD></TR></TABLE>	      </P><P>Automatically emptying the contents of a logfile	      (especially good for dealing with those nasty	      <SPANCLASS="QUOTE">"cookies"</SPAN> sent by commercial Web sites):</P><DIVCLASS="EXAMPLE"><HR><ANAME="COOKIES"></A><P><B>Example 28-1. Hiding the cookie jar</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;if [ -f ~/.netscape/cookies ]  # Remove, if exists.   2&nbsp;then   3&nbsp;  rm -f ~/.netscape/cookies   4&nbsp;fi   5&nbsp;   6&nbsp;ln -s /dev/null ~/.netscape/cookies   7&nbsp;# All cookies now get sent to a black hole, rather than saved to disk.</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="ZEROSREF1"></A>Uses of <TTCLASS="FILENAME">/dev/zero</TT></DT><DD><P>Like <TTCLASS="FILENAME">/dev/null</TT>,	      <TTCLASS="FILENAME">/dev/zero</TT> is a pseudo-device file, but	      it actually produces a stream of nulls	      (<SPANCLASS="emphasis"><ICLASS="EMPHASIS">binary</I></SPAN> zeros, not the ASCII	      kind). Output written to <TTCLASS="FILENAME">/dev/zero</TT>	      disappears, and it is fairly difficult to actually read	      the nulls from there, though it can be done with <AHREF="extmisc.html#ODREF">od</A> or a hex editor. <ANAME="SWAPFILEREF"></A>The chief use for	      <TTCLASS="FILENAME">/dev/zero</TT> is in creating an initialized	      dummy file of predetermined length intended as a temporary	      swap file.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX73"></A><P><B>Example 28-2. Setting up a swapfile using <TTCLASS="FILENAME">/dev/zero</TT></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# Creating a swapfile.   3&nbsp;   4&nbsp;ROOT_UID=0         # Root has $UID 0.   5&nbsp;E_WRONG_USER=65    # Not root?   6&nbsp;   7&nbsp;FILE=/swap   8&nbsp;BLOCKSIZE=1024   9&nbsp;MINBLOCKS=40  10&nbsp;SUCCESS=0  11&nbsp;  12&nbsp;  13&nbsp;# This script must be run as root.  14&nbsp;if [ "$UID" -ne "$ROOT_UID" ]  15&nbsp;then  16&nbsp;  echo; echo "You must be root to run this script."; echo  17&nbsp;  exit $E_WRONG_USER  18&nbsp;fi    19&nbsp;    20&nbsp;  21&nbsp;blocks=${1:-$MINBLOCKS}          #  Set to default of 40 blocks,  22&nbsp;                                 #+ if nothing specified on command line.  23&nbsp;# This is the equivalent of the command block below.  24&nbsp;# --------------------------------------------------  25&nbsp;# if [ -n "$1" ]  26&nbsp;# then  27&nbsp;#   blocks=$1  28&nbsp;# else  29&nbsp;#   blocks=$MINBLOCKS  30&nbsp;# fi  31&nbsp;# --------------------------------------------------  32&nbsp;  33&nbsp;  34&nbsp;if [ "$blocks" -lt $MINBLOCKS ]  35&nbsp;then  36&nbsp;  blocks=$MINBLOCKS              # Must be at least 40 blocks long.  37&nbsp;fi    38&nbsp;  39&nbsp;  40&nbsp;######################################################################  41&nbsp;echo "Creating swap file of size $blocks blocks (KB)."  42&nbsp;dd if=/dev/zero of=$FILE bs=$BLOCKSIZE count=$blocks  # Zero out file.  43&nbsp;mkswap $FILE $blocks             # Designate it a swap file.  44&nbsp;swapon $FILE                     # Activate swap file.  45&nbsp;#  Note that if one or more of these commands fails,  46&nbsp;#+ then it could cause nasty problems.  47&nbsp;######################################################################  48&nbsp;  49&nbsp;#  Exercise:  50&nbsp;#  Rewrite the above block of code so that if it does not execute  51&nbsp;#+ successfully, then:  52&nbsp;#    1) an error message is echoed to stderr,  53&nbsp;#    2) all temporary files are cleaned up, and  54&nbsp;#    3) the script exits in an orderly fashion with an  55&nbsp;#+      appropriate error code.  56&nbsp;  57&nbsp;echo "Swap file created and activated."  58&nbsp;  59&nbsp;exit $SUCCESS</PRE></TD></TR></TABLE><HR></DIV><P>Another application of <TTCLASS="FILENAME">/dev/zero</TT>	      is to <SPANCLASS="QUOTE">"zero out"</SPAN> a file of a designated	      size for a special purpose, such as mounting a filesystem	      on a <AHREF="devproc.html#LOOPBACKREF">loopback device</A>	      (see <AHREF="system.html#CREATEFS">Example 16-8</A>) or <SPANCLASS="QUOTE">"securely"</SPAN>	      deleting a file (see <AHREF="extmisc.html#BLOTOUT">Example 15-58</A>).</P><DIVCLASS="EXAMPLE"><HR><ANAME="RAMDISK"></A><P><B>Example 28-3. Creating a ramdisk</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# ramdisk.sh   3&nbsp;   4&nbsp;#  A "ramdisk" is a segment of system RAM memory   5&nbsp;#+ which acts as if it were a filesystem.   6&nbsp;#  Its advantage is very fast access (read/write time).   7&nbsp;#  Disadvantages: volatility, loss of data on reboot or powerdown.   8&nbsp;#+                less RAM available to system.   9&nbsp;#  10&nbsp;#  Of what use is a ramdisk?  11&nbsp;#  Keeping a large dataset, such as a table or dictionary on ramdisk,  12&nbsp;#+ speeds up data lookup, since memory access is much faster than disk access.  13&nbsp;  14&nbsp;  15&nbsp;E_NON_ROOT_USER=70             # Must run as root.  16&nbsp;ROOTUSER_NAME=root  17&nbsp;  18&nbsp;MOUNTPT=/mnt/ramdisk  19&nbsp;SIZE=2000                      # 2K blocks (change as appropriate)  20&nbsp;BLOCKSIZE=1024                 # 1K (1024 byte) block size  21&nbsp;DEVICE=/dev/ram0               # First ram device  22&nbsp;  23&nbsp;username=`id -nu`  24&nbsp;if [ "$username" != "$ROOTUSER_NAME" ]  25&nbsp;then  26&nbsp;  echo "Must be root to run \"`basename $0`\"."  27&nbsp;  exit $E_NON_ROOT_USER  28&nbsp;fi  29&nbsp;  30&nbsp;if [ ! -d "$MOUNTPT" ]         #  Test whether mount point already there,  31&nbsp;then                           #+ so no error if this script is run  32&nbsp;  mkdir $MOUNTPT               #+ multiple times.  33&nbsp;fi  34&nbsp;  35&nbsp;##############################################################################  36&nbsp;dd if=/dev/zero of=$DEVICE count=$SIZE bs=$BLOCKSIZE  # Zero out RAM device.  37&nbsp;                                                      # Why is this necessary?  38&nbsp;mke2fs $DEVICE                 # Create an ext2 filesystem on it.  39&nbsp;mount $DEVICE $MOUNTPT         # Mount it.  40&nbsp;chmod 777 $MOUNTPT             # Enables ordinary user to access ramdisk.  41&nbsp;                               # However, must be root to unmount it.  42&nbsp;##############################################################################  43&nbsp;# Need to test whether above commands succeed. Could cause problems otherwise.  44&nbsp;# Exercise: modify this script to make it safer.  45&nbsp;  46&nbsp;echo "\"$MOUNTPT\" now available for use."  47&nbsp;# The ramdisk is now accessible for storing files, even by an ordinary user.  48&nbsp;  49&nbsp;#  Caution, the ramdisk is volatile, and its contents will disappear  50&nbsp;#+ on reboot or power loss.  51&nbsp;#  Copy anything you want saved to a regular directory.  52&nbsp;  53&nbsp;# After reboot, run this script to again set up ramdisk.  54&nbsp;# Remounting /mnt/ramdisk without the other steps will not work.  55&nbsp;  56&nbsp;#  Suitably modified, this script can by invoked in /etc/rc.d/rc.local,  57&nbsp;#+ to set up ramdisk automatically at bootup.  58&nbsp;#  That may be appropriate on, for example, a database server.  59&nbsp;  60&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P>In addition to all the above,	      <TTCLASS="FILENAME">/dev/zero</TT> is needed by ELF binaries.</P></DD></DL></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="procref1.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="debugging.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><TTCLASS="FILENAME">/proc</TT></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part5.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Debugging</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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