0

我正在尝试使用 cli 命令捕获多个远程设备的输出,然后使用 grep 717GE 并打印到多个主机的屏幕。正如您在下面看到的,它从 $hosts 捕获 IP 并将其传递给 $outip,而不是在命令行中使用相同命令时显示的内容。我认为该变量将捕获从给定命令返回的数据。有人可以帮助我并帮助我了解我做错了什么吗?我真的对学习很感兴趣,所以请不要发表刻薄的评论。如果可能的话。

for host in ${hosts[@]}; do
  seven=( $(cli $hosts show gpon ont summary -n --max=3 --host ) )
  outip=( $(grep 717GE $seven) )
  echo $outip
done

输出:

+ for host in '${hosts[@]}'
+ seven=($(cli $hosts show gpon ont summary -n --max=3 --host ))
++ cli 10.100.112.2 show gpon ont summary -n --max=3 --host
+ outip=($(grep 717GE $seven))
++ grep 717GE 10.100.112.2 grep: 10.100.112.2: No such file or directory
4

1 回答 1

1

不要使用var=( .. ),除非你想创建一个数组,并使用 bash here-string grep something <<< "$var"(相当于echo "$var" | grep something)来搜索匹配的行(否则你说它$var包含要搜索的文件名列表,以及一些要设置的选项grep) :

for host in ${hosts[@]}; do
  # Assign as variable rather than array, and use $host instead of hosts
  seven=$(cli $host show gpon ont summary -n --max=3 --host )
  # Grep with "$seven" as text input and not as a list of filenames
  outip=$(grep 717GE <<< "$seven")
  echo "$outip"
done
于 2013-06-27T16:26:46.797 回答