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. [转]MongoDB密码设置(基于windows)

    参考文档:http://www.cnblogs.com/zengen/archive/2011/04/23/2025722.html   MongoDB部署到Windows上后是默认是无权限限制的的. ...

  2. Vim ide for shell development

    Source : This article is part of the ongoing Vi / Vim Tips and Tricks Series. As a Linux sysadmin or ...

  3. JSON 数组的遍历解析

    刚遇到一个接接口任务,发现其中返回数据中,是个字符串数组,数组中就是单个json形式的内容,其实应该也可以称这种数据叫做json数组吧,只不过是字符串形式.而我需要的是将这种内容解析出来,取到对于ke ...

  4. 成都PC网站建设需要考虑哪些费用呢

    亿合科技PC建设小编分享下:成都PC网站建设需要考虑哪些费用呢?随互联网的发展,越来越多人想建设自己网站,站长最关心的问题之一就是网站建设需要多少钱.每个网站建设的费用都是不一样的,但是都需要涵盖几个 ...

  5. linux 下查看系统资源和负载,以及性能监控

    1,查看磁盘   df -h   2,查看内存大小   free   free [-m|g]按MB,GB显示内存   vmstat   3,查看cpu   cat /proc/cpuinfo   只看 ...

  6. 解决SVN不显示状态图标

    打开注册表,找到"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlay ...

  7. Hdu 1443 Joseph

    Joseph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  8. CSS3 border-image详解、应用

    一.border-image的兼容性 border-image可以说是CSS3中的一员大将,将来一定会大放光彩,其应用潜力真的是非常的惊人.可惜目前支持的浏览器有限,仅Firefox3.5,chrom ...

  9. 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数002·AI人工智能

    <zw版·Halcon-delphi系列原创教程> Halcon分类函数002·AI人工智能 AI人工智能:包括knn.gmm.svm等 为方便阅读,在不影响说明的前提下,笔者对函数进行了 ...

  10. jQuery 选择器 (基础恶补)

    jQuery 元素选择器 jQuery 使用 CSS 选择器来选取 HTML 元素. $("p") 选取 <p> 元素. $("p.intro") ...