empty directory test via ssh

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
awerik
Level 1
Level 1
Posts: 2
Joined: Tue Mar 21, 2023 1:09 pm

empty directory test via ssh

Post by awerik »

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
Last edited by LockBot on Thu Sep 21, 2023 10:00 pm, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
rene
Level 20
Level 20
Posts: 12212
Joined: Sun Mar 27, 2016 6:58 pm

Re: empty directory test via ssh

Post by rene »

awerik wrote: Tue Mar 21, 2023 1:15 pm

Code: Select all

/data/teste/s5.sh: 35: [: unexpected operator
== is a valid operator for test/[ in bash but not in Debian/Ubuntu/Mint sh, which is dash rather than bash. I.e., either use single = for POSIX compliance or run the script explicitly through bash (although I feel it better to then also use the bash [[ variant rather than POSIX [).

Please by the way use [code] tags for posting code.
awerik
Level 1
Level 1
Posts: 2
Joined: Tue Mar 21, 2023 1:09 pm

Re: empty directory test via ssh

Post by awerik »

tks guys.

i could this way:

Code: Select all

if [ "$(ssh root@IP ls -A $DIR_SYNC_QLD 2>/dev/null)" = "" ];
then 
	RESP="1";
else 
	RESP="2"; 
	fi
	
Locked

Return to “Scripts & Bash”