linux - shell脚本如何减少中间文件的使用
高洛峰
高洛峰 2017-04-17 14:51:15
0
2
418

刚刚开始接触shell,用以处理工作中一些小问题
今天写了一个脚本,代码如下:

#!/bin/bash
zcat $1 | awk -F\" '{print $8}'|sort|uniq -c|sort -r|awk '{print $2,$1}' >ips.txt

cat ips.txt | while read line
do
A=(`echo $line`)
B=`nslookup ${A[0]}`
if [[ "$B" =~ "baidu" ]]
then
    echo ${A[@]} "真"
else
    echo ${A[@]} "假"
fi
done >iprst.log
rm ips.txt

脚本运行过程需要生成一个ips.txt文件,最后又删除。这种只在运行过程中需要用到的文件,我称之为中间文件。
在使用shell解决其他问题时也经常遇到这种情况,有时候甚至需要用到2~3个这种中间文件。
请问,有什么方法可以减少这类文件的使用,让代码更简洁呢?
嵌入式文档如何应用到以上脚本中?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
伊谢尔伦

The pipeline itself can solve this problem. Taking the above code as an example, it can be written like this:

zcat  | awk -F\" '{print }'|sort|uniq -c|sort -r|awk '{print ,}' | 
while read line
do
A=(`echo $line`)
B=`nslookup ${A[0]}`
if [[ "$B" =~ "baidu" ]]
then
    echo ${A[@]} "真"
else
    echo ${A[@]} "假"
fi
done >iprst.log
洪涛

You can use FIFO if necessary. But you really can’t say that there are many temporary files on it.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!