Bash和Expect - '正在打断.sh脚本

时间:2017-04-12 15:12:06

标签: bash sh expect

我正在尝试使用Expect包创建自动sh脚本,以便自动安装我们的产品。正如我计划的那样,在安装过程中会遇到问题。但是当脚本出现问题时:Enter PushVOD database name (leave blank for default 'pushvod'): 它每次关闭安装。 正如我认为引号中的问题 - '有问题:for default 'pushvod'

但我不知道如何解决它。我试着在期望期望中写出\',也尝试用*写出期望,但它没有帮助。写下关于这个问题的任何内容。

#!/bin/bash
#!/bin/expect
#   
apt-get --purge remove python-pip
apt-get --purge remove python-setuptools
echo " " > /etc/apt/sources.list
echo "deb http://httpredir.debian.org/debian jessie main" >> /etc/apt/sources.list | echo "deb-src http://httpredir.debian.org/debian jessie main" >> /etc/apt/sources.list | echo "deb http://httpredir.debian.org/debian jessie-updates main" >> /etc/apt/sources.list | echo "deb-src http://httpredir.debian.org/debian jessie-updates main" >> /etc/apt/sources.list | echo "deb http://security.debian.org/ jessie/updates main" >> /etc/apt/sources.list | echo "deb-src http://security.debian.org/ jessie/updates main" >> /etc/apt/sources.list
cd /var/tmp
/usr/bin/apt-get update
apt-get --assume-yes install expect
tar xvfz XXXXXXXXXXX.tar.gz
mv /var/tmp/XXXXXXXX.deb /var/tmp/pushvod-dependencies
echo "deb XXXXXXXXXX amd64/" > /etc/apt/sources.list
aptitude update
expect -c 'spawn aptitude install dpkg-dev;
expect "Accept this solution?" {send "n\r"};
expect "Accept this solution?" {send "Y\r"};
expect "Do you want to continue?" {send "Y\r"};
expect "To continue, enter \"Yes\"\; to abort, enter \"No\":" {send "Yes\r"};
expect "To continue, enter \"Yes\"\; to abort, enter \"No\":" {send "Yes\r"}; 
interact;'
cd pushvod-dependencies
dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz
echo "deb file:/var/tmp/pushvod-dependencies ./" >> /etc/apt/sources.list
aptitude update
expect -c 'spawn aptitude install pushvod;
expect "Accept this solution?" {send "n\r"};
expect "Accept this solution?" {send "Y\r"};
expect "Do you want to continue?" {send "Y\r"};
expect "To continue, enter \"Yes\"\; to abort, enter \"No\":" {send "Yes\r"};
expect "To continue, enter \"Yes\"\; to abort, enter \"No\":" {send "Yes\r"};
expect "Enter PushVOD database name*:" {send "pushvod\r"};
interact;'

1 个答案:

答案 0 :(得分:0)

  1. 你不需要把整个字符串放在期望中。默认情况下,期望expect命令 使用glob模式。所以Enter PushVOD database name*:没问题。 *将表示“此处有一些文字”。
  2. 为引用expect内的字符串更好地使用{}。它类似于贝壳''
  3. 您可以使用shell功能“Here Documents”来填充文件和 正在运行期望脚本。
  4. 在此命令tar xvfz XXXXXXXXXXX.tar.gz中,最后一个选项必须为f。在这个选项之后 必须遵循档案名称。
  5. 在此命令中echo "deb XXXXXXXXXX amd64/" > /etc/apt/sources.list您确定要吗? 只用这个字符串重写/etc/apt/sources.list?如果不是,您应该使用>>
  6. 有代码:

    #!/bin/bash
    
    apt-get --purge remove python-pip
    apt-get --purge remove python-setuptools
    cat << END > /etc/apt/sources.list
    deb     http://httpredir.debian.org/debian jessie main
    deb-src http://httpredir.debian.org/debian jessie main
    deb     http://httpredir.debian.org/debian jessie-updates main
    deb-src http://httpredir.debian.org/debian jessie-updates main
    deb h   ttp://security.debian.org/ jessie/updates main
    deb-src http://security.debian.org/ jessie/updates main
    END
    cd /var/tmp
    /usr/bin/apt-get update
    apt-get --assume-yes install expect
    tar xvzf XXXXXXXXXXX.tar.gz
    mv /var/tmp/XXXXXXXX.deb /var/tmp/pushvod-dependencies
    echo "deb XXXXXXXXXX amd64/" > /etc/apt/sources.list
    aptitude update
    expect << END 
        spawn aptitude install dpkg-dev
        expect {Accept this solution?} {send "n\r"}
        expect {Accept this solution?} {send "Y\r"}
        expect {Do you want to continue?} {send "Y\r"}
        expect {To continue, enter "Yes"; to abort, enter "No":} {send "Yes\r"}
        expect {To continue, enter "Yes"; to abort, enter "No":} {send "Yes\r"}
        interact
    END
    cd pushvod-dependencies
    dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz
    echo "deb file:/var/tmp/pushvod-dependencies ./" >> /etc/apt/sources.list
    aptitude update
    expect << END
        spawn aptitude install pushvod
        expect {Accept this solution?} {send "n\r"}
        expect {Accept this solution?} {send "Y\r"}
        expect {Do you want to continue?} {send "Y\r"}
        expect {To continue, enter "Yes"; to abort, enter "No":} {send "Yes\r"}
        expect {To continue, enter "Yes"; to abort, enter "No":} {send "Yes\r"}
        expect {Enter PushVOD database name*:} {send "pushvod\r"}
        interact;
    END