I'm trying to retrieve the paths to files and save those paths to a text file. Only the last iteration of the loop seems to be working. I've tried in both csh and in bash (making sure to change the shell environment as appropriate). Here is the bash script:
#!/bin/bash
#CSV file containing the list of strings to search for
strings_file="/project/crism/users/phillms1/Murchie2017MDAP/tyrrhena_tile_ids.csv"
#Output file where the full path of the file containing the strings will be appended
output_file="/project/crism/users/phillms1/Murchie2017MDAP/tyrrhena_tile_id_paths.txt"
#Read the strings from the CSV file
strings=$(cat "$strings_file")
#Search the current directory and all subdirectories for files containing the strings and "MRRSU" in their filename
for str in $strings; do
find . -type f -name "*$str*MRRSU*.IMG" -exec readlink -f {} ; >> "$output_file"
done
and here is the csh script:
#!/bin/csh
# CSV file containing the list of strings to search for
set strings_file = "/project/crism/users/phillms1/Murchie2017MDAP/tyrrhena_tile_ids.csv"
# Output file where the full path of the file containing the strings will be appended
set output_file = "/project/crism/users/phillms1/Murchie2017MDAP/tyrrhena_tile_id_paths.txt"
# Read the strings from the CSV file
set strings = `cat $strings_file`
# Search the current directory and all subdirectories for files containing the strings and "MRRSU" in their filename
foreach str ($strings)
find . -type f -name "*$str*MRRSU*.IMG" -exec readlink -f {} \; >> $output_file
#set files = `find . -type f -name "*$str*MRRSU*.IMG" -exec readlink -f {} \;`
#echo $files >> $output_file
end
#foreach str ($strings)
# set files = `find . -type f -name "*$str*MRRSU*.IMG"`
# if ($#files >= 0) then
# foreach file ($files)
# set full_path = `readlink -f $file`
# echo $full_path >> $output_file
# end
# endif
#end
You can see in the commented out lines I tried several different ways to do this.
I've tried just echo-ing the $str variable to make sure it contains what I thought, and it does. echo $str returns the values I expect. When I simply echo the find command, for example:
echo find . -type f -name "*$str*MRRSU*.IMG" -exec readlink -f {} \;
instead of returning the whole command it returns:
*MRRSU*.IMG" -exec readlink -f {} ;
and only the last iteration looks how it ought to look:
find . -type f -name "*0534*MRRSU*.IMG" -exec readlink -f {} ;
Does anyone know what's going on and how I can fix this?? Thank you!