Linux&shell之显示数据

简介:

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

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

在脚本中重定向输入

复制代码
#!/bin/bash
# redirecting file input

exec 0<testfile
count=1

while read line
do
  echo "Line #$count:$line"
  count=$[ $count + 1 ]
done
复制代码

(从文件testfile中读取数据)

创建自己的重定向

复制代码
#!/bin/bash
# using an alternative file descriptor

exec 3>testout

echo "This should display on the monitor"
echo "This should be stored in the file">&3
echo "Then this should be back on the monitor"
复制代码

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

重定向文件描述符

复制代码
#!/bin/bash
# storing STDOUT,then coming back to it

exec 3>&1
exec 1>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 1>&3

echo "Now things should be back to normal"
复制代码

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


创建输入文件描述符

复制代码
#!/bin/bash
# redirecting input file descriptors

exec 6<&0

exec 0<testfile

count=1
while read line
do
  echo "Line #$count:$line"
  count=$[ $count + 1 ]
done
exec 0<&6
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 3>testfile

echo "This is a test line of data">&3

exec 3>&-

echo "This won't work">&3
复制代码

(最后一句将不会写入文件,并且报错,因为文件描述符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 3>$tempfile

echo "This script writes to temp file $tempfile"

echo "This is the first line">&3
echo "This is the second line">&3
echo "This is the last line">&3
exec 3>&-

echo "Done creating temp file.The contents are:"
cat $tempfile
rm -f $tempfile 2>/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 3>$tempfile

echo "This script writes to temp file $tempfile"

echo "This is the first line">&3
echo "This is the second line">&3
echo "This is the last line">&3
exec 3>&-

echo "Done creating temp file.The contents are:"
cat $tempfile
rm -f $tempfile 2>/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 7>$tempfile1
exec 8>$tempfile2

echo "Sending data to directory $tempdir"
echo "This is a test line of data for file1">&7
echo "This is a test line of data for file2">&8
复制代码

 



记录消息
有时很有必要将输出同时发送到监视器和日志文件。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
复制代码

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



本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/3237722.html,如需转载请自行联系原作者

相关文章
|
6天前
|
Shell Linux
Linux shell编程学习笔记30:打造彩色的选项菜单
Linux shell编程学习笔记30:打造彩色的选项菜单
|
6天前
|
Shell Linux
Linux shell编程学习笔记82:w命令——一览无余
Linux shell编程学习笔记82:w命令——一览无余
|
11天前
|
人工智能 监控 Shell
常用的 55 个 Linux Shell 脚本(包括基础案例、文件操作、实用工具、图形化、sed、gawk)
这篇文章提供了55个常用的Linux Shell脚本实例,涵盖基础案例、文件操作、实用工具、图形化界面及sed、gawk的使用。
27 2
|
2月前
|
缓存 NoSQL Linux
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
|
1月前
|
Shell Linux 开发工具
linux shell 脚本调试技巧
【9月更文挑战第3天】在Linux中调试shell脚本可采用多种技巧:使用`-x`选项显示每行命令及变量扩展情况;通过`read`或`trap`设置断点;利用`echo`检查变量值,`set`显示所有变量;检查退出状态码 `$?` 进行错误处理;使用`bashdb`等调试工具实现更复杂调试功能。
|
2月前
|
Linux 开发工具
linux下使用gcp拷贝数据的时候显示进度条
linux下使用gcp拷贝数据的时候显示进度条
15 2
|
2月前
|
监控 网络协议 Linux
在Linux中,如何实时抓取并显示当前系统中tcp 80 端口的网络数据信息?
在Linux中,如何实时抓取并显示当前系统中tcp 80 端口的网络数据信息?
|
2月前
|
存储 监控 网络协议
在Linux中,如何使用 tcpdump 监听主机为 192.168.1.1,tcp 端⼝为 80 的数据,并将将输出结果保存输出到tcpdump.log?
在Linux中,如何使用 tcpdump 监听主机为 192.168.1.1,tcp 端⼝为 80 的数据,并将将输出结果保存输出到tcpdump.log?
|
2月前
|
监控 Shell Linux
在Linux中,如何使用shell脚本检测磁盘使用率?
在Linux中,如何使用shell脚本检测磁盘使用率?
|
2月前
|
Shell Linux 开发工具
在Linux中,如何编写shell脚本将当前目录下大于10K的文件转移到/tmp目录下?
在Linux中,如何编写shell脚本将当前目录下大于10K的文件转移到/tmp目录下?
下一篇
无影云桌面