'conditional binary operator expected with Do and ssh connection
This is part of my code that I working to find a word in a remote server connecting via ssh to that server
filename=test.repo
word=fail
exists=$(grep -c $word $filename)
file_server=$1
for i in $(cat $file_server)
do echo ''; echo $1 ;
ssh $i "printf '\e[33m'; hostname; printf '\e[0m\n]' ;
cd /etc/yum.repos.d/;
grep -c $word $filename;
echo $exists;
if [[ $exists -gt 0 ]];
then printf 'Keyword found, cleanup starts \n';
else printf ''$word', was not found, nothing to do here with '$filename'. \n';
fi"
done
This works with just the Do command, but if I add the if [[$exist -gt 0]]; , this got an error
bash: -c: line 4: conditional binary operator expected
bash: -c: line 4: syntax error near ;' bash: -c: line 4:
if [[ -gt 0]]; '
any suggestion
Solution 1:[1]
This line should be cut from the beginning of the script: exists=$(grep -c $word $filename)
And replace the call to grep inside the loop.
Solution 2:[2]
What I do, with some help of a colleague to fix this avoid using the variable on the ssh session.
file_server=$1
filename=testfile.repo
keyword=testword
if [[ -z $1 ]]
then
printf "\t\e[0;31m==================== ERROR ======================\n"
printf "\tServer list not found\e[0;0m\n"
exit 1
fi
for i in $(cat $file_server)
do echo ''; echo $1 ;
ssh $i "printf '\e[33m'; hostname; printf '\e[0m\n';
cd /etc/yum.repos.d/;
if grep -q $keyword $filename;
then
printf '\e[33m$keyword found, cleanup starts\e[0m\n';
sed -i.BAK '/^failovermethod=/d' $filename
grep failovermethod $filename;
printf '\e[33mBackup file created with the $keyword with this name $filename.BAK\e[0m\n';
grep 'failovermethod' pgdg-redhat-all-test.repo.BAK;
else
printf '\e[33m${keyword^^}\e[32m, was not found, nothing to do here. \e[0m\n';
fi;";
done
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | anonymous_wannabe |
Solution 2 | JohnWolf |