Linux 使用文件描述符标识每个文件对象。文件描述符是一个非负整数,可以唯一地标识会话中打开的文件。每个进程中最多可以有9个打开文件的描述符。

Linux 标准文件描述符

文件描述符  缩写     描述

0       STDIN      标准输入

1         STDOUT     标准输出

2       STDERR    标准错误

STDIN 文件描述符引用 shell 的标准输入。

STDOUT 文件描述符引用 shell 的标准输出。

STDERR 文件描述符处理错误消息。

可以将 STDEER 和 STDOUT 输出重定向到同一个输出文件,使用 &> 符号

ls -al test test2 test3 badtet &> test7

或者

command>&file:将命令的标准错误输出一起重定向到一个文件。

find /etc -name passwd >alloutlput >&

将标准输出、错误输出都存入到alloutout文件中

在脚本中重定向输出

在脚本中使用文件描述符在多个位置生成输出,只要重定向相应的文件描述符即可。

在文件描述符编号前面添加 & 号,表示在脚本的文件描述符指向的地方而不是普通的 STDOUT 上显示文本。

示例

#!/bin/bash
# testing STDEERR messages

echo "This is an error" >&2
echo "This is normal output"

使用 exec 命令通知 shell 在脚本执行期间重定向特定的文件描述符

示例

#!/bin/bash
# redirecting output to different locations

exec 2>testerror

echo "This is the start of the script"
echo "now redirecting all output to another location"

exec 1>testout

echo "This output should go to the testout file"
echo "but this should go to the testerror file" >&2

[root@tang sh]# ./test12-11
This is the start of the script
now redirecting all output to another location

在脚本中重定向输入

示例

#!/bin/bash
# redirecting file input

exec 0< states
count=1

while read line
do
echo "Line #$count: $line"
count=$[ $count + 1 ]
done

[root@tang sh]# ./test12-12
Line #1: Alabama
Line #2: Alaska
Line #3: Arizona
Line #4: Arkansas
Line #5: Colorado
Line #6: Connecticut
Line #7: Florida
Line #8: Georgia

自定义输出文件描述符

shell 中最多有9个打开的文件描述符,可以将其余的文件描述符应用到任何文件,然后在脚本中作用它们。

示例

#!/bin/bash
# using an alternative file descriptor

exec 3>test3out

echo "this should display on the monitor"
echo "and this should be stored in teh file" >&3
echo "then this should be back on the monitor"

[root@tang sh]# ./test12-13
this should display on the monitor
then this should be back on the monitor

重定向文件描述符

可以将 STDOUT 的原位置重定向到备选文件描述符,然后将该文件描述符重定向加 STDOUT。

示例

#!/bin/bash
# storing STDOUT, then coming back to it

exec 3>&1
exec 1>test14out

echo "this should store in the output file"
echo "along with this line."

exec 1>&3

echo "now things should be back to normal"

[root@tang sh]# ./test12-14
now things should be back to normal

自定义输入文件描述符

示例

#!/bin/bash
# redirecting input file descriptors

exec 6<&0

exec 0<states

count=1
while read line
do
echo "Line #$count: $line"
count=$[ $count + 1 ]
done

exec 0<&6
read -p "Are you done now?" answer
case $answer in
Y|y) echo "Goodby" ;;
N|n) echo "Sorry, this is the end." ;;
esac

关闭文件描述符

如果创建新的输入或输出文件描述符, shell 将在脚本退出时自动关闭它们。有时需要在脚本结束前手动关闭文件描述符。

要关闭文件描述符,将它重定向到特殊符号 &- 。

示例

#!/bin/bash
# testing closing file descriptors

exec 3>test17file
echo "This is a test line of data" >&3
exec 3>&-

cat test17file

exec 3>test17file
echo "This'll be bad" >&3

列出开放文件描述符

lsof 命令列出整个 Linux 系统上所有的开放文件描述符。

-p 指定进程 ID

-d 指定要显示的文件描述符编号

-a 连接其他两个选项的结果

示例

#!/bin/bash
# testing lsof with file descriptors

exec 3>test18file1
exec 6>test18file2
exec 7<states

/usr/sbin/lsof -a -p $$ -d0,1,2,3,6,7

禁止命令输出

如果不希望显示任何脚本输出,可以将 STDERR 重定向到称为空文件的特殊文件。

示例

ls -al > /dev/null

可以使用 /dev/null 文件作为输入文件用于输入重定向,由于 /dev/null 文件不包含任何内容,可以使用它快速将数据从现有文件移除。

cat /dev/null > testfile

创建临时文件

mktemp 命令可以轻松在 /tmp 文件夹中创建一个唯一的临时文件。

默认情况下,mktemp 在本地目录创建文件,需要指定一个文件名模板。模板包括文件名以及附加到文件名后的6个X。mktemp 命令的输出是它所创建的文件的名称。

示例

#!/bin/bash
# creating and using a temp file

tempfile=`mktemp test19.XXXXXX`
exec 3>$tempfile

echo "This scrit writes to temp file $tempfile"

echo "This is the first line" >&3
echo "This is the second line." >&3
exec 3>&-

echo "Done creating temp file. The contents are:"
cat $tempfile
rm -f $tempfile 2>/dev/null

-t 选项强迫 mktemp 在系统的临时文件夹中创建文件。

示例

#!/bin/bash
# creating a temp file in /tmp

