写在前面:案例、常用、归类、解释说明。(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. Dagger2学习之由浅入深

    概述 Dagger2是一款使用在Java和Android上的静态的,运行时依赖注入框架.官方地址:http://google.github.io/dagger/ 记得当初刚学习Dagger2的时候看了 ...

  2. C#中的占位符

    当我们需要在屏幕上输出一句话的时候,如果不断的使用+来连接各个字符串,一是容易出错,二是代码显示的非常乱.这时候,占位符就能够发挥作用! 占位符: string name="张三" ...

  3. C#学习第六天

    今天的内容主要是参数数组,C#允许函数指定一个(只能指定一个)特定的参数,这个参数必须是函数定义中最后一个参数,成为参数数组. 参数数组可以使用个数不定的参数调用函数,可以使用params关键字定义它 ...

  4. MVC4使用EF6连接mysql数据库

    1.需要安装MySql.Data.Entity.EF6,此dll可以在项目——>管理NuGet程序包里联机搜索MySql.Data.Entity.EF6并安装即可 2.连接字符串需要添加prov ...

  5. HTML5 File 对象

    实例说明1: <div class="container"> <input type="file" id="file" m ...

  6. target,currentTarget,delegateTarget,srcElement

    第一种情况:就是IE9+和其他现代浏览器,支持addEventListener方法.其结果是: this总是等于currentTarget currentTarget总是事件监听者 target总是事 ...

  7. struts2与spring整合问题,访问struts2链接时,spring会负责创建Action

    每次访问一次链接,spring会创建一个对象,并将链接所带的参数注入到Action的变量中(如何做到的呐) 因为: struts2的action每次访问都重新创建一个对象,那spring的ioc是怎么 ...

  8. [转] 与调试器共舞 - LLDB 的华尔兹

    你是否曾经苦恼于理解你的代码,而去尝试打印一个变量的值? NSLog(@"%@", whatIsInsideThisThing); 或者跳过一个函数调用来简化程序的行为? NSNu ...

  9. MySQL 删除数据库

    MySQL 删除数据库 使用 mysqladmin 删除数据库 使用普通用户登陆mysql服务器,你可能需要特定的权限来创建或者删除 MySQL 数据库. 所以我们这边使用root用户登录,root用 ...

  10. BOM和DOM详解

    DOM介绍 D(文档)可以理解为整个Web加载的网页文档,O(对象)可以理解为类似window对象只来的东西,可以调用属性和方法,这里我们说的是document对象,M(模型)可以理解为网页文档的树形 ...