2013-07-09 61 views
0

我只想編寫一個shell腳本,用於在運行我的命令時自動提供用戶名和密碼提示, 我試過這個,但沒有奏效。請幫助我解決任何問題。如何編寫自動交互式shell腳本?

#!/bin/bash 
#!/usr/bin/expect 

ls -ltr 

spawn sudo git pull 

expect "[sudo] password for sandeep:" 

send "sandeep123\n" 

expect "Username :" 

send "sandeep\n" 

expect "Password :" 

send "sandeep121\n" 

我得到以下輸出:

[email protected]:~$ ./checkout.sh 
total 64 
./checkout.sh: line 8: spawn: command not found 
couldn't read file "Username:": no such file or directory 
./checkout.sh: line 16: send: command not found 
couldn't read file "Password:": no such file or directory 
./checkout.sh: line 20: send: command not found 

回答

2

的預期的語法

expect -c 'spawn sudo git pull; expect assword; send "password\n"; interact' 

哪裏spawn sudo git pull;是你的命令,expect assword;等待assword出現,當它expect發送password

1

你有兩個#!行,但腳本只能由其中一個程序一次運行。由於大多數命令都是Expect命令,因此請刪除#!/bin/bash行,以便腳本可以由Expect運行。但是ls -ltr行不是Expect命令,會導致錯誤,但是您可以通過將其轉換爲puts [exec ls -ltr]來解決此問題。欲瞭解更多詳情,請參閱the Expect manual page