tempfile=`mktemp -t tmp.XXXXXX`
echo "This is a test file" > $tempfile
echo "This is the second line of the test." >> $tempfile

echo "The temp file is located at: $tempfile"
cat $tempfile
rm -f $tempfile

-d 选项让 mktemp 创建一个临时目录而不是一个文件。

示例

#!/bin/bash
# using a temporary directory

tempdir=`mktemp -d dir.XXXXXX`
cd $tempdir
tempfile1=`mktemp temp.XXXXXX`
tempfile2=`mktemp temp.XXXXXX`
exec 7>$tempfile1
exec 8>$tempfile2

echo "Sending data to directory $tempdir"
echo "This is a test line of data for $tempfile1" >&7
echo "This is a test line of data for $tempfile2" >&8

记录消息

使用 tee 命令将输出同时发送到监视器和日志文件。

tee 命令重定向来自 STDIN 的数据,可以与管道命令配置使用重定向任何命令的输出。

示例

#!/bin/bash
# using the tee command for logging

tempfile=test22file

echo "this is the start of the test" | tee $tempfile
echo "this is the second line of the test" | tee -a $tempfile
echo "this is the end of the test" | tee -a $tempfile

Shell 语法之输入输出的更多相关文章

  1. makefile中的shell语法

    在Makefile中写shell代码有点诡异,和不同的shell语法不太一样,如果不了解,看Makefile会莫名其妙.下面总结了一些. 1:尽在Makefile文件的目标项冒号后的另起一行的代码才是 ...

  2. shell中的输入输出和编程中的变量(shell 03)

    shell中的输入输出标准输入:键盘标准输出:终端显示器>> 追加是换行追加的echo -n 不尾随换行符 -e 启用解释反斜杠的转义功能 -E 禁用解释反斜杠的转义功能(默认) --he ...

  3. Hadoop学习笔记之HBase Shell语法练习

    Hadoop学习笔记之HBase Shell语法练习 作者:hugengyong 下面我们看看HBase Shell的一些基本操作命令,我列出了几个常用的HBase Shell命令,如下: 名称 命令 ...

  4. Shell语法规范

    ver:1.0 博客:https://www.cnblogs.com/Rohn 本文介绍了Shell编程的一些语法规范,主要参考依据为谷歌的Shell语法风格. 目录 背景 使用哪一种Shell 什么 ...

  5. shell语法习题练习进阶版

    第4章 shell语法深度习题练习 4.1 使用if,case,函数的方法将服务改成system(centos6) 4.1.1 if方法 4.1.1.1 system实现 4.1.1.1.1 编写代码 ...

  6. 读完学会shell语法,shell脚本80%已经学会

    第3章 shell语法讲解 3.1 shell运算讲解 3.1.1 运算符的讲解 3.1.2 shell运算方式的讲解 3.1.2.1 $(())运算 [root@m01 test_init] # a ...

  7. shell语法(二)

    Shell脚本语法 条件测试:test. [ ] 命令test或[可以测试一个条件是否成立,如果测试结果为真,则该命令的Exit Status为0,如果测试结果为假,则命令的Exit Status为1 ...

  8. shell语法学习

    [原文] 菜鸟笔记shell教程学习. 本篇博客只是记录shell的一些关键语法,主要是做一个记录,有些内容也是copy过来的,并不是一个完整的教程,想完整学习shell的同学可以前往 shell脚本 ...

  9. shell语法

    基本语法列表 #linux组成:内核+工具 #linux启动: . getty:提示登录名和密码,输入之后调用login . login:login验证用户名和密码,然后调用shell . shell ...

随机推荐

  1. 接口测试从未如此简单 - Postman (Chrome插件)

    接口测试从未如此简单 - Postman (Chrome插件) 一个非常有力的Http Client工具用来测试Web服务的, 我这里来介绍如何用它测试restful web service 注:转载 ...

  2. PHPExcel导出数据

    require_once './class/Excel/PHPExcel.php'; //将(1,1)转换成"A1"形式 function getCoordinate($row, ...

  3. KVC和KVO大优点

    都是动态的,运行时检查,给予了极大的方便

  4. Maven-010-maven 编译报错:Failure to ... in ... was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced.

    今晚在编译 maven 项目的时候,命令行报错,出现 Failure to ... in ... 类似错误,详细的错误信息如下所示: [INFO] -------------------------- ...

  5. Javascript模块化编程(三):require.js的用法 作者: 阮一峰

    声明:转载自阮一峰的网络日志 这个系列的第一部分和第二部分,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战. 我采用的是一个非常流行的库require.js. 一.为什么要用 ...

  6. c#:浅克隆和深克隆,序列化和反序列化

    一.浅克隆和深克隆(浅复制和深复制)浅克隆和深克隆最典型的应用是数据集对象DataSet的Clone和Copy方法.Clone()方法用来复制DataSet的结构,但是不复制DataSet的数据,实现 ...

  7. toggle函数

    $(function() { $('.love').toggle(function() { $(this).attr("src", "images/loved.png&q ...

  8. C# 遍历类的属性并取出值

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精    今天有点胡思乱想,想遍历MVC Model的属性并 ...

  9. leetcode_222 Count Complete Tree Nodes

    题目: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree fr ...

  10. 酶设计软件rosetta安装

    1.Rosetta uses SCons as a compile assistant. You will likely need to download and install this first ...