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
  • 66,199
  • 35
  • 114
  • 250
stormctr2
  • 65
  • 8
  • 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
  • 916
  • 1
  • 7
  • 13
  • Allo, I tried this code adjustment and what I am finding is even if a file does exist the script output is stating the file does not exist which makes no sense. – stormctr2 Feb 03 '22 at 22:38
  • It probably needs to be `if test -e "$file"` or `if [[ -e "$file" ]]` and really would be better written as a `while` loop than a `for` loop - see for example [How to loop over the lines of a file?](https://unix.stackexchange.com/a/580545/65304) – steeldriver Feb 04 '22 at 00:00
  • I don't think it has to be a while loop, but you're right with the missing test. But it should probably be a single `[` – allo Feb 04 '22 at 15:36
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
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501