这是我的代码的一部分,我试图在通过ssh连接到该服务器的远程服务器中找到一个单词。
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这只适用于Do命令,但如果我添加if [$exist -gt 0];,则会出现错误
-c:第4行:条件二进制操作符,预期bash:-c:第4行:;' bash: -c: line 4:附近的语法错误,如果[ -gt 0];‘
任何建议
发布于 2022-05-06 22:32:09
这一行应该从脚本的开头删除: exists=$(grep -c $word $filename)
并替换循环中对grep的调用。
发布于 2022-05-10 14:49:48
我所做的,在同事的帮助下解决了这个问题,避免在ssh会话中使用变量。
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;";
donehttps://stackoverflow.com/questions/72144246
复制相似问题