写在前面:案例、常用、归类、解释说明。(By Jim)

2>将STDEER输入到一个文件
1>将STDOUT输入到一个文件
&>将STDEER和STDOUT输入到同一个文件

在脚本中重定向输入

#!/bin/bash
# redirecting file input exec <testfile
count= while read line
do
echo "Line #$count:$line"
count=$[ $count + ]
done

(从文件testfile中读取数据)

创建自己的重定向

#!/bin/bash
# using an alternative file descriptor exec >testout echo "This should display on the monitor"
echo "This should be stored in the file">&
echo "Then this should be back on the monitor"

(第二行将被写入文件当中)

重定向文件描述符

#!/bin/bash
# storing STDOUT,then coming back to it exec >&
exec >testout echo "This should display on the monitor"
echo "This should be stored in the file"
echo "Then this should be back on the monitor" exec >& echo "Now things should be back to normal"

(中间的三行将被写入文件testout中,描述符3重定向到文件描述符1的当前位置,也就是STDOUT)

创建输入文件描述符

#!/bin/bash
# redirecting input file descriptors exec <& exec <testfile count=
while read line
do
echo "Line #$count:$line"
count=$[ $count + ]
done
exec <&
read -p "Are you done now?" answer
case $answer in
Y|y) echo "Goodbye";;
N|n) echo "Sorry,this is the end.";;
esac

(貌似执行完了,又回到请求页面了,如果没有exec 6<&0,就会直接执行完毕了)

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

#!/bin/bash
# testing closing file descriptors exec >testfile echo "This is a test line of data">& exec >&- echo "This won't work">&

(最后一句将不会写入文件,并且报错,因为文件描述符3已经关闭)

列出开放文件描述符
lsof命令列出整个Linux系统上所有的开放文件描述符。
-p可以指定进程ID(PID)
-d可以指定要显示的文件描述符编号

禁止命令输出
有时候不希望显示任何脚本输出,解决的办法,是将STDERR重定向到称为空文件null file的特殊文件。

使用临时文件
mktemp命令可以轻松在/tmp文件夹中创建一个唯一的临时文件。
它仅向文件所有者分配读取和写入权限,并使您成为文件的所有者。
mktemp testing.XXXXXX
就会创建一个临时文件

#!/bin/bash
# creating and using a temp file tempfile=`mktemp test.XXXXXX`
exec >$tempfile echo "This script writes to temp file $tempfile" echo "This is the first line">&
echo "This is the second line">&
echo "This is the last line">&
exec >&- echo "Done creating temp file.The contents are:"
cat $tempfile
rm -f $tempfile >/dev/null

结果:
This script writes to temp file test.tspEXq
Done creating temp file.The contents are:
This is the first line
This is the second line
This is the last line

在/temp中创建临时文件
-t选项强迫mktemp在系统的临时文件夹中创建文件。但使用该特性时,mktemp命令返回用于创建临时文件的完整路径名
[root@localhost shellscript]# mktemp -t test.XXXXXX
/tmp/test.v44fqo

#!/bin/bash
# creating a temp file in /tmp tempfile=`mktemp -t test.XXXXXX`
exec >$tempfile echo "This script writes to temp file $tempfile" echo "This is the first line">&
echo "This is the second line">&
echo "This is the last line">&
exec >&- echo "Done creating temp file.The contents are:"
cat $tempfile
rm -f $tempfile >/dev/null

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

#!/bin/bash
# using a temporary directory tempdir=`mktemp -d dir.XXXXXX`
cd $tempdir
tempfile1=`mktemp temp.XXXXXX`
tempfile2=`mktemp temp.XXXXXX`
exec >$tempfile1
exec >$tempfile2 echo "Sending data to directory $tempdir"
echo "This is a test line of data for file1">&
echo "This is a test line of data for file2">&

记录消息
有时很有必要将输出同时发送到监视器和日志文件。tee命令即可。
tee命令就像管道的T型接头。它将STDIN的数据同时发送到两个目的地。一个是STDOUT,另一个是tee命令行指定的文件名:
如果希望向文件添加数据,则必须使用-a选项:

#!/bin/bash
# using the tee command for logging tempfile=testfile echo "This is the first line"|tee $tempfile
echo "This is the second line"|tee -a $tempfile
echo "This is the last line"|tee -a $tempfile

(既能显示在屏幕上,又能保存到日志中)

