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)
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
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