Linux&shell之显示数据
写在前面:案例、常用、归类、解释说明。(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之显示数据的更多相关文章
- linux shell突然显示-bash-4.1#的解决方法
老沙昨天还登录这个linux服务器,并且命令行好好的,今天突然在linux shell中不显示路径了,显示为-bash-4.1#. 以下是老沙的解决方案 vim ~/.bash_profile 如果没 ...
- SSH Secure Shell Client连接Linux 命令行显示中文乱码问题 和oracle 查询数据中文乱码问题
一.SSH Secure Shell Client连接Linux 命令行显示中文乱码问题 linux 设置系统语言 修改 /etc/sysconfig/i18n 文件,如 LANG="en_ ...
- [转]linux shell数据重定向(输入重定向与输出重定向)详细分析
在了解重定向之前,我们先来看看linux 的文件描述符. linux文件描述符:可以理解为linux跟踪打开文件,而分配的一个数字,这个数字有点类似c语言操作文件时候的句柄,通过句柄就可以实现文件 ...
- I.MX6 Android Linux shell MMPF0100 i2c 获取数据
#!/system/bin/busybox ash # # I.MX6 Android Linux shell MMPF0100 i2c 获取数据 # 说明: # 本文主要记录通过shell脚本来获取 ...
- I.MX6 Android Linux shell MMPF0100 i2c 设置数据
#!/system/bin/busybox ash # # I.MX6 Android Linux shell MMPF0100 i2c 设置数据 # 说明: # 本文主要记录通过shell脚本来设置 ...
- linux shell中不显示路径了,显示为-bash-4.1#的两种解决办法
出现这个问题的原因是因为没有配置.bash_profile的问题,或者是我们不小心清空或删除了.bash_profile文件. 办法一:修改 ~/.bash_profile文件 步骤如下: vim ~ ...
- linux shell数据重定向(输入重定向与输出重定向)详细分析
linux shell下常用输入输出操作符是: 1. 标准输入 (stdin) :代码为 0 ,使用 < 或 << : /dev/stdin -> /proc/self/fd/ ...
- Linux shell脚本编程(一)
Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...
- Linux Shell 重定向与管道【转帖】
by 程默 在了解重定向之前,我们先来看看linux 的文件描述符. linux文件描述符:可以理解为linux跟踪打开文件,而分配的一个数字,这个数字有点类似c语言操作文件时候的句柄,通过句柄就可以 ...
随机推荐
- SAP-MM:发票、贷方凭证、事后借记、后续贷记
发票和事后借记 相同点:增加对供应商的应付款 不同点:针对同一订单收货,发票要先于事后借记(事后借记是对供应商后期发票金额的补充):发票和金额.订单数量有关系,而事后借记只是订单金额调整的凭证,仅仅是 ...
- 火球-UML大战需求分析(体验版3.0.2).pdf
火球-UML大战需求分析(体验版3.0.2).pdf http://files.cnblogs.com/files/happlyonline/%E7%81%AB%E7%90%83-UML%E5%A4% ...
- 黑马程序猿_try-catch-finally
------- android培训.java培训.期待与您交流! ---------- try-catch-finally中怎样定义语句呢? 1.try块中主要定义可能出现的异常处理语句 2.catc ...
- dom4j处理java中xml还是很方便的
http://blog.csdn.net/chenghui0317/article/details/11486271 输入: String flighter = RequestUtil.get(&qu ...
- iOS 类似美团外卖 app 两个 tableView 联动效果实现
写在前面 首先声明哈,不是广告,我就是用的时候觉得这个功能比较好玩,就想着实现了一下.效果如图: 接下来简单的说一下思路吧~ 大体思路 可能我们看到这种功能的实现的时候,首先想着的是我在这个控制器中左 ...
- HDU 5037 Frog(贪心)
题意比较难懂,一只青蛙过河,它最多一次跳L米,现在河中有石头,距离不等,上帝可以往里加石头,青蛙非常聪明,它一定会选择跳的次数最少的路径.问怎么添加石头能让青蛙最多的次数.输出青蛙跳的最多的次数. 考 ...
- Android 项目建立步骤
使用eclipse,进行安卓开发,在建立项目的时候,有些步骤必须注意的, 本文就是对使用eclipse进行android开发的简单说明: 一.模拟器配置设定 使用eclipse开发安卓,需要用到and ...
- System.Management命名空间
提供对大量管理信息和管理事件集合的访问,这些信息和事件是与根据 Windows 管理规范 (WMI) 结构对系统.设备和应用程序设置检测点有关的.应用程序和服务可以使用从 ManagementObje ...
- 使用top工具,找出消耗CPU 较多的进程
1.使用top工具,找出消耗CPU 较多的进程 [oracle@cuug ~]$ top top - 10:48:27 up 23:15, 4 users, load average: 1.09, ...
- MySQL 管理
MySQL 管理 启动及关闭 MySQL 服务器 首先,我们需要通过以下命令来检查MySQL服务器是否启动: ps -ef | grep mysqld 如果MySql已经启动,以上命令将输出mysql ...