bash - 获取 bash 中 find 命令返回的数组的最后一项
问题描述
我有命令:
bigfiles=$(find "${path}/" -printf '%s %p\n'| sort -nr | head -2 | sed 's/^[^ ]* //')
现在我想得到最后一个项目
anotherfile=$bigfiles[1]
它似乎是空的
如何从find
命令中获取结果的第 n 个元素?
解决方案
通过使用mapfile
which is a bash4+ feature,可能会做你想做的事。
mapfile -t bigfiles < <(find "${path}/" -printf '%s %p\n'| sort -nr | head -2 | sed 's/^[^ ]* //')
anotherfile=${bigfiles[-1]}
echo "$anotherfile"
-1
是数组中的最后一个元素/项。如果您只对 find 的 output 的最后一项感兴趣,您可以将输出通过管道传输到
tail
或者head
取决于您尝试使用它执行的操作。
推荐阅读
- android - Xamarin Forms 使用 SwipView 从 ListView 中删除项目
- elasticsearch - 如何在布尔查询中只选择一个分支
- node.js - 如何在对象数组中查找对象?
- testing - 如何进行 TDD,例如 SQL
- javascript - 输入数据后,仍保留在输入字段 ReactJS
- java - Spring Kafka Consumer 打印不正确的标题
- reactjs - 导出 npm 包方法名的问题
- android - Android - 如何检查用户是否看过消息
- html - HTML 5 视频问题
- python - 如何使用线程python返回两个值