写在前面:案例、常用、归类、解释说明。(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. java内存不足

    -Xmx1024m -Xms1024m -XX:PermSize=128m -XX:MaxPermSize=512m ------------------------- 亲测可用

  2. linux服务器上的php代码通过nginx发布,解决pathinfo模式问题

    附件1为修改前的正常访问php配置文件      附件2为修改后的能通过url地址访问php项目的配置文件    具体操作网址 如下:www.itokit.com/2012/0308/73275.ht ...

  3. Android(java)学习笔记234: 服务(service)之音乐播放器

    1.我们播放音乐,希望在后台长期运行,不希望因为内存不足等等原因,从而导致被gc回收,音乐播放终止,所以我们这里使用服务Service创建一个音乐播放器. 2.创建一个音乐播放器项目(使用服务) (1 ...

  4. HTML5 微信二维码提示框

    这是一个js的小案例,主要效果是显示一个微信二维码的提示框,非常简单实用. 源码如下: JS部分 <script src="js/jquery-1.8.3.min.js"&g ...

  5. sql - 面试

    一,关于 group by 表内容: 2005-05-09 胜 2005-05-09 胜 2005-05-09 负 2005-05-09 负 2005-05-10 胜 2005-05-10 负 200 ...

  6. linux command cp.

    Examples cp file1.txt newdir Copies the file1.txt in the current directory to the newdir subdirector ...

  7. IMP数据文件时ORA-00959错误分析

    有时间模拟一下ORA-00959的测试分析.

  8. SQL DMO のDMV

    这两天对公司的一个项目进行优化,看着长长的SQL,脑袋不经有些大,一时间竟然不知道如何下手,一顿手忙脚乱后,小有成效,响应速度快了不少,同样的条件下可以做到秒级响应.闲暇时间想了想,还是得做点功课,最 ...

  9. 防止sql注入 参数化解决方案

    StringBuilder strSql=new StringBuilder(); strSql.Append("insert into T_SysLog("); strSql.A ...

  10. linux ulimit的使用,如何产生core文件,调试段错误

    ---恢复内容开始--- 下面先简单介绍下ulimit命令: 1. limit -a 可以查看系统各种资源的限制,如: core文件大小,数据段的大小等. $ ulimit -a core file ...