Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash Shell脚本失败,错误为预期的二进制运算符_Bash_Shell_Scripting - Fatal编程技术网

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

Bash Shell脚本失败,错误为预期的二进制运算符,bash,shell,scripting,Bash,Shell,Scripting,我在运行脚本时遇到群集\u网络\u操作。sh:line 44:[:0:应为二进制运算符错误 在我的脚本中,我调用ssh copy id命令,然后根据其输出设置返回代码 ssh拷贝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 machi

我在运行脚本时遇到群集\u网络\u操作。sh:line 44:[:0:应为二进制运算符错误

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

ssh拷贝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() {
    # 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

Remove
[
]
调用
contains
函数$?不是ssh复制id输出,而是返回代码,包含0“any string”对IMHO没有什么意义