read (翻译 man 3)

本文详细介绍了 Linux 下 read 函数的使用方法,包括其参数、返回值及可能发生的错误情况。此外,还提供了关于不同文件系统下 read 操作行为的说明。

 

READ(2)                       Linux Programmer's Manual                       READ(2)

NAME

       read - read from a file descriptor
//从fd中读取数据

SYNOPSIS

       #include <unistd.h>

       ssize_t read(int fd, void *buf, size_t count);

DESCRIPTION

       read() attempts to read up to count bytes from file descriptor fd into the
       buffer starting at buf.
//read()尝试从fd中读出count个字节放在buf的开头
       If count is zero, read() returns zero and has no other results.  If count is
       greater than SSIZE_MAX, the result is unspecified.
//如果count是0,read()返回0并没有其他的结果.如果count比SSIZE_MAX还大,结果是未定义的.

RETURN VALUE

       On success, the number of bytes read is returned (zero indicates end of file),
       and the file position is advanced by this number.  It is not an error if this
       number is smaller than the number of bytes requested; this may happen for
       example because fewer bytes are actually available right now (maybe because we
       were close to end-of-file, or because we are reading from a pipe, or from a
       terminal), or because read() was interrupted by a signal.  On error, -1 is
       returned, and errno is set appropriately.  In this case it is left unspecified
       whether the file position (if any) changes.
//成功:返回读取的字节数(0代表这文件末尾),文件的位置在返回的字节数之前?.如果返回的数字小于
//要求读出的数字,这不是一个错误.这种情况可能在例如只有少量的字符当前有效(可能是因为是使用
//EOF关闭,或者是从管道读取数据,或者是从一个终端),或者是被一个信号中断.
//错误:返回-1,errno被适当的设置.在这种情况下文件指针的位置是否变化是留下的未特别指出的.

ERRORS

       EAGAIN The file descriptor fd refers to a file other than a socket and has
              been marked nonblocking (O_NONBLOCK), and the read would block.
//fd引用的是一个文件而不是一个socket,而且fd已经标识为非阻塞,读操作将会阻塞.
       EAGAIN or EWOULDBLOCK
              The file descriptor fd refers to a socket and has been marked
              nonblocking (O_NONBLOCK), and the read would block.  POSIX.1-2001
              allows either error to be returned for this case, and does not require
              these constants to have the same value, so a portable application
              should check for both possibilities.
//fd引用的是一个socket,而且fd已经标识为非阻塞,读操作将会阻塞.POSIX.1-2001允许在这种情况下
//返回一个错误.不需要这些常量保持相同的值,所以一个轻便的程序应该检测这2种可能性
EBADF fd is not a valid file descriptor or is not open for reading. //fd不是一个有效的fd,或者还没有打开 EFAULT buf is outside your accessible address space. //buf溢出,超过你分配的大小 EINTR The call was interrupted by a signal before any data was read; see signal(7). //在读取数据之前被信号中断 EINVAL fd is attached to an object which is unsuitable for reading; or the file was opened with the O_DIRECT flag, and either the address specified in buf, the value specified in count, or the current file offset is not suitably aligned. //对象的fd不适合读取,或者是文件打开的时候使用了O_DIRECT,buf,count中的值或者是
//当前文件的偏移量都是没有线性分配的.
EINVAL fd was created via a call to timerfd_create(2) and the wrong size buffer was given to read(); see timerfd_create(2) for further information. //fd通过timerfd_create()的调用创建出来.错误的buffer大小给了read() EIO I/O error. This will happen for example when the process is in a background process group, tries to read from its controlling tty, and either it is ignoring or blocking SIGTTIN or its process group is orphaned. It may also occur when there is a low-level I/O error while reading from a disk or tape. //IO错误:例如进程在后台进程组,试图从它控制的tty中读取数据,和进程忽略或者被SIGTTIN阻塞
//或者进程的组被孤立.IO错误也可能在低等级的IO错误当从一个磁盘或者是磁带读取数据的时候发生.
EISDIR fd refers to a directory. //fd指向的是一个文件夹 Other errors may occur, depending on the object connected to fd. POSIX allows a read() that is interrupted after reading some data to return -1 (with errno set to EINTR) or to return the number of bytes already read.
//其他的错误也会发生,取决于对象关联的fd.POSIX允许被中断的read()在读取一些数据之后返回-1
//(errno为EINTR),或者返回实际读取的的字节数.

CONFORMING TO

       SVr4, 4.3BSD, POSIX.1-2001.

