📄 devproc.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>/dev and /proc</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Advanced Topics"HREF="part4.html"><LINKREL="PREVIOUS"TITLE="Arrays"HREF="arrays.html"><LINKREL="NEXT"TITLE="/proc"HREF="procref1.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"><TABLEWIDTH="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="arrays.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="procref1.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="DEVPROC">Chapter 27. /dev and /proc</A></H1><P><ANAME="DEVPROCREF"></A></P><P>A Linux or UNIX machine typically has the <TTCLASS="FILENAME">/dev</TT> and <TTCLASS="FILENAME">/proc</TT> special-purpose directories.</P><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="DEVREF1">27.1. <TTCLASS="FILENAME">/dev</TT></A></H1><P>The <TTCLASS="FILENAME">/dev</TT> directory contains entries for the physical <ICLASS="EMPHASIS">devices</I> that may or may not be present in the hardware. <ANAME="AEN14570"HREF="#FTN.AEN14570">[1]</A> The hard drive partitions containing the mounted filesystem(s) have entries in <TTCLASS="FILENAME">/dev</TT>, as a simple <AHREF="system.html#DFREF">df</A> shows. <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>df</B></TT> <TTCLASS="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><ANAME="LOOPBACKREF"></A>Among other things, the <TTCLASS="FILENAME">/dev</TT> directory also contains <ICLASS="EMPHASIS">loopback</I> devices, such as <TTCLASS="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. <ANAME="AEN14588"HREF="#FTN.AEN14588">[2]</A> This enables mounting an entire filesystem within a single large file. See <AHREF="system.html#CREATEFS">Example 13-8</A> and <AHREF="system.html#ISOMOUNTREF">Example 13-7</A>.</P><P>A few of the pseudo-devices in <TTCLASS="FILENAME">/dev</TT> have other specialized uses, such as <AHREF="zeros.html#ZEROSREF"><TTCLASS="FILENAME">/dev/null</TT></A>, <AHREF="zeros.html#ZEROSREF1"><TTCLASS="FILENAME">/dev/zero</TT></A>, <AHREF="randomvar.html#URANDOMREF"><TTCLASS="FILENAME">/dev/urandom</TT></A>, <TTCLASS="FILENAME">/dev/sda1</TT>, <TTCLASS="FILENAME">/dev/udp</TT>, and <TTCLASS="FILENAME">/dev/tcp</TT>.</P><P>For instance:</P><P>To <AHREF="system.html#MOUNTREF">mount</A> a USB flash drive, append the following line to <TTCLASS="FILENAME">/etc/fstab</TT>. <ANAME="AEN14609"HREF="#FTN.AEN14609">[3]</A> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 /dev/sda1 /mnt/flashdrive auto noauto,user,noatime 0 0</PRE></TD></TR></TABLE> (See also <AHREF="contributed-scripts.html#USBINST">Example A-23</A>.)</P><P><ANAME="SOCKETREF"></A></P><P>When executing a command on a <TTCLASS="FILENAME">/dev/tcp/$host/$port</TT> pseudo-device file, Bash opens a TCP connection to the associated <ICLASS="EMPHASIS">socket</I>. <ANAME="AEN14624"HREF="#FTN.AEN14624">[4]</A> </P><P>Getting the time from <TTCLASS="FILENAME">nist.gov</TT>:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cat </dev/tcp/time.nist.gov/13</B></TT> <TTCLASS="COMPUTEROUTPUT">53082 04-03-18 04:26:54 68 0 0 502.3 UTC(NIST) *</TT> </PRE></TD></TR></TABLE><P>[Mark contributed the above example.]</P><P>Downloading a URL:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>exec 5<>/dev/tcp/www.net.cn/80</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo -e "GET / HTTP/1.0\n" >&5</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cat <&5</B></TT> </PRE></TD></TR></TABLE><P>[Thanks, Mark and Mihai Maties.]</P><DIVCLASS="EXAMPLE"><HR><ANAME="DEVTCP"></A><P><B>Example 27-1. Using <TTCLASS="FILENAME">/dev/tcp</TT> for troubleshooting</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="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=www.dns-diy.com # 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></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN14570"HREF="devproc.html#AEN14570">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>The entries in <TTCLASS="FILENAME">/dev</TT> provide mount points for physical and virtual devices. These entries use very little drive space.</P><P>Some devices, such as <TTCLASS="FILENAME">/dev/null</TT>, <TTCLASS="FILENAME">/dev/zero</TT>, and <TTCLASS="FILENAME">/dev/urandom</TT> are virtual. They are not actual physical devices and exist only in software.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN14588"HREF="devproc.html#AEN14588">[2]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>A <ICLASS="EMPHASIS">block device</I> reads and/or writes data in chunks, or blocks, in contrast to a <ICLASS="EMPHASIS">character device</I>, which acesses data in character units. Examples of block devices are a hard drive and CD ROM drive. An example of a character device is a keyboard.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN14609"HREF="devproc.html#AEN14609">[3]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>Of course, the mount point <TTCLASS="FILENAME">/mnt/flashdrive</TT> must exist. If not, then, as root, <BCLASS="COMMAND">mkdir /mnt/flashdrive</B>.</P><P>To actually mount the drive, use the following command: <BCLASS="COMMAND">mount /mnt/flashdrive</B></P><P>Newer Linux distros automount flash drives in the <TTCLASS="FILENAME">/media</TT> directory.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN14624"HREF="devproc.html#AEN14624">[4]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>A <ICLASS="EMPHASIS">socket</I> is a communications node associated with a specific I/O port. 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></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="arrays.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="procref1.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Arrays</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part4.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><TTCLASS="FILENAME">/proc</TT></TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -