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 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>/dev and /proc</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="Indirect References"
HREF="ivr.html"><LINK
REL="NEXT"
TITLE="/proc"
HREF="procref1.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="ivr.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="procref1.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="DEVPROC"
></A
>Chapter 29. <TT
CLASS="FILENAME"
>/dev</TT
> and <TT
CLASS="FILENAME"
>/proc</TT
></H1
><P
><A
NAME="DEVPROCREF"
></A
></P
><P
>A Linux or UNIX filesystem typically has the
<TT
CLASS="FILENAME"
>/dev</TT
> and
<TT
CLASS="FILENAME"
>/proc</TT
> special-purpose
directories.</P
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="DEVREF1"
></A
>29.1. <TT
CLASS="FILENAME"
>/dev</TT
></H1
><P
><A
NAME="DEVFILEREF"
></A
>The <TT
CLASS="FILENAME"
>/dev</TT
> directory contains entries for
the <I
CLASS="FIRSTTERM"
>physical devices</I
> that may or may not
be present in the hardware.
<A
NAME="AEN19045"
HREF="#FTN.AEN19045"
>[1]</A
>
Appropriately enough, these are called <I
CLASS="FIRSTTERM"
>device
files</I
>.
As an example, the hard drive partitions containing
the mounted filesystem(s) have entries in <TT
CLASS="FILENAME"
>/dev</TT
>, as <A
HREF="system.html#DFREF"
>df</A
> shows.</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>df</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>Filesystem 1k-blocks Used Available Use%
Mounted on
/dev/hda6 495876 222748 247527 48% /
/dev/hda1 50755 3887 44248 9% /boot
/dev/hda8 367013 13262 334803 4% /home
/dev/hda5 1714416 1123624 503704 70% /usr</TT
>
</PRE
></TD
></TR
></TABLE
>
</P
><P
><A
NAME="LOOPBACKREF"
></A
>Among other things, the <TT
CLASS="FILENAME"
>/dev</TT
> directory
contains <I
CLASS="FIRSTTERM"
>loopback</I
> devices, such as
<TT
CLASS="FILENAME"
>/dev/loop0</TT
>. A loopback device is a gimmick
that allows an ordinary file to be accessed as if it were a
block device.
<A
NAME="AEN19065"
HREF="#FTN.AEN19065"
>[2]</A
>
This permits mounting an entire filesystem within a
single large file. See <A
HREF="system.html#CREATEFS"
>Example 17-8</A
> and <A
HREF="system.html#ISOMOUNTREF"
>Example 17-7</A
>.</P
><P
>A few of the pseudo-devices in <TT
CLASS="FILENAME"
>/dev</TT
>
have other specialized uses, such as <A
HREF="zeros.html#ZEROSREF"
><TT
CLASS="FILENAME"
>/dev/null</TT
></A
>, <A
HREF="zeros.html#ZEROSREF1"
><TT
CLASS="FILENAME"
>/dev/zero</TT
></A
>, <A
HREF="randomvar.html#URANDOMREF"
><TT
CLASS="FILENAME"
>/dev/urandom</TT
></A
>,
<TT
CLASS="FILENAME"
>/dev/sda1</TT
> (hard drive partition),
<TT
CLASS="FILENAME"
>/dev/udp</TT
> (<I
CLASS="FIRSTTERM"
>User
Datagram Packet</I
> port), and <A
HREF="devproc.html#DEVTCP"
><TT
CLASS="FILENAME"
>/dev/tcp</TT
></A
>.</P
><P
>For instance:</P
><P
>To manually <A
HREF="system.html#MOUNTREF"
>mount</A
>
a USB flash drive, append the following line to
<A
HREF="system.html#FSTABREF"
><TT
CLASS="FILENAME"
>/etc/fstab</TT
></A
>.
<A
NAME="AEN19093"
HREF="#FTN.AEN19093"
>[3]</A
>
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 /dev/sda1 /mnt/flashdrive auto noauto,user,noatime 0 0</PRE
></TD
></TR
></TABLE
>
(See also <A
HREF="contributed-scripts.html#USBINST"
>Example A-23</A
>.)</P
><P
>Checking whether a disk is in the CD-burner
(soft-linked to <TT
CLASS="FILENAME"
>/dev/hdc</TT
>):
<TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 head -1 /dev/hdc
2
3
4 # head: cannot open '/dev/hdc' for reading: No medium found
5 # (No disc in the drive.)
6
7 # head: error reading '/dev/hdc': Input/output error
8 # (There is a disk in the drive, but it can't be read;
9 #+ possibly it's an unrecorded CDR blank.)
10
11 # Stream of characters and assorted gibberish
12 # (There is a pre-recorded disk in the drive,
13 #+ and this is raw output -- a stream of ASCII and binary data.)
14 # Here we see the wisdom of using 'head' to limit the output
15 #+ to manageable proportions, rather than 'cat' or something similar.
16
17
18 # Now, it's just a matter of checking/parsing the output and taking
19 #+ appropriate action.</PRE
></TD
></TR
></TABLE
></P
><P
><A
NAME="SOCKETREF"
></A
></P
><P
>When executing a command on a
<TT
CLASS="FILENAME"
>/dev/tcp/$host/$port</TT
> pseudo-device file, Bash
opens a TCP connection to the associated
<I
CLASS="FIRSTTERM"
>socket</I
>.</P
><TABLE
CLASS="SIDEBAR"
BORDER="1"
CELLPADDING="5"
><TR
><TD
><DIV
CLASS="SIDEBAR"
><A
NAME="AEN19112"
></A
><P
>A <I
CLASS="FIRSTTERM"
>socket</I
> is a
communications node associated with a specific I/O
port. (This is analogous to a <I
CLASS="FIRSTTERM"
>hardware
socket</I
>, or <I
CLASS="FIRSTTERM"
>receptacle</I
>,
for a connecting cable.) It permits data transfer between
hardware devices on the same machine, between machines
on the same network, between machines across different
networks, and, of course, between machines at different
locations on the Internet.</P
></DIV
></TD
></TR
></TABLE
><P
><A
NAME="NPREF"
></A
>The following examples assume an active Internet
connection.</P
><P
>Getting the time from <TT
CLASS="FILENAME"
>nist.gov</TT
>:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>cat </dev/tcp/time.nist.gov/13</B
></TT
>
<TT
CLASS="COMPUTEROUTPUT"
>53082 04-03-18 04:26:54 68 0 0 502.3 UTC(NIST) *</TT
>
</PRE
></TD
></TR
></TABLE
><P
>[Mark contributed this example.]</P
><P
>Generalizing the above into a script:</P
><P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # This script must run with root permissions.
3
4 URL="time.nist.gov/13"
5
6 Time=$(cat </dev/tcp/"$URL")
7 UTC=$(echo "$Time" | awk '{print$3}') # Third field is UTC (GMT) time.
8 # Exercise: modify this for different time zones.
9
10 echo "UTC Time = "$UTC""</PRE
></TD
></TR
></TABLE
></P
><P
><A
NAME="NW001"
></A
>Downloading a URL:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>exec 5<>/dev/tcp/www.net.cn/80</B
></TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>echo -e "GET / HTTP/1.0\n" >&5</B
></TT
>
<TT
CLASS="PROMPT"
>bash$ </TT
><TT
CLASS="USERINPUT"
><B
>cat <&5</B
></TT
>
</PRE
></TD
></TR
></TABLE
><P
>[Thanks, Mark and Mihai Maties.]</P
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="DEVTCP"
></A
><P
><B
>Example 29-1. Using <TT
CLASS="FILENAME"
>/dev/tcp</TT
> for
troubleshooting</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # dev-tcp.sh: /dev/tcp redirection to check Internet connection.
3
4 # Script by Troy Engel.
5 # Used with permission.
6
7 TCP_HOST=news-15.net # A known spam-friendly ISP.
8 TCP_PORT=80 # Port 80 is http.
9
10 # Try to connect. (Somewhat similar to a 'ping' . . .)
11 echo "HEAD / HTTP/1.0" >/dev/tcp/${TCP_HOST}/${TCP_PORT}
12 MYEXIT=$?
13
14 : <<EXPLANATION
15 If bash was compiled with --enable-net-redirections, it has the capability of
16 using a special character device for both TCP and UDP redirections. These
17 redirections are used identically as STDIN/STDOUT/STDERR. The device entries
18 are 30,36 for /dev/tcp:
19
20 mknod /dev/tcp c 30 36
21
22 >From the bash reference:
23 /dev/tcp/host/port
24 If host is a valid hostname or Internet address, and port is an integer
25 port number or service name, Bash attempts to open a TCP connection to the
26 corresponding socket.
27 EXPLANATION
28
29
30 if [ "X$MYEXIT" = "X0" ]; then
31 echo "Connection successful. Exit code: $MYEXIT"
32 else
33 echo "Connection unsuccessful. Exit code: $MYEXIT"
34 fi
35
36 exit $MYEXIT</PRE
></TD
></TR
></TABLE
><HR></DIV
><DIV
CLASS="EXAMPLE"
><HR><A
NAME="MUSICSCR"
></A
><P
><B
>Example 29-2. Playing music</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> 1 #!/bin/bash
2 # music.sh
3
4 # Music without external files
5
6 # Author: Antonio Macchi
7 # Used in ABS Guide with permission.
8
9
10 # /dev/dsp default = 8000 frames per second, 8 bits per frame (1 byte),
11 #+ 1 channel (mono)
12
13 duration=2000 # If 8000 bytes = 1 second, then 2000 = 1/4 second.
14 volume=$'\xc0' # Max volume = \xff (or \x00).
15 mute=$'\x80' # No volume = \x80 (the middle).
16
17 function mknote () # $1=Note Hz in bytes (e.g. A = 440Hz ::
18 { #+ 8000 fps / 440 = 16 :: A = 16 bytes per second)
19 for t in `seq 0 $duration`
20 do
21 test $(( $t % $1 )) = 0 && echo -n $volume || echo -n $mute
22 done
23 }
24
25 e=`mknote 49`
26 g=`mknote 41`
27 a=`mknote 36`
28 b=`mknote 32`
29 c=`mknote 30`
30 cis=`mknote 29`
31 d=`mknote 27`
32 e2=`mknote 24`
33 n=`mknote 32767`
34 # European notation.
35
36 echo -n "$g$e2$d$c$d$c$a$g$n$g$e$n$g$e2$d$c$c$b$c$cis$n$cis$d \
37 $n$g$e2$d$c$d$c$a$g$n$g$e$n$g$a$d$c$b$a$b$c" > /dev/dsp
38 # dsp = Digital Signal Processor
39
40 exit # A "bonny" example of an elegant shell script!</PRE
></TD
></TR
></TABLE
><HR></DIV
></DIV
></DIV
><H3
CLASS="FOOTNOTES"
>Notes</H3
><TABLE
BORDER="0"
CLASS="FOOTNOTES"
WIDTH="100%"
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN19045"
HREF="devproc.html#AEN19045"
>[1]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>The entries in <TT
CLASS="FILENAME"
>/dev</TT
>
provide mount points for physical and virtual devices. These
entries use very little drive space.</P
><P
>Some devices, such as <TT
CLASS="FILENAME"
>/dev/null</TT
>,
<TT
CLASS="FILENAME"
>/dev/zero</TT
>,
and <TT
CLASS="FILENAME"
>/dev/urandom</TT
> are virtual. They
are not actual physical devices and exist only in
software.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN19065"
HREF="devproc.html#AEN19065"
>[2]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
><A
NAME="BLOCKDEVREF"
></A
>A <I
CLASS="FIRSTTERM"
>block
device</I
> reads and/or writes data in chunks,
or <I
CLASS="FIRSTTERM"
>blocks</I
>, in contrast to a <A
NAME="CHARDEVREF"
></A
><I
CLASS="FIRSTTERM"
>character device</I
>,
which acesses data in <I
CLASS="FIRSTTERM"
>character</I
>
units. Examples of block devices are hard drives, CDROM
drives, and flash drives. Examples of character devices are
keyboards, modems, sound cards.</P
></TD
></TR
><TR
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="5%"
><A
NAME="FTN.AEN19093"
HREF="devproc.html#AEN19093"
>[3]</A
></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
WIDTH="95%"
><P
>Of course, the mount point
<TT
CLASS="FILENAME"
>/mnt/flashdrive</TT
> must exist. If not,
then, as <I
CLASS="FIRSTTERM"
>root</I
>, <B
CLASS="COMMAND"
>mkdir
/mnt/flashdrive</B
>.</P
><P
>To actually mount the drive, use the following command:
<B
CLASS="COMMAND"
>mount /mnt/flashdrive</B
></P
><P
>Newer Linux distros automount flash drives in the
<TT
CLASS="FILENAME"
>/media</TT
>
directory without user intervention.</P
></TD
></TR
></TABLE
><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="ivr.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="procref1.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Indirect References</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="part5.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><TT
CLASS="FILENAME"
>/proc</TT
></TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|