2017-02-13 35 views
0

我想寫一個腳本,將sftp文件到我的亞馬遜開發者的帳戶。下面是該腳本:爲什麼我會在我的期望腳本中看到單引號和意外行爲?

#!/usr/bin/expect -- 
# 
# 
set timeout -1 
#log_user 1 

if {[llength $argv] < 3 } { 
     puts "usage: sftp-to-amazon.exp <APPCODE> <APPNAME> <SFTP_USER>" 
     puts "" 
     puts "This script will sftp binary files to the amazon sftp server for the given APPCODE." 
     puts "and APPNAME. APPNAME is like Dragnet_AMZ_1951_V4." 
     puts "You can get the APPCODE from the Amazon Developers Console." 
     exit 1 
} 

set appcode [lindex $argv 0] 
set appname [lindex $argv 1] 
set sftp_user [lindex $argv 2] 


puts "App code is $appcode app name is $appname sftp_user is $sftp_user" 

stty -echo 
send_user "Enter password for $sftp_user: " 
expect_user -re "(.*)\n" 
set sftp_pass $expect_out(1,string) 

set sftp_host 'dar.amazon-digital-ftp.com' 

puts "/usr/bin/sftp -o 'StrictHostKeyChecking no' ${sftp_user}@${sftp_host}" 
if [ catch "spawn /usr/bin/sftp -o 'StrictHostKeyChecking no' [email protected]$sftp_host" reason ] { 
     puts "failed to spawn line 115 /usr/bin/sftp [email protected]$sftp_host : $reason\n" 
     set success 0 
     exit 1 
} 
expect -re "[email protected]$sftp_host's password: $" { 
     puts "Sending password" 
     send "$sftp_pass\r" 
} 

puts "Script complete." 

當我運行該腳本,我得到這樣的輸出:

$ ./sftp-to-amazon.exp M1S3R61WOY9B0 ONETWO VM3H65THINGBATFA7 
App code is M1S3R61WOY9B0 app name is ONETWO sftp_user is VM3H65THINGBATFA7 
Enter password for VM3H65THINGBATFA7: /usr/bin/sftp -o 'StrictHostKeyChecking no' [email protected]'dar.amazon-digital-ftp.com' 
spawn /usr/bin/sftp -o 'StrictHostKeyChecking no' [email protected]'dar.amazon-digital-ftp.com' 
command-line: line 0: Bad configuration option: 'stricthostkeychecking 
Couldn't read packet: Connection reset by peer 
Script complete. 

當我運行...

/usr/bin/sftp -o 'StrictHostKeyChecking no' [email protected]'dar.amazon-digital-ftp.com' 

...自身從命令行工作正常。

回答

1

因爲單引號在期望中沒有特殊含義(Tcl)。

if [ catch "spawn /usr/bin/sftp -o 'StrictHostKeyChecking no' [email protected]$sftp_host" reason ] { 
#         ^..................... ..^ 
#         two separate words with literal quote chars 

Tcl等價於shell的單引號是花括號。您需要

if [ catch "spawn /usr/bin/sftp -o {StrictHostKeyChecking no} [email protected]$sftp_host" reason ] { 
#         ^........................^ 
#         one word 
+0

謝謝!這有助於消除'chars。我需要制定我懷疑的正則表達式模式。 –

相關問題