【问题标题】:How to check if a specific folder exists and no other file or folder in same path如何检查特定文件夹是否存在并且同一路径中没有其他文件或文件夹
【发布时间】:2022-10-06 08:00:10
【问题描述】:

如果文件夹存在于特定路径中,我们需要运行特定的测试集。

如果其他文件是与此文件夹一起存在的文件夹,我们需要运行不同的命令集。

If {only this folder present}

Run scriptA.sh

Elseif {this folder and other folders/files}

Run scriptB.sh

Fi

    标签: linux bash shell


    【解决方案1】:

    如果$folder 存在,此代码将运行scriptA.sh,但如果$other_folder 也存在,它将运行scriptB.sh

    check(){
        [[ -d $folder ]] && {
    
            [[ -d $other_folder ]] && {
                scriptB.sh
                return
            }
    
            scriptA.sh
        }
    }
    
    check
    

    【讨论】:

    • 这里的$other_folder 可以是$folder 以外的任何东西,不具体。那么我该怎么做呢?
    • 就这样设置吧other_folder=anything/not/specific
    【解决方案2】:

    我在父文件夹中,我创建了一个名为“blablabla”的文件夹,并在“blablabla”文件夹中执行find,这就是我得到的:

    Prompt> find ./blablabla
    ./blablabla
    

    我对另一个文件夹做同样的事情,它有一些内容,这就是我看到的:

    Prompt> find ./Other_folder
    ./Other_folder
    ./Other_folder/content_1
    ...
    

    换句话说(计算结果):

    没有内容的文件夹:

    Prompt> find ./blablabla | wc -l
    1
    

    包含内容的文件夹:

    Prompt> find ./Other_folder | wc -l
    <larger than 1>
    

    因此,通过检查 find ./&lt;your_folder&gt; | wc -l 的值是否为 1,您就有了一个很好的标准。

    【讨论】:

      【解决方案3】:
      if [ $( ls | wc -l ) -eq 1 && ( -d "$myfolder" ) ]; then 
      
       scriptA.sh 
      else 
       scriptB.sh
      fi
      

      这确认只有我的文件夹存在,没有别的。让我知道你们的想法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-28
        • 1970-01-01
        • 1970-01-01
        • 2018-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多