I am trying to find two files in a if condition to do a task
Please find below the script that i have built
while read line
do
filename = $line
if [[ -f $filename+".dat"] && [ -f $filename+".trg"]]; then
cp /tmp/$filename.dat /sftp/$filename.dat
else
cat $filename "no trg file associated" >> /sftp/sftp_log.$datetime
fi
done < /tmp/sftp.uniq
Brackets don't work that way. You can't mix [[ ]] and [ ] inside each other.
Also, string concatenation doesn't work that way. $var+".ext" will give you "filename+.ext"
Also, equals don't work that way. Don't put any extra spaces between the variable, the equals, and the contents.
Also, brackets do need a space between them and the things inside them.
Also, cat doesn't work that way. There's no file named "no trg file associated" so it will throw an error. I think you wanted echo.
Also, there's no reason to reopen /sftp/sftp_log.$datetime umpteen times when you can just redirect the loop's output once.
Also, you should use the ${filename} syntax instead of $filename, since the shell will always know when the var begins and ends.
while read line
do
filename="$line"
if [[ -f "${filename}.dat" ]] && [[ -f "${filename}.trg" ]]; then
cp /tmp/$filename.dat /sftp/$filename.dat
else
echo "${filename}: no trg file associated"
fi
done < /tmp/sftp.uniq >> /sftp/sftp_log.$datetime