写在前面:案例、常用、归类、解释说明。(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. XMPPFrameWork IOS 开发(一)xmpp简介

    原始地址:XMPPFrameWork IOS 开发(一) XMPP : The Extensible Messaging and Presence Protocol 中文全称: 可扩展通讯和表示协议 ...

  2. Android Fragment动态添加 FragmentTransaction FragmentManager

    Fragment常用的三个类:android.app.Fragment 主要用于定义Fragmentandroid.app.FragmentManager 主要用于在Activity中操作Fragme ...

  3. JQuery请求WebService返回数据的几种处理方式

    打开自己的博客仔细浏览了一番,发现已经好久没有写博客了,由于最近一直比较忙碌懈怠了好多.默默反省三分钟.......言归正传,现在就对最近在学习webservice的过程中遇到的几种类型的问题中我的理 ...

  4. MongoDB C++ 2.4.5 driver 编译安装问题

    安装参考前文,http://blog.csdn.net/sheismylife/article/details/8794589 方法一致.只不过这次在GCC4.8.1上编译. scons instal ...

  5. Meth | phpstorm 2016.2 的最新破解方法(截止2016-8-1)

    今天刚更新了phpstorm 2016.2版本,发现网上提供的破解地址都有问题,即*.lanyus.com及*.qinxi1992.cn下的全部授权服务器已遭JetBrains封杀. 最后网上找到一个 ...

  6. 如何设置jsp默认的编码为utf-8

    方法一: 文件里写: <%@ page contentType="text/html; charset=UTF-8"  %> 方法二: 选择window –> P ...

  7. 使用 trait 时报PHP Parse error: syntax error, unexpected 'use' (T_USE) 这个错误

    找一大圈原因, 最后终于找到了, 不是PHP版本的原因[], 是自己把use 写到了类里的方法里了. 这个东东,  不能脱离类单独使用, 否则的话, 会被认为是命名空间了. 测试例子如下 // Tra ...

  8. git 创建远程仓库

    在远程服务器上$ cd /server/path/ $ git init --bare myproject.git 在本地 1> $ cd /client/path/ 运行 git init 2 ...

  9. 一些 Windows 系统不常见的 鼠标光标常数

    一些 Windows  系统不常见的 鼠标光标常数 Private Declare Function SetCursor Lib "user32" (ByVal hCursor A ...

  10. JavaScript ArrayBuffer浅析

    时隔一年半,再次来到博客园.回首刚接触前端时所写的两篇随笔,无法直视啊~ --------------------------------------------------------------- ...