12

非常感谢您的帮助!

我在 bash 中有这段代码:

for d in this_folder/*    
    do    
        plugin=$(basename $d)
        echo $plugin'?'
        read $plugin
    done

这就像一个魅力。对于“this_folder”中的每个文件夹,将其作为问题回显并将输入存储到具有相同名称的变量中。

但是现在我想排除一些文件夹,例如,它会询问该目录中的每个文件夹,前提是它们不是以下文件夹中的任何一个:全局、插件和 css。

任何想法我怎样才能做到这一点?

谢谢!

更新:

这是最终代码的样子:

base="coordfinder|editor_and_options|global|gyro|movecamera|orientation|sa"

> vt_conf.sh
echo "# ========== Base"     >> vt_conf.sh
for d in $orig_include/@($base)
do
    plugin=$(basename $d)
    echo "$plugin=y"         >> vt_conf.sh
done
echo ''                      >> vt_conf.sh
echo "# ========== Optional" >> vt_conf.sh
for d in $orig_include/!($base)
do
    plugin=$(basename $d)
    echo "$plugin=n"         >> vt_conf.sh
done
4

6 回答 6

16

如果您有最新版本的 bash,则可以使用扩展的 glob ( shopt -s extglob):

shopt -s extglob

for d in this_folder/!(global|plugins|css)/   
do    
    plugin=$(basename "$d")
    echo $plugin'?'
    read $plugin
done
于 2012-10-26T12:59:04.420 回答
10

您可以使用continue跳过循环的一次迭代:

for d in this_folder/*    
    do    
        plugin=$(basename $d)
        [[ $plugin =~ ^(global|plugins|css)$ ]] && continue
        echo $plugin'?'
        read $plugin
    done
于 2012-10-26T12:21:00.970 回答
1

如果您打算仅排除名为 global、css、plugins 的目录。这可能不是一个优雅的解决方案,但会做你想做的事。

for d in this_folder/*    
do  
    flag=1
    #scan through the path if it contains that string
    for i in "/css/" "/plugins/" "/global/"
    do

    if [[ $( echo "$d"|grep "$i" ) && $? -eq 0 ]]
    then
      flag=0;break;
    fi
    done

    #Only if the directory path does NOT contain those strings proceed
    if [[ $flag -eq 0 ]]
    then
    plugin=$(basename $d)
    echo $plugin'?'
    read $plugin
    fi


done
于 2012-10-26T12:53:50.820 回答
0

您可以使用findandawk构建目录列表,然后将结果存储在变量中。与此类似的东西(未经测试):

dirs=$(find this_folder -maxdepth 1 -type d -printf "%f\n" | awk '!match($0,/^(global|plugins|css)$/)')
for d in $dirs; do
    # ...
done

2019 年 5 月 16 日更新:

while read -r d; do
    # ...
done < <(gfind  -maxdepth 1 -type d -printf "%f\n" | awk '!match($0,/^(global|plugins|css)$/)')
于 2012-10-26T11:47:57.717 回答
0

虽然How to exclude some files from the loop in shell script被标记为此 Q/A 的欺骗并关闭,但 Q 专门询问了如何在 BASH 脚本中排除文件,这正是我所需要的(在脚本中检查本地 URL 中链接片段(# 之后的部分)的有效性。这是我的解决方案。

    for FILE in *
    do
        ## https://linuxize.com/post/how-to-check-if-string-contains-substring-in-bash/
        if [[ "$FILE" == *"cnp_"* ]]
        then
            echo 'cnp_* file found; skipping'
            continue
        fi
        ## rest of script
    done

输出:

    cnp_* file found; skipping

    ----------------------------------------
    FILE: 1 | NAME: linkchecker-test_file1.html
    PATH: /mnt/Vancouver/domains/buriedtruth.com/linkchecker-tests/linkchecker-test_file1.html

     RAW LINE: #bookmark1
    FULL PATH: /mnt/Vancouver/domains/buriedtruth.com/linkchecker-tests/linkchecker-test_file1.html#bookmark1
         LINK: /mnt/Vancouver/domains/buriedtruth.com/linkchecker-tests/linkchecker-test_file1.html
     FRAGMENT: bookmark1
       STATUS: OK
    ...

我的测试目录包含 3 个文件,其中一个我想排除(旧网站的网页抓取:包含大量已弃用链接片段的索引)。

[victoria@victoria link_fragment_tester]$ tree
.
├── cnp_members-index.html
├── linkchecker-test_file1.html -> /mnt/Vancouver/domains/buriedtruth.com/linkchecker-tests/linkchecker-test_file1.html
└── linkchecker-test_file2.html -> /mnt/Vancouver/domains/buriedtruth.com/linkchecker-tests/linkchecker-test_file2.html

0 directories, 3 files
[victoria@victoria link_fragment_tester]$ 
于 2020-10-21T19:34:30.877 回答
0

拆分路径,检查要忽略的每个文件夹名称。
如果您以后想增加要忽略的文件夹的数量,这种忽略文件夹的方式很方便。(它们在这里存储在被忽略的文件夹中)。
在代码中添加了注释。
这在 Ubuntu 20.04.2 中对我来说很好用

#!/bin/bash
shopt -s globstar #necessary for search ./**/*
ignoredfolders=("folder1name" "folder2name")

for i in ./**/*
do
    #pattern for splitting forward slashes into empty spaces
    ARRAY=(${i//\//" "} ); 
    for k in "${ignoredfolders[@]}"; do
        #does path (splitted in ARRAY) contain a foldername ($k) to be ignored? 
        if [[ " ${ARRAY[@]} " =~  "$k"  ]]; then
            #this skips this loop and the outer loop
            continue 2
        fi
    done
    # all ignored folders are ignored, you code sits here...
    #code ...
    #code ...

done
于 2021-05-06T17:57:02.587 回答