Cobra Forum

Linux Community Discussions => Ubuntu, Linux and OS Chat => Topic started by: kalpana on Nov 07, 2023, 04:02 AM

Title: empty directory test via ssh
Post by: kalpana on Nov 07, 2023, 04:02 AM
Hello,
I'm trying to tweak a script for synchronizing files between servers.
At one point I need to check if a directory on server 2 is empty or has files to start or rsync.


I'm using the following code to test.



if [ "$(ssh root@meuIP ls -A /data/teste/transfer/ 2>/dev/null)" == "" ]; then echo "VAZIO"; else echo "CONTEM ARQUIVOS"; fi
Ele funciona como linha única direta no shell, mas quando coloco ele dentro do script ele não funciona.


CODE:
Declaration of variables bla bla



if [ "$(ssh $server2 ls -A $DIR_ 2>/dev/null)" == "" ]; then
rsync --dry-run --remove-source-files -avzrh "$SRV_PRD2":"$DIR_PRD2" "$DIR_PRD1"


EXIT:
root@server1:~# sh -x /data/teste/s5.sh
+ ssh root@ip ls -A /data/teste/transfer/
+ [ == ]
/data/teste/s5.sh: 35: [: unexpected operator
+ exit 0


Can you help me understand the error?


Thanks