bash - 获取函数内的最后一个 echo 语句并将变量放入 bash
问题描述
我有以下 bash 脚本,在该语句中,它有几个命令和几个 echo 命令。是否可以将唯一的最后一个 echo 语句作为 return 语句并忽略所有其他 echo 命令?
#!/bin/bash
function statement(){
echo "This is the first statement"
echo "This is the second statement"
# do something else
echo "The result is ok" # I want to display only this in my output.
}
last_echo=$(echo $(statement))
echo "${last_echo}"
这将输出每个语句:
This is the first statement This is the second statement The result is ok
预期输出:
The result is ok
解决方案
用于tail
仅提取最后一行。
result=$(statement | tail -n 1)
推荐阅读
- android - 修复底部导航栏在其片段包含协调器布局时不断扩展
- unity3d - 似乎没有为 VisualStudio 设置 RoslynTargetsPath
- javascript - 有没有办法使用防止默认并仍然接收发布请求?
- linux - 如何传递命令行参数
- c# - 不使用 Mailkit 或 .net smtp 客户端发送的电子邮件
- tkinter - tkinter 在 python 3.6 上的 windows 模态窗口阻止 deiconify 功能
- apache-kafka - Kafka 连接错误处理和改进的日志记录
- python - 离线 Python 语音识别
- javascript - 在组件中使用 dispatch() 时未调用 Redux 减速器
- html - 无法存储 html 内容并再次显示以进行编辑