ruby - 如何读取终端中的最后一行并使用 ruby 将其保存在 var 中
问题描述
我在终端有这个
>>>>>>>>>>>>>>>>>>>Connecting bucket<<<<<<<<<<<<<<<<<<<<<<
#<Aws::S3::Client:0x00000005219698>
>>>>>>>>>>>>>>>>>>>Established Bucket Connetion <<<<<<<<<<<<<<<<<<<<<<
No objects.
我想取最后一行,在这种情况下(无对象),并将其保存在 var 中。有人知道ruby中的代码如何?提前致谢
解决方案
捕获命令的输出command_name
,通过管道tail -n 1
仅打印最后一行,如下所示:
my_var = `command_name | tail -n 1`
也可以看看:
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
推荐阅读
- angular - Microsoft.AspNetCore.SpaServices:错误:给定的 rootPath 中的错误未定义不是程序的文件
- php - 在 PHP 上存储来自 CLI 命令输出的值 (Windows)
- reactjs - 在 NextJS 中实现本地喜欢/不喜欢的功能并使其在客户端路由之间保持不变
- c++ - 函数标题中的 Qt/Qt Creator 和箭头运算符 (->) - 是否有效?
- spring-boot - Apache Camel CXF 设置传输属性
- amazon-web-services - 使用 Visual Studio IDE 部署 sam 时出现 Python UTF-8 错误
- kubernetes - kubernetes 批量重启所有命名空间 pod 以使新的配置映射配置工作
- angular - Javascript 可以代替 TypeScript 用于 Angular 开发吗?
- php - 使用 laravel 命令生成多个文件
- vim - 将 Vim 的语言特定设置放在哪里?