29.1 /dev
/dev 目录包含硬件中可能存在或不存在的物理设备的条目。这些条目很恰当地被称为设备文件。作为一个例子,包含挂载了文件系统的硬盘分区在 /dev 中就有条目,如 df 所示。
bash$ df
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其中,/dev 目录包含回环设备,例如 /dev/loop0。回环设备是是一种小花招,它允许像访问块设备一样访问普通文件。这就允许将整个文件系统挂载到单个大文件内。参见例 17-8 和例 17-7。
/dev 中的一些伪设备有其他的特殊用途,例如 /dev/null、/dev/zero、/dev/urandom、/dev/sda1(硬盘分区)、/dev/udp(用户数据报文端口)和 /dev/tcp。
例如:
要手动挂载一个 USB 闪存,应将下面的行添加到 /etc/fstab。
/dev/sda1 /mnt/flashdrive auto noauto,user,noatime 0 0(另请参见例 A-23)。
检查一个磁盘是否在 CD 刻录机中(软链接到 /dev/hdc):
head -1 /dev/hdc
# head: cannot open '/dev/hdc' for reading: No medium found
# (No disc in the drive.)
# head: error reading '/dev/hdc': Input/output error
# (There is a disk in the drive, but it can't be read;
#+ possibly it's an unrecorded CDR blank.)
# Stream of characters and assorted gibberish
# (There is a pre-recorded disk in the drive,
#+ and this is raw output -- a stream of ASCII and binary data.)
# Here we see the wisdom of using 'head' to limit the output
#+ to manageable proportions, rather than 'cat' or something similar.
# Now, it's just a matter of checking/parsing the output and taking
#+ appropriate action.当对 /dev/tcp/$host/$port 伪设备文件执行命令时,Bash 打开一个到关联套接字的 TCP 连接。
套接字(socket)是与特定 I/O 端口关联的通信节点。(这与连接线的硬件接口,或插座类似)。它允许同一个机器上、同一个网络的机器之间、不同网络的机器之间以及(当然)位于互联网不同位置的机器之间进行数据传输。
下面的示例假设有一个有效的互联网连接。
从 nist.gov 获取时间:
【Mark 贡献了此示例。】
将上述内容概括为一个脚本:
下载一个 URL:
【感谢 Mark 和 Mihai Maties。】
例 29-1. 使用 /dev/tcp 进行排错
例 29-2. 播放音乐
注
与以字符为单位访问数据的字符设备相比,块设备以大段或块的形式读取和/或写入数据。块设备的例子有硬盘、CD 光盘以及闪存。字符设备的例子有键盘、调制解调器、声卡。
最后更新于
这有帮助吗?