Linux&shell之显示数据的更多相关文章

  1. linux shell突然显示-bash-4.1#的解决方法

    老沙昨天还登录这个linux服务器,并且命令行好好的,今天突然在linux shell中不显示路径了,显示为-bash-4.1#. 以下是老沙的解决方案 vim ~/.bash_profile 如果没 ...

  2. SSH Secure Shell Client连接Linux 命令行显示中文乱码问题 和oracle 查询数据中文乱码问题

    一.SSH Secure Shell Client连接Linux 命令行显示中文乱码问题 linux 设置系统语言 修改 /etc/sysconfig/i18n 文件,如 LANG="en_ ...

  3. [转]linux shell数据重定向(输入重定向与输出重定向)详细分析

      在了解重定向之前,我们先来看看linux 的文件描述符. linux文件描述符:可以理解为linux跟踪打开文件,而分配的一个数字,这个数字有点类似c语言操作文件时候的句柄,通过句柄就可以实现文件 ...

  4. I.MX6 Android Linux shell MMPF0100 i2c 获取数据

    #!/system/bin/busybox ash # # I.MX6 Android Linux shell MMPF0100 i2c 获取数据 # 说明: # 本文主要记录通过shell脚本来获取 ...

  5. I.MX6 Android Linux shell MMPF0100 i2c 设置数据

    #!/system/bin/busybox ash # # I.MX6 Android Linux shell MMPF0100 i2c 设置数据 # 说明: # 本文主要记录通过shell脚本来设置 ...

  6. linux shell中不显示路径了,显示为-bash-4.1#的两种解决办法

    出现这个问题的原因是因为没有配置.bash_profile的问题,或者是我们不小心清空或删除了.bash_profile文件. 办法一:修改 ~/.bash_profile文件 步骤如下: vim ~ ...

  7. linux shell数据重定向(输入重定向与输出重定向)详细分析

    linux shell下常用输入输出操作符是: 1. 标准输入 (stdin) :代码为 0 ,使用 < 或 << : /dev/stdin -> /proc/self/fd/ ...

  8. Linux shell脚本编程(一)

    Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...

  9. Linux Shell 重定向与管道【转帖】

    by 程默 在了解重定向之前,我们先来看看linux 的文件描述符. linux文件描述符:可以理解为linux跟踪打开文件,而分配的一个数字,这个数字有点类似c语言操作文件时候的句柄,通过句柄就可以 ...

随机推荐

  1. ScriptManager的使用方法

    脚本管理控件(ScriptManger)是ASP.NET AJAX中很重要的控件,通过使用ScriptManger可以进行整个页面的局部更新的管理.ScriptManger用来处理页面上局部更新,同一 ...

  2. service redis does not support chkconfig的解决办法

    原文链接: http://my.oschina.net/maczhao/blog/322931 问题解决办法如下: 必须把下面两行注释放在/etc/init.d/redis文件靠前的注释中: # ch ...

  3. Linux基础系列—Linux体系结构和Linux内核结构

    /** ****************************************************************************** * @author    暴走的小 ...

  4. Ext4.1 grid 多选(可无checkbox)

    转载 在Ext4.1中的grid默认只能实现单选的. 如果你想要你的grid有多选功能,需要给grid增加selModel 如果你使用了Ext.create('Ext.selection.Checkb ...

  5. FusionChart学习笔记(部分)

    目录 第一阶段    1 一.创建第一个FusionChart    1 (1)导入js文件    1 (2)定义Div Id    1 (3)定义xml格式的数据文件    1 (4)编写js代码  ...

  6. UIGestureRecognizer手势识别

    UIGestureRecognizer 1.#import "ViewController.h"2.3.@interface ViewController ()<UIGest ...

  7. [技术翻译]Guava-libraries(一): 用户指导

    用户指导 本文翻译自http://code.google.com/p/guava-libraries/wiki/GuavaExplained,由十八子将翻译,发表于博客园 http://www.cnb ...

  8. 重新开始学习javase_隐藏实施过程

    一.隐藏实施过程 对于隐藏实施过程,thinking in java中讲了很好,无非就是一个好的程序尽量做到,对外公开的程序,即使内部程序发生变动,也不会影响这些公开的服务的使用 类的导入java中的 ...

  9. PHP后台传值

    前台数据往后台传值,往往是新手最头痛的,最近在学习thinkPHP的时候,也遇到了这种问题,总结一下,往不足之处请大家指教. 一.前台界面代码,往后台传值有两种方式,一种是get,另一种是post,新 ...

  10. tomcat启动报错:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these environment variable

    linux 下 启动tomcat 报: Neither the JAVA_HOME nor the JRE_HOME environment variable is definedAt least o ...