如何处理在bash脚本中找到的结果? [英] How can I process the results of find in a bash script?

查看:142
本文介绍了如何处理在bash脚本中找到的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用 find 命令来存储一个文件名列表。



由于某些原因,数组无法在学校使用的bash中工作,我的程序在我自己的笔记本电脑上工作。

所以我想知道是否有另一种方法这样做,这是我有:

  array =(`find。-name* .txt`)#this将所有.txt文件存储到数组

然后我可以访问数组项并复制所有使用cat命令的文件。



有另外一种方法可以不使用数组吗?

解决方案

您可以使用类似的东西:

  find。 -name'* .txt'|而读线;做
echoProcessing file'$ line'
done

做一个副本:

  find。 -name'* .txt'|而读线; 
echo复制'$ line'到/ tmp
cp - $ line/ tmp
完成

HTH


I'm trying to use an array to store a list of file names using the find command.

For some reason the array fails to work in the bash used by the school, my program works on my own laptop though.

So I was wondering if there's another way to do it, this is what i have:

array = (`find . -name "*.txt"`)  #this will store all the .txt files into the array

Then I can access the array items and make a copies of all the files using the cat command.

Is there another way to do it without using an array?

解决方案

You could use something like that:

find . -name '*.txt' | while read line; do
    echo "Processing file '$line'"
done

E.g. make a copy:

find . -name '*.txt' | while read line; do
    echo "Copying '$line' to /tmp"
    cp -- "$line" /tmp
done

HTH

这篇关于如何处理在bash脚本中找到的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