通过shell函数调用函数参数:在shell脚本中调用

通过shell函数调用函数参数:在shell脚本中调用

问题描述:

我有一个名为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

看起来你在写TCL代码。如果您安装了tclsh,则可以将shebang设置为#!/ usr/bin/tclsh,然后写入所有您想要的TCL。

,如果你要坚持POSIX shell代码,那么set声明或许应改为:

filename="$1" 

我会建议你做

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 

嗨格伦·杰克曼和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