shell脚本之exec操作文件描述符 + 示例
2016-03-29 15:02
387 查看
关于exec操作文件描述符主要注意事项:
1、exec 3</home/shell/testdir/a.txt //以“只读方式”打开/home/shell/testdir/a.txt,文件描述符对应为3
2、exec 3>/home/shell/testdir/a.txt //以“只写方式”打开/home/shell/testdir/a.txt,文件描述符对应为3
3、exec 3<>/home/shell/testdir/a.txt //以“读写方式”打开/home/shell/testdir/a.txt,文件描述符对应为3
4、exec 3<&- //关闭文件描述符3
简单示例,实现功能是从a.txt文件中提取第2,4,6行的内容打印到终端并且存储到b.txt文件中,代码如下所示:
#!/bin/bash
file="/home/shell/testdir/a.txt"
exec 3>/home/shell/testdir/b.txt
for N in 2 4 6;do
line=`head -$N $file | tail -1`
echo $line
echo "$line" >&3
done
echo 3>&-
运行结果:
1、exec 3</home/shell/testdir/a.txt //以“只读方式”打开/home/shell/testdir/a.txt,文件描述符对应为3
2、exec 3>/home/shell/testdir/a.txt //以“只写方式”打开/home/shell/testdir/a.txt,文件描述符对应为3
3、exec 3<>/home/shell/testdir/a.txt //以“读写方式”打开/home/shell/testdir/a.txt,文件描述符对应为3
4、exec 3<&- //关闭文件描述符3
简单示例,实现功能是从a.txt文件中提取第2,4,6行的内容打印到终端并且存储到b.txt文件中,代码如下所示:
#!/bin/bash
file="/home/shell/testdir/a.txt"
exec 3>/home/shell/testdir/b.txt
for N in 2 4 6;do
line=`head -$N $file | tail -1`
echo $line
echo "$line" >&3
done
echo 3>&-
运行结果:
相关文章推荐
- Linux socket 初步
- android wifi 无线调试
- Linux Kernel 4.0 RC5 发布!
- linux lsof详解
- linux 文件权限
- Linux 执行数学运算
- 10 篇对初学者和专家都有用的 Linux 命令教程
- Linux 与 Windows 对UNICODE 的处理方式
- Ubuntu12.04下QQ完美走起啊!走起啊!有木有啊!
- 解決Linux下Android开发真机调试设备不被识别问题
- 运维入门
- 运维提升
- Linux 自检和 SystemTap
- 动态清空 nohup 输出文件
- install scrapy with pip and easy_install
- Ubuntu Linux使用体验
- c语言实现hashmap(转载)