need help for finding files

Hi,

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

I am getting the below error

conditional binary operator expected
syntax error near `-f'
`if [[ -f $filename+".dat"] && [ -f $filename+".trg"]]; then'

Please suggest, how can i correct this

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

A space is missing before ]]

...trg" ]]...

Thank you for your replies guys...

I tried the code in red with the space

That did work a bit, it went into the while loop and printing the error message multiple times

Error message as below

filename: command not found

seems like the value is not getting into filename variable

filename = "$line"

Please suggest

---------- Post updated at 01:02 PM ---------- Previous update was at 11:42 AM ----------

Got it, i found that there is no reason to assign the line value to filename, hence used the line itself and it is working fine.

Thanks for all your help

That's not precisely my code you're using, you added things here and there, like spaces.