0

I have script to open a text file which has a list of 1650 site locations. I am attempting to loop thru this list print two separate lists of files 1) which are found and 2) which are missing. The list of site locations is a single column. The problem I am running in to is the script is not reading the input file and looping thru it. The script is just looking for the the single file "instead of reading and looping thru each line of this file".

#!/bin/bash
file=Sites-1.txt
do
if "$file" ;
then
  echo "$file found" >> found.tmp
else
  echo "$file has not been found" >> missing.tmp
fi
done

input example from Sites-1.txt for files looking for

01-033-0043-SO2-2014.dat.out
01-033-0044-SO2-2014.dat.out
01-033-1002-SO2-2014.dat.out
01-071-0020-SO2-2014.dat.out
01-071-0026-SO2-2014.dat.out

Expected output files composition

found.tmp
01-033-0043-SO2-2014.dat.out found
01-033-0044-SO2-2014.dat.out found 
01-071-0026-SO2-2014.dat.out found
missing.tmp
01-033-1002-SO2-2014.dat.out has not been found
01-071-0020-SO2-2014.dat.out has not been found
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • 1
    Please add sample input and your desired output for that sample input to your question (no comment here). – Cyrus Feb 03 '22 at 21:20
  • is the single file in the current working directory, or is it anywhere on the computer, or is it anywhere in a subdirectory of the current working directory? – Evan Carroll Feb 03 '22 at 21:50
  • The single file is in the current working directory as the the files i'm searching for – stormctr2 Feb 03 '22 at 21:57

2 Answers2

0

Your script is not checking anything useful. if "$file" doesn't check if the file exists, you need to use -e "$file" for that. You would still only check if the list exists, though.

You're probably looking for a script like this:

#!/bin/bash
filelist=sites-1.txt

for file in $(cat $filelist); do if [ -e "$file" ] then echo "$file found" >> found.tmp else echo "$file has not been found" >> missing.tmp fi done

allo
  • 946
  • 1
  • 7
  • 14
0

If Sites-1.txt contains one file name per line, and none of the names of the files in the current directory contain newline characters, you could do:

comm -23 <(sort -u Sites-1.txt) <(ls -A)

To report the unique lines that are in Sites-1.txt and not in the output of ls -A.

Or with zsh:

expected=( ${(f)"$(<Sites-1.txt)"} )
  actual=( *(ND) )
 missing=( ${expected:|actual} )

print -rC1 -- $missing