path - 获取具有特定名称的所有挂载目录
问题描述
我正在尝试接收具有特定名称的所有安装目录,以便将它们传送到另一个命令中。
里面/mnt/
有 2 个文件夹,plotfield1
并plotfield2
ls|grep plotfield
正确返回这两个文件夹名称:
但我正在寻找的是完整路径:
/mnt/plotfield1
/mnt/plotfield2
目前,为了测试,我使用这个:
cd /mnt/
test=$(ls|grep plotfield|cat <(echo -n '/mnt/') -)
echo $test
有没有更好的方法来获取文件的完整路径?目标是这样的伪代码:
ls|grep plotfield|concat|xargs chia plots add -d
解决方案
这应该很简单,使用类似 glob 的表达式plotfield*
并使用内置函数printf
将输出放入标准输出,即从任何路径
printf '%s\n' /mnt/plotfield*
# or
printf '%s\n' /mnt/plotfield?
此外,为了存储多行输出,请使用数组类型来保存您的值。因此,要将上述行存储到数组中,请使用mapfile
或readarray
mapfile -t paths < <(printf '%s\n' /mnt/plotfield?)
并根据需要将数组扩展传递"${paths[@]}"
给任何其他命令。
推荐阅读
- c - 写C函数产生不可读的文件
- c# - 无法在 C++/WinRT Xaml Islands 项目中使用来自 C# WinRT 组件的 Xaml 组件
- javascript - 在 Typescript 中过滤整数数组 - Angular 2
- c# - Xamarin Forms 中的全局资源
- python - 将时间转换为数据时间格式,保持“AM/PM”
- html - CSS Dropcap Bootstrap 5
- python - 在 PySpark 中使用列条件替换空值
- sql - 计算具有相同 id 的列的值的存储过程
- built-in - 如何覆盖 Autodesk 内置查看器扩展
- python - 有没有办法识别列表中的 IP 地址或 MAC 地址?