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

📄 devproc.html

📁 Shall高级编程
💻 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.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Advanced Topics"HREF="part5.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"><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="arrays.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="procref1.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="DEVPROC"></A>Chapter 27. <TTCLASS="FILENAME">/dev</TT> and <TTCLASS="FILENAME">/proc</TT></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"></A>27.1. <TTCLASS="FILENAME">/dev</TT></H1><P>The <TTCLASS="FILENAME">/dev</TT> directory	contains entries for the physical <ICLASS="FIRSTTERM">devices</I>	that may or may not be present in the hardware.	   <ANAME="AEN17493"HREF="#FTN.AEN17493">[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.</P><P><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="FIRSTTERM">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="AEN17512"HREF="#FTN.AEN17512">[2]</A>	This enables mounting an entire filesystem within a	single large file. See <AHREF="system.html#CREATEFS">Example 16-8</A> and <AHREF="system.html#ISOMOUNTREF">Example 16-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="AEN17533"HREF="#FTN.AEN17533">[3]</A>	   <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;/dev/sda1    /mnt/flashdrive    auto    noauto,user,noatime    0 0</PRE></TD></TR></TABLE>         (See also <AHREF="contributed-scripts.html#USBINST">Example A-25</A>.)</P><P>Checking whether a disk is in the CD-burner         (soft-linked to <TTCLASS="FILENAME">/dev/hdc</TT>):	 <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;head -1 /dev/hdc   2&nbsp;   3&nbsp;   4&nbsp;#  head: cannot open '/dev/hdc' for reading: No medium found   5&nbsp;#  (No disc in the drive.)   6&nbsp;   7&nbsp;#  head: error reading '/dev/hdc': Input/output error   8&nbsp;#  (There is a disk in the drive, but it can't be read;   9&nbsp;#+  possibly it's an unrecorded CDR blank.)     10&nbsp;  11&nbsp;#  Stream of characters and assorted gibberish  12&nbsp;#  (There is a pre-recorded disk in the drive,  13&nbsp;#+  and this is raw output -- a stream of ASCII and binary data.)  14&nbsp;#  Here we see the wisdom of using 'head' to limit the output  15&nbsp;#+ to manageable proportions, rather than 'cat' or something similar.  16&nbsp;  17&nbsp;  18&nbsp;#  Now, it's just a matter of checking/parsing the output and taking  19&nbsp;#+ appropriate action.</PRE></TD></TR></TABLE></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="FIRSTTERM">socket</I>.</P><TABLECLASS="SIDEBAR"BORDER="1"CELLPADDING="5"><TR><TD><DIVCLASS="SIDEBAR"><ANAME="AEN17552"></A><P>A <ICLASS="FIRSTTERM">socket</I> is a	      communications node associated with a specific I/O	      port. (This is analogous to a <ICLASS="FIRSTTERM">hardware	      socket</I>, or <ICLASS="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>The following examples assume an active Internet         connection.</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 &#60;/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&#60;&#62;/dev/tcp/www.net.cn/80</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo -e "GET / HTTP/1.0\n" &#62;&#38;5</B></TT> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>cat &#60;&#38;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&nbsp;#!/bin/bash   2&nbsp;# dev-tcp.sh: /dev/tcp redirection to check Internet connection.   3&nbsp;   4&nbsp;# Script by Troy Engel.   5&nbsp;# Used with permission.   6&nbsp;    7&nbsp;TCP_HOST=www.dns-diy.com   # A known spam-friendly ISP.   8&nbsp;TCP_PORT=80                # Port 80 is http.   9&nbsp;    10&nbsp;# Try to connect. (Somewhat similar to a 'ping' . . .)   11&nbsp;echo "HEAD / HTTP/1.0" &#62;/dev/tcp/${TCP_HOST}/${TCP_PORT}  12&nbsp;MYEXIT=$?  13&nbsp;  14&nbsp;: &#60;&#60;EXPLANATION  15&nbsp;If bash was compiled with --enable-net-redirections, it has the capability of  16&nbsp;using a special character device for both TCP and UDP redirections. These  17&nbsp;redirections are used identically as STDIN/STDOUT/STDERR. The device entries  18&nbsp;are 30,36 for /dev/tcp:  19&nbsp;  20&nbsp;  mknod /dev/tcp c 30 36  21&nbsp;  22&nbsp;&#62;From the bash reference:  23&nbsp;/dev/tcp/host/port  24&nbsp;    If host is a valid hostname or Internet address, and port is an integer  25&nbsp;port number or service name, Bash attempts to open a TCP connection to the  26&nbsp;corresponding socket.  27&nbsp;EXPLANATION  28&nbsp;  29&nbsp;     30&nbsp;if [ "X$MYEXIT" = "X0" ]; then  31&nbsp;  echo "Connection successful. Exit code: $MYEXIT"  32&nbsp;else  33&nbsp;  echo "Connection unsuccessful. Exit code: $MYEXIT"  34&nbsp;fi  35&nbsp;  36&nbsp;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.AEN17493"HREF="devproc.html#AEN17493">[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.AEN17512"HREF="devproc.html#AEN17512">[2]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>A <ICLASS="FIRSTTERM">block device</I> reads	    and/or writes data in chunks, or blocks, in contrast to a	    <ICLASS="FIRSTTERM">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.AEN17533"HREF="devproc.html#AEN17533">[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 <ICLASS="FIRSTTERM">root</I>, <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></TABLE><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="arrays.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="procref1.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Arrays</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part5.html"ACCESSKEY="U">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 + -