【问题标题】:getting all mounting directories with a specific name获取具有特定名称的所有挂载目录
【发布时间】:2021-09-13 09:59:11
【问题描述】:

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

/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

【问题讨论】:

    标签: path pipe concatenation ls


    【解决方案1】:

    这应该很简单,使用像plotfield* 这样的全局表达式并使用内置的printf 将输出放入标准输出,即从任何路径

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

    对于存储多行输出,使用数组类型来保存您的值。所以要将上述行存储到数组中,请使用mapfilereadarray

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

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

    【讨论】:

      猜你喜欢
      • 2013-04-29
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      • 2020-12-17
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多