If statement to check file transfer

Hello Guys,

I am trying scp few file within if statement, but getting error, can someone please help to understand, what mistake I am making ?

if [ [ scp $StatusFile $user@$log02:$scppath |grep '100%' > /dev/null ] && [ scp $StatusFile $user@$log01:$scppath |grep '100%' > /dev/null ] ] ;
then
        echo " Files transferred to Log servers successfully. "
else
        echo " One or more file transfer failed over network. "
fi

I

The best way to get help is if you include all your variables, since any of your variables:

$StatusFile $user@$log02:$scppath

... could be the potential source of any error.

Also, you did not include your error message. You said you were

But you did not post the exact error. Why not?

ERROR is something like below. And variables doesn't have any error as without if statement, commands runs properly.

line 25: [: missing `]'
grep: ]: No such file or directory
+ echo ' One or more file transfer failed over network. '
 One or more file transfer failed over network.

The if command needs a list of commands executed. man bash :

In the if list, you can use normal commands (e.g. scp ), the [[ compound command, or the test and [ builtin commands which in turn need "conditional expressions".

You don't deploy either. [ [ is not a valid construct, and your shell will have said so. Even if using the correct [[ , it would interpret scp as a string, not as a command to be executed.
Try (untested)

if scp ...  &&  scp ... ; then ... fi

I need to transfer both the files & if both gets copied correctly, it should return success message, I had tried to use suggested pattern " if scp ... && scp ... ; then ... fi " but even though both files gets copied, its executing else statement, that's why I tried to use [[

Below is what I tried, can you please take a look if I am missing something ? Must be something very silly which i am not paying attention to.

if scp $StatusFile $user@$log02:$scppath |grep '100%' > /dev/null  &  scp $StatusFile $user@$log01:$scppath |grep '100%' > /dev/null ;
then
        echo " Files transferred to Log servers successfully. "
else
        echo " One or more file transfer failed over network. "
fi

Did you use a single & or a double && ?

I have used single '&' because I want to transfer both files irrespective of result of first transfer.

The idea is to send both files & if any of the transfer fails, trigger else statement that one of the transfer is failed or else success message.

Do you know the difference between & and && ?

man bash :

Yes, I do know the difference but as I need to run both commands without the dependency of other to run or fail, I had to make some adjustment.

Nevermind, I have tried to scp only one file & have come to know that the issue was with 'grep' somehow, its not able to grep output of scp command, so I have to come up with the workaround, which I was trying to avoid for further obvious reasons of code.

So here is workaround, if anyone at all wants to know.

scp $StatusFile $user@$log02:$scppath
if [ $? -eq 0 ] ; then
scp $StatusFile $user@$log01:$scppath
if [ $? -eq 0 ] ; then
echo "$day Files transferred to log servers successfully." >> $scplog
else
echo "$day One or more file transfer failed over network." >> $scplog
fi
else
scp $StatusFile $user@$log01:$scppath
echo "$day One or more file transfer failed over network." >> $scplog
fi

How about

scp $StatusFile $user@$log02:$scppath
((RES+=$?))
scp $StatusFile $user@$log01:$scppath
((RES+=$?))
if [ "$RES" -ne 0 ]
  then echo "$day  $RES file transfer failed over network."
  else echo "$day Files transferred to log servers successfully." 
fi
1 Like

Hello Rudic,

This looks better & is working better for my code. Thanks for the help.