【问题标题】:Exclude two or more directories in bash for loop在 bash for 循环中排除两个或多个目录
【发布时间】:2020-09-02 10:59:52
【问题描述】:

我在该父目录中有一个名为 Stem 的父目录我有大约 20 个子目录和 20 个子目录名称以 _a 结尾,每个子目录有一个名为 violations.txt 的文件。在这 20 个子目录中,有两个子目录名为 Trans_ashift_a,我不需要对这两个子目录执行任何脚本操作。 我只需要在 18 个子目录上执行我的脚本。

我尝试了下面的代码,但没有得到确切的输出。

#!/bin/bash
echo "These warning reports are based on this run directory:" ; pwd
echo " " ;
File="violatons.txt"
for d in *_a;
do
if ( "$d"=="Trans_a"||"shift_a" ); then
True
else
    Statement 
fi
done

注意:在某些情况下,我们需要在脚本中排除的子目录可能会增加或减少,这取决于情况。

【问题讨论】:

标签: bash shell for-loop pattern-matching


【解决方案1】:
for f in !(Trans|shift)_a/violations.txt; do
    echo "$f"
    # do stuff
done

!(pattern-list) 匹配除给定模式之一之外的任何内容。

参考:https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Pattern-Matching

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    • 1970-01-01
    • 2021-09-22
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    相关资源
    最近更新 更多