0

我正在尝试编写一个脚本来读取带有文件名的文件,并输出是否在目录中找到了这些文件。

从逻辑上讲,我认为它是这样的:

$filelist = prompt for file with filenames
$directory = prompt for directory path where find command is performed

new Array[] = read $filelist line by line

for i, i > numberoflines, i++
     if find Array[i] in $directory is false 
        echo "$i not found"
export to result.txt

我一直很难让 Bash 做到这一点,有什么想法吗?

4

2 回答 2

0

首先,我假设所有文件名都是在标准输入中提供的。例如,如果文件names.txt包含文件名并且check.sh是脚本,您可以像调用它一样调用它

cat names.txt | ./script.sh

以获得所需的行为(即,使用来自 的文件名names.txt)。

其次,在里面script.sh你可以循环如下标准输入的所有行

while read line
do
  ... # do your checks on $line here
done

编辑:由于@rici 指出的问题,我调整了我的答案以使用标准输入而不是命令行参数。

于 2013-08-02T03:23:58.153 回答
0
while read dirname
do
  echo $dirname >> result.txt
  while read filename
  do
    find $dirname -type f -name $filename >> result.txt
  done <filenames.txt
done <dirnames.txt 
于 2013-08-02T07:18:45.250 回答