bash手册 之重定向原理与实现

http://www.gnu.org/software/bash/manual/bashref.html#Redirections

http://www.cnblogs.com/weidagang2046/p/io-redirection.html 原理与实现

http://blog.csdn.net/taiyang1987912/article/details/39401265

[[email protected] tmp]# cd /dev/fd
[[email protected] fd]# ll
总用量 0
lrwx------ 1 root root 64 6月 4 12:17 0 -> /dev/pts/0
lrwx------ 1 root root 64 6月 4 12:17 1 -> /dev/pts/0
lrwx------ 1 root root 64 6月 4 12:17 2 -> /dev/pts/0
lrwx------ 1 root root 64 6月 4 12:17 255 -> /dev/pts/0

#使用exec将stdin重定向到文件
#!/bin/bash

exec 8<&0           #FD 8是FD 0的副本,用于恢复标准输入
exec < file         #将标准输入重定向到file
read a              #读取file的第一行
read b              #读取file的第二行
echo "read has read $$"
echo "read has read $PPID"
sleep 100
echo "----------------"
echo $a             #标准输出
echo $b             #标准输出

该进程的文件描述符FD 变为如下

[[email protected] fd]# ll
总用量 0
lr-x------ 1 root root 64 6月 4 13:03 0 -> /root/file
lrwx------ 1 root root 64 6月 4 13:03 1 -> /dev/pts/0
lrwx------ 1 root root 64 6月 4 13:03 2 -> /dev/pts/0
lr-x------ 1 root root 64 6月 4 13:03 254 -> /root/a.sh
lrwx------ 1 root root 64 6月 4 13:03 255 -> /dev/pts/0
lrwx------ 1 root root 64 6月 4 13:03 8 -> /dev/pts/0

echo "close FD 8:"
exec 0<&8 8<&- #将FD 8复制到FD 0,恢复FD 0,并关闭FD 8,其他进程可以重复使用FD 8
echo -n "Enter Data:"
read c #read从标准输入读取数据
echo $c
echo $$
echo $PPID
sleep 100

[[email protected] 4598]# ll fd
总用量 0
lrwx------ 1 root root 64 6月 4 13:11 0 -> /dev/pts/0
lrwx------ 1 root root 64 6月 4 13:11 1 -> /dev/pts/0
lrwx------ 1 root root 64 6月 4 13:11 2 -> /dev/pts/0
lr-x------ 1 root root 64 6月 4 13:11 254 -> /root/a.sh
lrwx------ 1 root root 64 6月 4 13:11 255 -> /dev/pts/0

 
时间: 2024-08-03 05:10:13

bash手册 之重定向原理与实现的相关文章

bash功能特性五 重定向和管道

一.管道 使用"|"符号来实现管道功能:管道可以实现在一行中处理使用管道符号连接的多个命令,后面的命令使用前面命令的输出结果做为处理对象. 二.数据流重定向 数据输入和输出的类型: 标准输入(stdin):代码为0,默认设备为键盘: 标准输出(stdout):代码为1,默认设备为显示器: 标准错误输出(stderr):代码为2,默认设备为显示器. 重定向就是将数据不使用默认的设备进行输入或输出. 1.输出重定向 标准输出重定向: COMMAND > POSITION:覆盖输出 C

linux bash 中的重定向

在 linux 中,这些重定向都是等价的 $ file command arg1 arg2 arg3 $ command > file arg1 arg2 arg3 $ command arg1 > file arg2 arg3 $ command arg1 arg2 > file arg3 $ command arg1 arg2 arg3 > file

bash帮助文档简单学习;bash手册翻译

bash的文档还是要学习的:man -a bash BASH(1) BASH(1) NAME bash - GNU Bourne-Again SHell SYNOPSIS bash [options] [file] COPYRIGHT Bash is Copyright (C) 1989-2005 by the Free Software Foundation, Inc. DESCRIPTION Bash is an sh-compatible command language interpre

linux下的IO重定向与管道相关的知识简析

一.bash重定向部分简单翻译 1.1.bash手册关于重定向短短的注解(因为过于经典,所以摘录出来) 我的翻译要开始毁经典啦... 参考:https://blog.csdn.net/spch2008/article/details/51433353/ https://www.cnblogs.com/lqminn/archive/2013/05/30/3108283.htmlhttps://bash.cyberciti.biz/guide/Here_stringshttps://bbs.csdn

谢烟客---------Linux之Bash基础特性

框架:     bash的引用:命令引用.变量引用     bash命令历史     bash中的通配符     bash中的管道     I/O重定向     命令补全     路径补全     bash的快捷键     命令的别名     命令行的展开      1)命令的执行结果与命令的执行状态结果      命令的执行结果: 用户输入命令+Enter后,命令如何执行     命令提示符,回车键后:bash切片,分析命令,[选项],参数,提请给内核,分配资源,运行为一个进程  1)用户接口

linux下部分文件管理类基本命令汇总以及bash展开特性介绍

一.文件管理类基本命令 1.1.表格汇总 今天要讲解的命令如下表所示,按照外部命令和内建命令做基本划分: 内建命令列表: 命令名字 基本说明 file 检测文件类型 cd 改变shell的工作目录 dirs 显示目录堆栈信息 popd 从堆栈中移除目录 pushd 向堆栈中添加目录 外部命令列表: 命令名字 基本说明 ls 列出目录内容 tree 以"树状"格式列出目录内容 pwd 打印当前工作路径的名字(是一个绝对路径) echo 显示文本行 cat 连接文件内容,并打印到标准输出

【转】Linux 技巧: Bash 参数和参数扩展

重点看下清单7 现在,很多 Linux® 和 UNIX® 系统上都有 bash shell,它是 Linux 上常见的默认 shell.通过本文,您将了解到如何在 bash 脚本中处理参数和选项,以及如何使用 shell 的参数扩展检查或修改参数.本文重点介绍 bash,文中的示例都是在以 bash 为 shell 的 Linux 系统上运行.但是,很多其他的 shell 中也有这些扩展,比如 ksh.ash 或 dash,您可以在其他 UNIX 系统或者甚至是 Cygwin 之类的环境中使用这

bash shell学习笔记(一)—— 常用命令

一.基本的bash shell命令 1.默认bash shell 提示符是美元符号($); 2.bash手册 使用man命令来访问存储在Linux系统上的手册页面,如: bogon:~ Mac$ man kill KILL(1)                   BSD General Commands Manual                  KILL(1) NAME kill -- terminate or signal a process SYNOPSIS kill [-s sig

Bash特性总结

Bash 是 GNU 计划中重要的工具软件之一,目前也是 Linux distributions 的标准 shell . bash 主要兼容于 sh ,并且依据一些使用者需求,而加强的 shell 版本.不论你使用的是那个 distribution ,你都难逃需要学习 bash 的宿命!那么这个 shell 有什么好处,干嘛 Linux 要使用他作为默认的 shell 呢? bash 主要的优点有底下几个: 命令历史 命令补全.路径补全 命令替换 命令行展开 命令别名 glob通配符 bash快