NOTES

       On NFS file systems, reading small amounts of data will only update the
       timestamp the first time, subsequent calls may not do so.  This is caused by
       client side attribute caching, because most if not all NFS clients leave
       st_atime (last file access time) updates to the server and client side reads
       satisfied from the client's cache will not cause st_atime updates on the
       server as there are no server side reads.  UNIX semantics can be obtained by
       disabling client side attribute caching, but in most situations this will
       substantially increase server load and decrease performance.
//在NFS下,读取少量数据仅仅会在最初的时候更新时间戳,接下来的调用就不做这些了.这是由于客户端
//方面的属性导致的,因为并不是所有的NFS客户端将最后访问时间升级到从客户端读取的服务器端和客户端
//的数据,这将不会导致更新st_atime因为服务器端不会读文件.Unix语义可以通过客户端的属性缓存来阻止,
//但是在大多数情况下,这将大幅增加服务器的负担和降低系统性能.
Many file systems and disks were considered to be fast enough that the implementation of O_NONBLOCK was deemed unnecessary. So, O_NONBLOCK may not be available on files and/or disks.
//很多文件系统和磁盘都被认为是足够快的,那样O_NONBLOCK的执行就认为是不必要的.
//所以,O_NONBLOCK在文件和磁盘中就可能无效

SEE ALSO

       close(2), fcntl(2), ioctl(2), lseek(2), open(2), pread(2), readdir(2),
       readlink(2), readv(2), select(2), write(2), fread(3)

COLOPHON

       This page is part of release 3.29 of the Linux man-pages project.  A
       description of the project, and information about reporting bugs, can be found
       at http://www.kernel.org/doc/man-pages/.

Linux                                 2009-02-23                              READ(2)

