shell交互式脚本,实现对a.sh交互内容实现自动化操作

脚本如下
#!/usr/bin/expect  

# 设置超时时间(秒),防止脚本无限等待  
set timeout 120  

# 启动 aa.sh 脚本  
spawn ./setup.sh
sleep 2
# 预期交互模式  
expect {
    "Please select an option*" {
        send "2\r"
        exp_continue
    }
   sleep 2
    "Please select an option:" {
        send "2\r"
        exp_continue
    }
    "set system env*" {
        send "yes\r"
        exp_continue
    }
        "*model*" {
        send "2\r"
        exp_continue
    }
        "*buildtype*" {
        send "1\r"
        exp_continue
    }
        "*march*" {
        send "1\r"
        exp_continue
    }
           "Press enter to continue*" {
        send "\r"
        exp_continue
    }
    # 可以添加一个通用的匹配来结束交互  
    default {
        expect eof # 等待脚本结束  
    }
}

# 脚本结束,可以添加一些清理工作或输出  
puts "Interaction with aa.sh completed."

 

posted @ 2024-09-30 15:00  白灰  阅读(83)  评论(0)    收藏  举报