首页 > 解决方案 > 获取具有特定名称的所有挂载目录

问题描述

我正在尝试接收具有特定名称的所有安装目录,以便将它们传送到另一个命令中。

里面/mnt/有 2 个文件夹,plotfield1plotfield2
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

标签: pathpipeconcatenationls

解决方案


这应该很简单,使用类似 glob 的表达式plotfield*并使用内置函数printf将输出放入标准输出,即从任何路径

printf '%s\n' /mnt/plotfield*
# or 
printf '%s\n' /mnt/plotfield?

此外,为了存储多行输出,请使用数组类型来保存您的值。因此,要将上述行存储到数组中,请使用mapfilereadarray

mapfile -t paths < <(printf '%s\n' /mnt/plotfield?)

并根据需要将数组扩展传递"${paths[@]}"给任何其他命令。


推荐阅读