【Linux最强学习笔记3】Linux输入输出管理

    科技2026-06-17  21

    文章目录

    1.输入输出的定义2.管理输入3.管理系统输出4.例题

    1.输入输出的定义

    字符设备 是显示字符到屏幕上的设备文件 输入输出文件名文件描述符设备标准输入stdin0键盘标准正确输出stdout1显示器标准错误输出stderr2显示器

    2.管理输入

    输入重定向:

    类型语法功能标准输入命令 < 文件1把文件1的内容定向到程序中多行输入命令 << 标识符读入输入内容,直到标识符再次出现

    举例:

    tr 'a-z' 'A-Z' < test #把test文件中的内容定向到tr程序中 tr 'a-z' 'A-Z' << EOF #多行录入 abcdefgh xsdsaa EOF #标识符任意,再次出现时表示录入结束

    3.管理系统输出

    输出重定向:

    类型语法功能覆盖输出命令 > 文件将内容覆盖输出到文件中追加输出>>将内容追加输出到文件中

    举例:

    find /etc -name passwd > file.out #重定向正确输出 find /etc -name passwd 2> file.err #重定向错误输出 find /etc -name passwd &> file.all #重定向所有输出 find /etc -name passwd >> file.out #追加正确输出 find /etc -name passwd 2>> file.err #追加错误输出 find /etc -name passwd &>> file.all #追加所有输出

    管道符 ’ | ':

    command A | command B 将A命令的输出作为B命令的输入 2>&1 把编号为2的输入转换到编号为1的输出中 tee 复制输出到指定位置

    管道在一条命令中可以使用多次

    4.例题

    在普通用户下执行命令完成以下操作: 1.查找/etc/下的passwd文件屏蔽错误输出 2.查找/etc/下的passwd文件正确输出保存到/tmp目录中的westos.out中,错误输出保存到/tmp/目录中的westos.err中 3.查找/etc/下的passwd文件保存所有输出到/tmp目录中的westos.all中并统计输入的行数 4.查找/etc/下的passwd文件统计输出行数并显示输出内容 5.转换/etc/目录中passwd文件中的所有字母为答谢并统计文件行数 6.请用脚本非交互模式编写文件westos.file内容为: hello linux hello westos hello linux westos linux is very nice !!

    #1.查找/etc/下的passwd文件屏蔽错误输出 [westos@localhost Desktop]$ find /etc/ -name passwd 2> /dev/null #2.查找/etc/下的passwd文件正确输出保存到/tmp目录中的westos.out中,错误输出保存到/tmp/目录中的westos.err中 [westos@localhost Desktop]$ find /etc/ -name passwd > /tmp/westos.out 2> /tmp/westos.err #3.查找/etc/下的passwd文件保存所有输出到/tmp目录中的westos.all中并统计输入的行数 [westos@localhost Desktop]$ find /etc -name passwd 2>&1 | tee /tmp/westos.all | wc -l #4.查找/etc/下的passwd文件统计输出行数并显示输出内容 [westos@localhost Desktop]$ find /etc -name passwd 2>&1 | tee /dev/pts/0 | wc -l #5.转换/etc/目录中passwd文件中的所有字母为大写并统计文件行数 [westos@localhost Desktop]$ tr 'a-z' 'A-Z' < /etc/passwd | wc -l #6.请用脚本非交互模式编写文件westos.file内容为: # hello linux # hello westos # hello linux # westos linux is very nice !! [westos@localhost Desktop]$ vim westos.sh cat > westos.file << EOF hello linux hello westos hello linux westos linux is ver nice!! EOF [westos@localhost Desktop]$ sh westos.sh hello linux hello westos hello linux westos linux is ver nice!!
    Processed: 0.009, SQL: 9