首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Do和ssh连接所需的条件二进制运算符

Do和ssh连接所需的条件二进制运算符
EN

Stack Overflow用户
提问于 2022-05-06 15:49:07
回答 2查看 51关注 0票数 0

这是我的代码的一部分,我试图在通过ssh连接到该服务器的远程服务器中找到一个单词。

代码语言:javascript
复制
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];‘

任何建议

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-05-06 22:32:09

这一行应该从脚本的开头删除: exists=$(grep -c $word $filename)

并替换循环中对grep的调用。

票数 0
EN

Stack Overflow用户

发布于 2022-05-10 14:49:48

我所做的,在同事的帮助下解决了这个问题,避免在ssh会话中使用变量。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72144246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档