Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 多重';查找';对于一次观察者-仅执行第一个_Bash_Shell_Find_Watch - Fatal编程技术网

Bash 多重';查找';对于一次观察者-仅执行第一个

Bash 多重';查找';对于一次观察者-仅执行第一个,bash,shell,find,watch,Bash,Shell,Find,Watch,我正在尝试运行一个脚本,它将监视不同的文件夹和文件,如果它们发生更改,则执行一个命令。其中一个文件夹路径接受动态变量,该变量将根据在CLI中运行脚本时输入的内容进行更改,而另一个不会更改 但是,根据文件路径,需要采取不同的操作 #!/usr/bin/env bash THEME=$1 while true; do echo "inside find" find code/themes/$THEME/src -name '*.html' -o -name '*.ts' -o -na

我正在尝试运行一个脚本,它将监视不同的文件夹和文件,如果它们发生更改,则执行一个命令。其中一个文件夹路径接受动态变量,该变量将根据在CLI中运行脚本时输入的内容进行更改,而另一个不会更改

但是,根据文件路径,需要采取不同的操作

#!/usr/bin/env bash

THEME=$1

while true;
do
  echo "inside find"
    find code/themes/$THEME/src -name '*.html' -o -name '*.ts' -o -name '*.scss' | entr -d rsync -avh code/themes/$THEME/*  ./
    find code/base/src -name '*.html' -o -name '*.ts' -o -name '*.scss' | entr -d rsync -avh code/base/*  ./
done
当我运行脚本时发生的情况是,它只执行并将手表放在第一个查找位置(在上面粘贴的代码示例中,仅动态路径)

目前我的问题是:

  • 只调用第一个查找,并且只在代码/themes/$THEME/src路径上放置一个监视
  • 第二个发现永远不会执行
如何让两者同时运行?或者,我如何在一行中写出所有内容

此外,我以前从未编写过脚本或使用过bash,因此如果您对重构有任何建议或能够帮助解决我的问题,请温和地进行技术解释

我的主要目标是:

  • 我想设置一个监视,查看路径代码/themes/$THEME/src中的任何更改
  • 我想设置一个监视程序,监视路径代码/base/src中的任何更改
  • 我希望两个观察者同时运行

  • 如果您的任务只是确保两个(或1+个主题)目录的下游副本始终是最新的,则每隔n分钟同步一次的
    cron
    作业可能是最简单和最健壮的解决方案
    cron
    有每分钟一次的粒度,尽管每五分钟一次的粒度对于您的用例来说已经足够了
    rsync
    by design不会复制已经是最新的文件,因此在无操作的情况下,它应该可以相当快地执行

    * * * * * bin/rsyncthemes
    
    (这是每分钟一次;在Linux上,您可以在第一个字段中说每5分钟运行一次
    */5
    ,但这并不适用于所有
    cron
    方言。)。。。其中,
    rsyncthemes
    包含如下内容

    #!/bin/sh
    # cron jobs always start in your home directory,
    # but you want to put an absolute path here
    # to run this in the directory where you want rsync to copy these files
    cd somewhere
    
    for dir in code/themes/one \
        code/themes/other \
        code/base
    do
        rsync -avh "$dir"/*  ./
    done
    
    for dir in code/themes/one \
        code/themes/other \
        code/base
    do
        while true; do
            find "$dir"/src -name '*.html' -o -name '*.ts' -o -name '*.scss' |
            entr -d rsync -avh "$dir"/*  ./
        done & # <-- run each loop as a background job
    done
    
    显然,这需要标记为可执行(
    chmod a+x rsyncthemes
    ),并保存在我们在
    cron
    作业中指定的目录中(即主目录中的
    bin

    但是,如果您执意使用
    entr
    来避免不必要地运行
    rsync
    ,我猜您需要类似的

    #!/bin/sh
    # cron jobs always start in your home directory,
    # but you want to put an absolute path here
    # to run this in the directory where you want rsync to copy these files
    cd somewhere
    
    for dir in code/themes/one \
        code/themes/other \
        code/base
    do
        rsync -avh "$dir"/*  ./
    done
    
    for dir in code/themes/one \
        code/themes/other \
        code/base
    do
        while true; do
            find "$dir"/src -name '*.html' -o -name '*.ts' -o -name '*.scss' |
            entr -d rsync -avh "$dir"/*  ./
        done & # <-- run each loop as a background job
    done
    
    用于代码/主题/one中的目录\
    代码/主题/其他\
    代码/基
    做
    虽然真实;做
    查找“$dir”/src-name'*.html'-o-name'*.ts'-o-name'*.scss'|
    entr-d rsync-avh“$dir”/*/
    
    完成&#
    entr
    显然是您被引导到我不熟悉的
    entr
    的地方,但如果我读对了,它会运行第一个
    find
    entr
    管道,然后运行第二个
    find
    并坐在那里等待列出的任何文件发生更改,您应该在
    $THEME
    周围使用双引号,或者实际使用小写变量名(大写保留供系统使用)。即使您从未期望参数包含shell元字符,如果您尝试传入需要引号的参数,当您在不使用引号的情况下使用该参数时,错误消息也会令人困惑。进一步查看@tripleee是的,在您提到与我的另一个问题类似的问题后,我正在与entr玩。现在,使用基于路径的不同命令在多个查找中面临此问题。感谢您对语法约定的建议。我会实施的。也许你应该更详细地解释你希望发生什么。是否有理由按顺序执行这些操作?如果您有两个
    ,而
    循环彼此独立运行会怎么样?我用entr实现了解决方案,它正在工作!非常感谢。我将研究你提到的一些东西,并在我玩过之后应用它们。再次感谢。