0

I'm trying to write a script that reads a file with filenames, and outputs whether or not those files were found in a directory.

Logically I'm thinking it goes like this:

$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

I've been having a hard time getting Bash to do this, any ideas?

sxflynn
  • 211
  • 1
  • 5
  • 17

2 Answers2

0

First, I would just assume that all the file-names are supplied on standard input. E.g., if the file names.txt contains the file-names and check.sh is the script, you can invoke it like

cat names.txt | ./script.sh

to obtain the desired behaviour (i.e., using the file-names from names.txt).

Second, inside script.sh you can loop as follows over all lines of the standard input

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

Edit: I adapted my answer to use standard input instead of command line arguments, due to the problem indicated by @rici.

chris
  • 4,988
  • 20
  • 36
  • 1
    If there are spaces in any filename, then that will break. The `"$@"` doesn't save it, because the words were already split by `$(cat filenames.txt)` (Ok, you didn't use `$()`, but it doesn't make any difference, and backticks are deprecated.) – rici Aug 02 '13 at 04:07
  • @rici You are right. It would probably be better to read the input line-by-line instead of via arguments. – chris Aug 02 '13 at 04:11
  • How would I use the vertical pipe inside of a script? I'm hoping to make this something that a person could just double click in Finder. – sxflynn Aug 02 '13 at 12:58
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 
  • How would I make `dirname` dynamic based on the location of the script, ie. script is located in `~/project/script.sh` so I want `dirname` to automatically be `~/project/`? Same with `filenames.txt` **edit:** Sorry nvm, I searched stackexchange and found the answer at `http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in` – sxflynn Aug 02 '13 at 12:10
  • I [found the answer](http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in) to my comment question about dynamic directories. – sxflynn Aug 02 '13 at 12:16