在編寫shell腳本中,使用免交互方式spawn追蹤進程報錯

原腳本內容(編寫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
