我希望从shell中获取命令到变量的输出

时间:2016-04-07 09:27:12

标签: linux shell expect ping spawn

这是我在shell中的代码。

#!/usr/bin/expect -f
array set routers { 0 "192.168.1.1" 1 "127.0.0.1"}
array set password { 0 "56343451" 1 "123456789" }
set size 2
set COUNT 10
for {set i 0} {$i < $size} {incr i 1} {
  set count "[ping -c $COUNT $routers($i) | grep 'received' | awk -F',' '{ print \$2 }' | awk '{ print \$1 }']"
  if { $count <= 3 } {
    spawn ssh root@$routers($i)  
    expect { "no)?" { send "yes\r"; exp_continue }
    password: { send $password($i)"\r"; exp_continue}
    interact
    }
  }
}

我希望将ping的输出接收到$ count。 当我尝试运行时,会发生“”:没有这样的文件或目录“

1 个答案:

答案 0 :(得分:1)

你必须支持awk表达式。

% set COUNT 3
3
% set router 135.xxx.xx.xxx
% exec /bin/ping -c $COUNT $router  | grep "received" | awk -F "," {{print $2}} | awk {{print $1}}
3
%