安裝expect 執行以下命令
sudo apt-get install tcl tk expect
在使用expect 寫自動交互功能時 遇到問題
automate_expect.sh
#! /usr/bin/expect
spawn ./interactive.sh
expect "Enter number:"
send "1\n"
expect eof
然後在命令行輸入
sh automate_expect.sh
既然出現 spawn not found 錯誤
一直出現這個錯誤,基本上都是出學者
原來linux 執行sh腳本有兩種方式,一種時將腳本作爲sh 的命令行參數,另一種時將腳本作爲具有執行權限的可執行腳本
將腳本作爲sh 的命令行參數運行的方式如下
sh script.sh 或 sh /home/path/script.sh
將腳本作爲具有執行權限的可執行腳本運行方式如下
先然腳本文件具有可執行權限,執行下面代碼
chmod a+x script.sh 或chmod 755 script.sh
執行腳本
./script.sh 或/home/path/script.sh
而作爲sh 命令行參數來運行。那麼腳本的#! 的一行就會失效
所以纔會出現spawn not found 錯誤,所有上面的automate_expect.sh 腳本必須用以前命令運行
./automate_expect.sh