Shell脚本失败,错误二进制运算符是预期的

时间:2017-05-19 06:50:12

标签: bash shell scripting

我在运行我的脚本时遇到 cluster_network_operations.sh:第44行:[:0:二元运算符预期错误

在我的脚本中,我正在调用 ssh-copy-id 命令,然后根据其输出设置返回码。

ssh-copy-id:output

WARNING: All keys were skipped because they already exist on the remote system.

Successful:

Number of key(s) added: 1

Now try logging into the machine, with "ssh 'bhupesh@10.236.61.133'", and check in:

2. All keys were skipped because they already exist on the remote system

我使用了一个函数contains来检查字符串中的子字符串。下面的代码段,

contains() {
    # check if the substring exits in a string. 
    string="$1"
    substring="$2"
    if test "${string#*$substring}" != "$string"
    then
        return 0    # $substring is in $string
    else
        return 1    # $substring is not in $string
    fi
}

enable_auth(){
#   Enabling the authentication by sending the pub_key of all the nodes in
#   the cluster to the new gateway.
#   Currently, its a one way secure authentication.
    #pdsh -S -g all -N cat /root/.ssh/id_rsa.pub | ssh "$2"@"$1" 'cat >> .ssh/authorized_keys'
    ssh-copy-id -i ~/.ssh/id_rsa.pub "$2"@"$1"
    ret=$?
    #sub_str = "Now try logging into the machine, with"
    if [ contains "$ret" "Now try logging into the machine, with" ]
    then
        echo "Successfully Configured"
        ret=0
    elif [ contains "$ret" "All keys were skipped because they already exist on the remote system" ]
    then
        echo "System "$1" is already configured for the user: $2"
        ret=0
    else
        ret=1
    fi
    echo 0 
}

1 个答案:

答案 0 :(得分:0)

shell中的

返回代码0表示成功或为真,<> 0表示错误

退出状态的使用示例

function fncInitialize() {
        var objDate = $(this).find("input[data-datepicker='True']");
        objDate.datepicker();
        fncBindControls();
    }

    function fncBindControls() { 
    }
相关问题