我们如何在shell脚本中的if循环内使用Expect命令

时间:2018-12-20 19:12:16

标签: shell expect

我有一组命令来安装我的产品。安装过程中会提示一些提示。问题是我们有两个版本的产品,并且每个版本的安装提示都不同。

我放置了一个if条件,并在其中添加了期望值。但是脚本不断失败。

   #!/bin/bash

#In this mode any command this script runs which returns a non-zero 
exitcode - an error in the world of shell - will cause your script to 
itself terminate immediately with an error.
set -e
set -x

#Below varibles is used to set flag  '0=Stage Pass' and '1=Stage Fail'
f="Fail" gotoStage="Pass" sdiclistage="Pass" 

# An error exit function
error_exit()
{
        echo "$1" 1>&2
        exit 1
}

# Using error_exit : Goto data insight folder
if cd /opt/datainsight/; then
        echo "inside /opt/datainsight directory."
else
{
        error_exit "Cannot change directory!  Aborting."
        gotoStage=$f;
}
fi

echo "Worker ip: $1"
echo "NMS ip: $2"
echo "DI tar name: $3"
echo "DI version: $4"
echo "executing sdi-cli cluster command"

if [[ "1.5.3" == "1.5"* ]];
then {

    echo "inside compa"
    /usr/bin/expect <<EOD
    set timeout 600

    spawn sdi-cli cluster provision -y 

    expect {
            timeout { send_user "\nFail to get password promt\n"; exit 
1 }
                   "password for pwd:"
            }

          send "pwd\r";

    expect {
            timeout { send_user "\n Fail to continue connecting \n"; 
exit 1}
            "Are you sure you want to continue connecting"
    }

    send "yes\r"


    expect {
            timeout { send_user "\n Fail to get second password promt 
\n"; exit 1}
            "password for pwd:"
    }

    send "pwd\r"



    expect {
            timeout { send_user "\n Error for completing provision\n"; 
exit 1}
             "Provisioning complete."
    }

    EOD
    echo "\nOutside sdi-cli cluster provision\n";
}
else 
{
    echo "In else"
    /usr/bin/expect <<EOD
    set timeout 600

    spawn sdi-cli cluster provision -y

      expect {
                timeout { send_user "\n Fail to continue connecting 
\n"; exit 1}
                "Are you sure you want to continue connecting"
        }

        send "yes\r"

    expect {
        timeout { send_user "\n Error for completing provision\n"; exit 
1}
        "Provisioning complete."
    }

    EOD
    echo "\nOutside sdi-cli cluster provision\n";

 }
 fi


if [ $? = 1 ]; then
{
        echo "\nError for executing sdi-cli script\n";
        sdiclistage=$f;
}
else
{
        echo "\n sdi-cli script executed successfully\n";
}
fi


if [ $gotoStage = "Pass" ] && [ $sdiclistage = "Pass" ] ; then
{
        echo "PASS" >> result.txt
        echo "\n All installation stages Passed\n ";
}
else
{
        echo "FAIL" >> result.txt
        echo "\n All/Some installation stages failed \n";
}
fi

sleep 300 #5min
ui=$(kubectl get pods |grep ui |awk '{print $1}') #get ui pod name
kubectl exec -it $ui cat GITINFO.json >>buildinfo.txt
echo "\n Build info file created successfully";

这总是失败

echo 'DI version: 1.5.3'
+ echo 'executing sdi-cli cluster command'
./di_install14_deepika.sh: line 134: warning: here-document at line 41 delimited by end-of-file (wanted `EOD')
./di_install14_deepika.sh: line 135: syntax error: unexpected end of file

0 个答案:

没有答案