<think>首先,用户的问题是:“我想查看Linux系统中man命令的中文版帮助文档或使用方法 请问Linux man command 中文使用手册”。所以,我需要提供关于man命令的中文使用手册。 根据系统级指令: - 所有行内数学表达式必须使用$...$格式,但这里没有数学表达式,所以可能不需要。 - 独立公式必须使用$$...$$格式并单独成段,同样,这里可能不适用。 - LaTeX语法正确:确保任何公式正确。 - 使用中文回答:整个回答要用中文。 - 生成相关问题:在回答末尾生成相关问题。 - 回答中引用的段落末尾自然地添加引用标识:引用[^1]等。 用户可见层: - 回答结构清晰,帮助用户逐步解决问题。 - 尽量保证回答真实可靠。 - 参考站内引用:有引用[1]、[2]、[3]提供的信息。 引用内容: - 引用[1]: 介绍man命令的基本语法:man [options] [section number] [command name]。 - 引用[2]: 区分内部命令和外部命令,内部命令可通过man bash查看。 - 引用[3]: 提示man命令的使用选项,如man n command显示指定章节,man -a command显示所有章节等。 我需要整合这些信息,提供一个清晰的中文使用手册。man命令的官方文档通常是英文的,但用户要求中文版,所以我会用中文解释。 结构: 1. **介绍man命令**:解释man是什么。 2. **基本语法**:给出基本用法。 3. **选项和参数**:详细说明常见选项。 4. **章节介绍**:解释man手册的章节。 5. **示例**:提供一些使用示例。 6. **如何获取中文帮助**:由于man手册默认是英文的,建议如何获取中文版或使用翻译工具。 7. **相关问题**:生成相关问题。 关于中文版帮助:实际上,man手册默认是英文的,但用户可以通过安装中文手册包来获取中文版。例如,在Debian/Ubuntu上,可以安装manpages-zh包。我会提到这一点。 生成相关问题:在末尾添加相关问题。 确保回答真实可靠:基于Linux标准知识。 最终回答结构: - 开头:介绍man命令。 - 主体:逐步解释使用方法。 - 结尾:如何获取中文帮助和相关问题。</think>### Linux man 命令中文使用手册 man 命令是 Linux 系统中用于查看命令、函数、配置文件等官方文档的工具。其名称来源于“manual”(手册),几乎所有标准程序和工具都有对应的 man 手册。默认情况下,man 手册是英文的,但您可以通过安装中文手册包(如 `manpages-zh`)来获取中文版。以下是详细的中文使用指南,结构清晰,便于逐步操作。 #### 1. **man 命令的基本介绍** - man 命令用于访问 Linux 系统文档,包括: - **命令**:如 `ls`, `grep`。 - **函数**:如 C 语言系统调用。 - **配置文件**:如 `/etc/passwd`。 - **其他**:如特殊文件(设备文件)。 - 手册分为多个章节(section),每个章节对应不同类型的内容。例如,章节 1 是用户命令,章节 5 是配置文件格式[^1]。 - 默认输出为英文,但安装中文包后,可显示中文内容。 #### 2. **基本语法** man 命令的基本语法格式为: ```bash man [选项] [章节号] [命令名] ``` - **[选项]**:可选参数,用于定制输出。 - **[章节号]**:可选,指定手册的章节(如 1, 2, 5)。 - **[命令名]**:要查询的命令或主题名称。 - 示例:`man ls` 查看 `ls` 命令的帮助。 #### 3. **常用选项详解** 以下是 man 命令的常用选项,帮助您更灵活地使用: - `man [命令名]`:显示默认章节的手册(通常是章节 1)。 - `man [章节号] [命令名]`:显示指定章节的手册。例如,`man 5 passwd` 查看 `/etc/passwd` 配置文件的文档(章节 5)[^3]。 - `man -a [命令名]`:显示所有章节的手册。例如,`man -a printf` 会依次显示 `printf` 命令(章节 1)和 C 语言函数(章节 3)的文档。 - `man -k [关键词]`:搜索关键词相关的所有手册页。类似于 `apropos` 命令。例如,`man -k "network"` 列出所有包含“network”的手册。 - `man -w [命令名]`:显示手册文件所在的路径。例如,`man -w ls` 输出 `/usr/share/man/man1/ls.1.gz`。 - `man -aw [命令名]`:结合 `-a` 选项,显示所有章节手册的路径[^3]。 - 其他实用选项: - `-f`:显示命令的简短描述,等同于 `whatis` 命令。 - `-h` 或 `--help`:显示 man 命令本身的帮助信息。 #### 4. **man 手册的章节结构** man 手册分为 9 个主要章节,了解这些有助于精确查询: - **章节 1**:用户命令(如 `ls`, `cd`)。 - **章节 2**:系统调用(如 `open`, `read`)。 - **章节 3**:库函数(如 `printf`, `malloc`)。 - **章节 4**:特殊文件(如设备文件 `/dev/sda`)。 - **章节 5**:文件格式和配置文件(如 `/etc/fstab`)。 - **章节 6**:游戏和屏幕保护程序。 - **章节 7**:杂项(如宏包和协议)。 - **章节 8**:系统管理命令(如 `mount`, `shutdown`)。 - **章节 9**:内核相关文档。 - 提示:Linux 命令可分为内部命令(内建命令,如 `cd`)和外部命令(独立程序)。内部命令可通过 `man bash` 查看所有列表[^2]。 #### 5. **如何获取中文版手册** 默认 man 手册是英文的,但您可以安装中文翻译包: - **安装中文手册包**(以 Debian/Ubuntu 为例): ```bash sudo apt update sudo apt install manpages-zh # 安装中文手册 ``` 安装后,使用 `cman` 命令代替 `man` 查看中文内容。例如:`cman ls`。 - **临时切换语言**:如果未安装中文包,可设置环境变量临时使用英文手册: ```bash LANG=zh_CN.UTF-8 man [命令名] # 部分系统支持中文输出 ``` - **替代方法**:如果中文包不可用,建议使用在线资源或翻译工具: - 访问 [Linux Manual Pages](https://man7.org/linux/man-pages/) 在线查看英文手册,并用浏览器翻译功能转为中文。 - 使用 `man [命令名] | grep -i [关键词]` 过滤关键信息后手动翻译。 #### 6. **实用示例** - 查看 `grep` 命令的中文帮助: ```bash cman grep # 如果安装了中文包 ``` - 搜索所有与“网络”相关的手册: ```bash man -k "network" ``` - 查看 `passwd` 配置文件的文档(指定章节 5): ```bash man 5 passwd ``` #### 7. **常见问题解决** - **问题:man 命令输出乱码?** 确保系统支持中文编码:运行 `locale` 检查语言设置,或安装中文字体包。 - **问题:找不到某些命令的手册?** 可能是未安装完整手册包:运行 `sudo apt install man-db` 更新数据库。 - **提示**:man 手册使用 less 分页器查看,按 `q` 退出,`/关键词` 搜索内容。 man 命令是 Linux 系统管理的核心工具,通过以上步骤,您可以高效获取中文帮助。如果安装中文包后仍遇问题,建议查阅系统文档或社区支持[^1][^2][^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值