2016-03-08 103 views
1

我有一個名爲test.sh的腳本。通過shell函數調用函數參數:在shell腳本中調用

其中包含的功能,

APPFE_Routes() 
{ 


set filename [lindex $argv 0] 

expect -c ' 

spawn ssh [email protected] 

expect "Password: " 

send "[email protected]\r" 

expect "~]$" 

send "su\r" 

expect "Password: " 

send "500tps\r" 

expect "]#" 

send "put ifconfig grep -i $filename\r" 

expect -re "]#" 

send "exit\r" 

' 

} 

我打電話下來在test.sh腳本。

APPFE_Routes mkt mkt1 

我想這些mktmkt1變量傳遞expect腳本功能 通過上面的代碼。

我無法得到相同的結果。 其實我從一個文件中獲取這些變量到shell中,然後需要通過期望shell中的函數。 單獨的expect腳本不是必需的。 解決方案受到高度讚賞。

+2

'設置文件名[LINDEX $ ARGV 0]'是'Tcl'命令,它不會在工作shell腳本。 – Dinesh

+0

感謝Dinesh..Could請你建議我另一種方式 – user2160906

+0

外殼相當於是'文件名= $ 1'。 – chepner

回答

0

看起來你在寫TCL代碼。如果您安裝了tclsh,則可以將shebang設置爲#!/ usr/bin/tclsh,然後寫入所有您想要的TCL。

,如果你要堅持POSIX shell代碼,那麼set聲明或許應改爲:

filename="$1" 
0

我會建議你做

shell_func() { 
    expect -c ' 
     your expect code 
    ' "[email protected]" 
} 

但預計無法處理閱讀論點後-c

$ expect -c 'puts [join $argv \n]' foo bar baz 
can't read "argv": no such variable 
    while executing 
"join $argv \n" 
    invoked from within 
"puts [join $argv \n]" 
couldn't read file "foo": no such file or directory 

所以我建議你把t他的論點在對環境的外殼功能期望:

APPFE_Routes() 
{ 
    FILENAME="$1" expect -c ' 
     puts "hello $env(FILENAME)" 
    ' 
} 
APPFE_Routes world 
hello world 
0

嗨格倫·傑克曼和ALL,

感謝您的答覆。我在大家的幫助下完成了我的工作。 但我做了其他的事情來避免這個問題。 code APPFE_IP = $ {lines_ary [I]};

SIG_IP = $ {lines_ary第[i + 1]};

SIG_Gateway = $ {lines_ary [i + 2]};

APP_IP = $ {lines_ary第[i + 3]};

APP_GATEWAY = $ {lines_ary [i + 4]};

RDS1_IP = $ {lines_ary [i + 5]};

RDS1_APP = $ {lines_ary [i + 6]};

RDS1_GATEWAY = $ {lines_ary [i + 7]};

網絡

###### APPFE路線RDS1

{

在/ usr/bin中/期望< < EOF

產卵SSH管理@ $ APPFE_IP

期待「密碼:」

發送「Admin @ 1234 \ r」

期待-re 「〜] $」

發送 「蘇 - \ R」

期望 「密碼:」

發送 「500tps \ R」

期待-re「〜 ]#「

發送 」路由添加-host $ RDS1_APP GW $ APP_GATEWAY \ R「

期待-re 」〜]#「

發送 「退出\ R」

EOF

}

code