1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Of Zeros and Nulls</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
"><LINK
REL="HOME"
TITLE="Advanced Bash-Scripting Guide"
HREF="index.html"><LINK
REL="UP"
TITLE="Advanced Topics"
HREF="part5.html"><LINK
REL="PREVIOUS"
TITLE="Network Programming"
HREF="networkprogramming.html"><LINK
REL="NEXT"
TITLE="Debugging"
HREF="debugging.html"><META
HTTP-EQUIV="Content-Style-Type"
CONTENT="text/css"><LINK
REL="stylesheet"
HREF="common/kde-common.css"
TYPE="text/css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=iso-8859-1"><META
HTTP-EQUIV="Content-Language"
CONTENT="en"><LINK
REL="stylesheet"
HREF="common/kde-localised.css"
TYPE="text/css"
TITLE="KDE-English"><LINK
REL="stylesheet"
HREF="common/kde-default.css"
TYPE="text/css"
TITLE="KDE-Default"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#AA0000"
VLINK="#AA0055"
ALINK="#AA0000"
STYLE="font-family: sans-serif;"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="networkprogramming.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="debugging.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="ZEROS"
></A
>Chapter 31. Of Zeros and Nulls</H1
><TABLE
BORDER="0"
WIDTH="100%"
CELLSPACING="0"
CELLPADDING="0"
CLASS="EPIGRAPH"
><TR
><TD
WIDTH="45%"
> </TD
><TD
WIDTH="45%"
ALIGN="LEFT"
VALIGN="TOP"
><I
><P
><I
>Faultily faultless, icily regular, splendidly null</I
></P
><P
><I
>Dead perfection; no more.</I
></P
><P
><I
>--Alfred Lord Tennyson</I
></P
></I
></TD
></TR
></TABLE
><P
><A
NAME="ZEROSREF"
></A
></P
><DIV
CLASS="VARIABLELIST"
><P
><B
><A
NAME="ZERONULL1"
></A
><TT
CLASS="FILENAME"
>/dev/zero</TT
>
... <TT
CLASS="FILENAME"
>/dev/null</TT
></B
></P
><DL
><DT
><A
NAME="DEVNULLREF"
></A
>Uses of
<TT
CLASS="FILENAME"
>/dev/null</TT
></DT
><DD
><P
>Think of <TT
CLASS="FILENAME"
>/dev/null</TT
> as a <I
CLASS="FIRSTTERM"
>black
hole</I
>. It is essentially the equivalent of
a write-only file. Everything written to it disappears.
Attempts to read or output from it result in nothing. All
the same, <TT
CLASS="FILENAME"
>/dev/null</TT
> can be quite
useful from both the command-line and in scripts.</P
><P
>Suppressing <TT
CLASS="FILENAME"
>stdout</TT
>.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 cat $filename >/dev/null
2 # Contents of the file will not list to stdout.</PRE
></TD
></TR
></TABLE
>
</P
><P
>Suppressing <TT
CLASS="FILENAME"
>stderr</TT
>
(from <A
HREF="moreadv.html#EX57"
>Example 16-3</A
>).
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 rm $badname 2>/dev/null
2 # So error messages [stderr] deep-sixed.</PRE
></TD
></TR
></TABLE
>
</P
><P
>Suppressing output from <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>both</I
></SPAN
>
<TT
CLASS="FILENAME"
>stdout</TT
> and <TT
CLASS="FILENAME"
>stderr</TT
>.
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 cat $filename 2>/dev/null >/dev/null
2 # If "$filename" does not exist, there will be no error message output.
3 # If "$filename" does exist, the contents of the file will not list to stdout.
4 # Therefore, no output at all will result from the above line of code.
5 #
6 # This can be useful in situations where the return code from a command
7 #+ needs to be tested, but no output is desired.
8 #
9 # cat $filename &>/dev/null
10 # 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 <A
HREF="sha-bang.html#EX1"
>Example 2-1</A
> and <A
HREF="sha-bang.html#EX2"
>Example 2-3</A
>):
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 cat /dev/null > /var/log/messages
2 # : > /var/log/messages has same effect, but does not spawn a new process.
3
4 cat /dev/null > /var/log/wtmp</PRE
></TD
></TR
></TABLE
>
</P
><P
>Automatically emptying the contents of a logfile
(especially good for dealing with those nasty
<SPAN
CLASS="QUOTE"
>"cookies"</SPAN
> sent by commercial Web sites):</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="COOKIES"
></A
><P
><B
>Example 31-1. Hiding the cookie jar</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 # Obsolete Netscape browser.
2 # Same principle applies to newer browsers.
3
4 if [ -f ~/.netscape/cookies ] # Remove, if exists.
5 then
6 rm -f ~/.netscape/cookies
7 fi
8
9 ln -s /dev/null ~/.netscape/cookies
10 # All cookies now get sent to a black hole, rather than saved to disk.</PRE
></TD
></TR
></TABLE
><HR></DIV
></DD
><DT
><A
NAME="ZEROSREF1"
></A
>Uses of <TT
CLASS="FILENAME"
>/dev/zero</TT
></DT
><DD
><P
>Like <TT
CLASS="FILENAME"
>/dev/null</TT
>,
<TT
CLASS="FILENAME"
>/dev/zero</TT
> is a pseudo-device file, but
it actually produces a stream of nulls
(<SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>binary</I
></SPAN
> zeros, not the <A
HREF="special-chars.html#ASCIIDEF"
>ASCII</A
> kind). Output written
to <TT
CLASS="FILENAME"
>/dev/zero</TT
> disappears, and it is
fairly difficult to actually read the nulls emitted there,
though it can be done with <A
HREF="extmisc.html#ODREF"
>od</A
>
or a hex editor. <A
NAME="SWAPFILEREF"
></A
>The chief use of
<TT
CLASS="FILENAME"
>/dev/zero</TT
> is creating an initialized
dummy file of predetermined length intended as a temporary
swap file.</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="EX73"
></A
><P
><B
>Example 31-2. Setting up a swapfile using <TT
CLASS="FILENAME"
>/dev/zero</TT
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # Creating a swap file.
3
4 # A swap file provides a temporary storage cache
5 #+ which helps speed up certain filesystem operations.
6
7 ROOT_UID=0 # Root has $UID 0.
8 E_WRONG_USER=85 # Not root?
9
10 FILE=/swap
11 BLOCKSIZE=1024
12 MINBLOCKS=40
13 SUCCESS=0
14
15
16 # This script must be run as root.
17 if [ "$UID" -ne "$ROOT_UID" ]
18 then
19 echo; echo "You must be root to run this script."; echo
20 exit $E_WRONG_USER
21 fi
22
23
24 blocks=${1:-$MINBLOCKS} # Set to default of 40 blocks,
25 #+ if nothing specified on command-line.
26 # This is the equivalent of the command block below.
27 # --------------------------------------------------
28 # if [ -n "$1" ]
29 # then
30 # blocks=$1
31 # else
32 # blocks=$MINBLOCKS
33 # fi
34 # --------------------------------------------------
35
36
37 if [ "$blocks" -lt $MINBLOCKS ]
38 then
39 blocks=$MINBLOCKS # Must be at least 40 blocks long.
40 fi
41
42
43 ######################################################################
44 echo "Creating swap file of size $blocks blocks (KB)."
45 dd if=/dev/zero of=$FILE bs=$BLOCKSIZE count=$blocks # Zero out file.
46 mkswap $FILE $blocks # Designate it a swap file.
47 swapon $FILE # Activate swap file.
48 retcode=$? # Everything worked?
49 # Note that if one or more of these commands fails,
50 #+ then it could cause nasty problems.
51 ######################################################################
52
53 # Exercise:
54 # Rewrite the above block of code so that if it does not execute
55 #+ successfully, then:
56 # 1) an error message is echoed to stderr,
57 # 2) all temporary files are cleaned up, and
58 # 3) the script exits in an orderly fashion with an
59 #+ appropriate error code.
60
61 echo "Swap file created and activated."
62
63 exit $retcode</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>Another application of <TT
CLASS="FILENAME"
>/dev/zero</TT
>
is to <SPAN
CLASS="QUOTE"
>"zero out"</SPAN
> a file of a designated
size for a special purpose, such as mounting a filesystem
on a <A
HREF="devproc.html#LOOPBACKREF"
>loopback device</A
>
(see <A
HREF="system.html#CREATEFS"
>Example 17-8</A
>) or <SPAN
CLASS="QUOTE"
>"securely"</SPAN
>
deleting a file (see <A
HREF="extmisc.html#BLOTOUT"
>Example 16-61</A
>).</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="RAMDISK"
></A
><P
><B
>Example 31-3. Creating a ramdisk</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="90%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # ramdisk.sh
3
4 # A "ramdisk" is a segment of system RAM memory
5 #+ which acts as if it were a filesystem.
6 # Its advantage is very fast access (read/write time).
7 # Disadvantages: volatility, loss of data on reboot or powerdown,
8 #+ less RAM available to system.
9 #
10 # Of what use is a ramdisk?
11 # Keeping a large dataset, such as a table or dictionary on ramdisk,
12 #+ speeds up data lookup, since memory access is much faster than disk access.
13
14
15 E_NON_ROOT_USER=70 # Must run as root.
16 ROOTUSER_NAME=root
17
18 MOUNTPT=/mnt/ramdisk # Create with mkdir /mnt/ramdisk.
19 SIZE=2000 # 2K blocks (change as appropriate)
20 BLOCKSIZE=1024 # 1K (1024 byte) block size
21 DEVICE=/dev/ram0 # First ram device
22
23 username=`id -nu`
24 if [ "$username" != "$ROOTUSER_NAME" ]
25 then
26 echo "Must be root to run \"`basename $0`\"."
27 exit $E_NON_ROOT_USER
28 fi
29
30 if [ ! -d "$MOUNTPT" ] # Test whether mount point already there,
31 then #+ so no error if this script is run
32 mkdir $MOUNTPT #+ multiple times.
33 fi
34
35 ##############################################################################
36 dd if=/dev/zero of=$DEVICE count=$SIZE bs=$BLOCKSIZE # Zero out RAM device.
37 # Why is this necessary?
38 mke2fs $DEVICE # Create an ext2 filesystem on it.
39 mount $DEVICE $MOUNTPT # Mount it.
40 chmod 777 $MOUNTPT # Enables ordinary user to access ramdisk.
41 # However, must be root to unmount it.
42 ##############################################################################
43 # Need to test whether above commands succeed. Could cause problems otherwise.
44 # Exercise: modify this script to make it safer.
45
46 echo "\"$MOUNTPT\" now available for use."
47 # The ramdisk is now accessible for storing files, even by an ordinary user.
48
49 # Caution, the ramdisk is volatile, and its contents will disappear
50 #+ on reboot or power loss.
51 # Copy anything you want saved to a regular directory.
52
53 # After reboot, run this script to again set up ramdisk.
54 # Remounting /mnt/ramdisk without the other steps will not work.
55
56 # Suitably modified, this script can by invoked in /etc/rc.d/rc.local,
57 #+ to set up ramdisk automatically at bootup.
58 # That may be appropriate on, for example, a database server.
59
60 exit 0</PRE
></TD
></TR
></TABLE
><HR></DIV
><P
>In addition to all the above,
<TT
CLASS="FILENAME"
>/dev/zero</TT
> is needed by ELF
(<I
CLASS="FIRSTTERM"
>Executable and Linking Format</I
>)
UNIX/Linux binaries.</P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="networkprogramming.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="debugging.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Network Programming</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="part5.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Debugging</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|