首页 > 解决方案 > 如何读取终端中的最后一行并使用 ruby​​ 将其保存在 var 中

问题描述

我在终端有这个

   >>>>>>>>>>>>>>>>>>>Connecting bucket<<<<<<<<<<<<<<<<<<<<<<
  #<Aws::S3::Client:0x00000005219698>
  >>>>>>>>>>>>>>>>>>>Established Bucket Connetion <<<<<<<<<<<<<<<<<<<<<<
  No objects.

我想取最后一行,在这种情况下(无对象),并将其保存在 var 中。有人知道ruby中的代码如何?提前致谢

标签: rubylinux

解决方案


捕获命令的输出command_name,通过管道tail -n 1仅打印最后一行,如下所示:

my_var = `command_name | tail -n 1`

也可以看看:

tail文档

tail(1) - Linux manual page

       -n, --lines=[+]NUM
              output the last NUM lines, instead of the last 10; or use
              -n +NUM to output starting with line NUM

推荐阅读