在编写shell脚本中,使用免交互方式spawn追踪进程报错

expect  spawn报错invalid command name_大括号

原脚本内容(编写redis一键安装部署脚本) 

/usr/bin/expect <<EOF
cd /opt/redis-5.0.7/utils
spawn ./install_server.sh
expect "instance: [6379]" {send "\r"}
expect "[/etc/redis/6379.conf]" {send "\r"}
expect "[/var/log/redis_6379.log]" {send "\r"}
expect "[/var/lib/redis/6379]" {send "\r"}
expect "path []" {send "/usr/local/redis/bin/redis-server"}
expect eof
EOF

查找原因,并尝试分解各个步骤

最后发现,是expect里面的内容中的括号【】有问题,不能使用[],将其去除即可执行了,或者使用 -ex {},把语句放到大括号中即可

send中也是如此,也不可用[],需要转义或者使用send -- {}格式,将send的内容放到大括号中转义 

修改后的脚本

yum -y install expect &> /dev/null
/usr/bin/expect <<EOF
cd /opt/redis-5.0.7/utils
spawn /opt/redis-5.0.7/utils/install_server.sh
expect "instance:" {send "\r"}
expect "/etc/redis/6379.conf" {send "\r"}
expect "/var/log/redis_6379.log" {send "\r"}
expect "/var/lib/redis/6379" {send "\r"}
expect "path" {send "/usr/local/redis/bin/redis-server\r"}
expect "ENTER" {send "\r"}
expect eof
EOF

expect  spawn报错invalid command name_redis_02