【问题标题】:How to get two outputs from a bash find exec command如何从 bash find exec 命令中获取两个输出
【发布时间】:2021-09-24 18:22:53
【问题描述】:

我主持了一个照片库。您可以运行一个命令来自动添加内容。看起来像这样;

php artisan lychee:sync /path/to/import --album_id="album ID"

这个命令需要两个变量;

  1. 导入路径
  2. 专辑编号

我正在对一个目录运行 Find 以返回过去 5 天内添加的所有子目录;

find /mnt/Pictures -mtime -5 -mindepth 1
/mnt/Pictures/Paris Trip
/mnt/Pictures/Spain
/mnt/Pictures/America

现在,我可以使用 Find 中的 -exec 函数调用脚本来添加此内容,但 {} 的值是要导入的路径。我需要导入路径和目录名称;比如

php artisan lychee:sync /mnt/Pictures/Spain --album_id="Spain"

这是我目前为止的地方;

find /mnt/Pictures -mtime -5 -mindepth 1 -exec php artisan lychee:sync {} \;

有什么想法吗?

【问题讨论】:

  • this 会回答您的问题吗?使用该方法,您可以例如使用awk -F/ '{print $NF}' 对抗第二个{} ...

标签: linux bash find exec


【解决方案1】:

只需添加一个sh 调用即可:

find /mnt/Pictures -mtime -5 -mindepth 1 -exec \
    sh -c 'php artisan lychee:sync "$1" --album_id="$(basename "$1")"' _ {} \;

【讨论】:

    【解决方案2】:

    一种选择是使用 bash 循环:

    find /mnt/Pictures -mtime -5 -mindepth 1 -print0 | \
       while IFS= read -r -d '' pth; do  \
          php artisan lychee:sync "${pth}" --albumId="${pth##*/}"; \
       done;
    

    【讨论】:

    • 哎呀。谢谢修复。
    猜你喜欢
    • 1970-01-01
    • 2013-12-07
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 2014-06-16
    • 2012-09-26
    • 2017-04-09
    相关资源
    最近更新 更多