-4

I've a simple bash script. PFB :

CWD=/cygdrive/c/Users/abhisek.samanta/Desktop/New_folder
cd $CWD
for i in `cat req_files.txt`
do
echo "file_found : $i">a.txt
done

Note : req_files.txt is having a list of files which i need to find in this directory . If the files are found, it should list the name of the file which was found & print it in "a.txt".

The req_files.txt is having multiple files,but only the first filename is getting printed & not the rest of the files. Pl help.

Kusalananda
  • 333,661

5 Answers5

1

You appear to be overwriting the contents of a.txt each time round the loop.

echo "file_found : $i">a.txt

Should replace > with >>.

Or place it after the done to avoid opening and closing it each time. Adding this to the other answer to give

done < req_files.txt > a.txt

I've left it as just open not append as it doesn't need to save anything from what's shown so far.

Guy
  • 894
0

If you want to read from file which lines contain spaces/tabs/etc you have to use while loop instead of for

CWD=/cygdrive/c/Users/abhisek.samanta/Desktop/New_folder
cd $CWD
while read i
do
echo "file_found : $i">a.txt
done < req_files.txt
user1700494
  • 2,202
0

Your code does not contain lines to search in the given directory and compare the files inside that directory with the given files (req_files.txt). All you do is keep on overwriting (since you are using > operator, use >> to append instead) the file a.txt with contents of req_files.txt one by one and when the script finishes running, you are left with only the last item written to a.txt.

As per my understanding, your goal is:

Search and list all the files inside that given path. Then compare that list with the files listed in req_files.txt. And finally, list the matching ones in the output file a.txt. In that case, below code does it for you:

cwd=/cygdrive/c/Users/abhisek.samanta/Desktop/New_folder
cd $cwd #change dir to the required path
>a.txt  # create output file a.txt
for file in `ls`:
do
        grep "`echo "$file"`" req_files.txt >> a.txt
done

In for loop, you are now listing the files using ls command. This script will create a file a.txt inside the given path which contains the files from req_files.txt that are present in that given path.

Vinod
  • 131
0

Redirection operator needs to be replaced with concatenation

echo "file_found : $i" >> a.txt
chaos
  • 48,171
0

You are truncating (emptying) the output file in each iteration of your loop.

You are also looping over the output of cat, which is dangerous as the shell will split the output on spaces, tabs and newlines (by default) and also perform filename globbing on the result. If a line says *, it will be replaced by all the filenames in the current directory.

Instead:

cd /cygdrive/c/Users/abhisek.samanta/Desktop/New_folder || exit 1

while IFS= read -r line; do
    printf 'File found: %s\n' "$line"
done <req_files.txt >a.txt

This would read from the file req_files.txt and write the output of the loop into a.txt. The file a.txt would be truncated before output is written to it. If it does not exist, it will be created.

Would you want to append the output to an already existing file, use >> rather than >.

See also:

Kusalananda
  • 333,661