簡體   English   中英

Shell腳本失敗,出現預期的錯誤二進制運算符

[英]Shell script is failing with the error binary operator expected

我正在獲取cluster_network_operations.sh:行44:[:0:運行腳本時二進制運算符預期的錯誤

在我的腳本中,我調用ssh-copy-id命令,然后根據其輸出設置返回碼。

ssh-copy-id:輸出

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() {
    # 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 
}

在shell中返回代碼0表示成功或true,而<> 0表示錯誤

退出狀態用法示例

function contains {
    local string substring
    string=$1
    substring=$2
    [[ ${string#*$substring} != ${string} ]]
    return $?
}

if contains abc b; then echo ok; else echo KO; fi

if ! contains xyz c; then echo ok; else echo KO; fi

contains abc b && echo success
contains abc b || echo failed

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM