Debian Codegraph @

HTML/zeros.html

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