bash 目录中每个文件夹的 for 循环,不包括其中一些

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13085929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:38:51  来源:igfitidea点击:

for-loop for every folder in a directory, excluding some of them

bashfor-loop

提问by RafaelGP

Thank you very much in advance for helping!

非常感谢您的帮助!

I have this code in bash:

我在 bash 中有这个代码:

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

Which works like a charm. For every folders inside 'this_folder', echo it as a question and store the input into a variable with the same name.

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

But now I'd like to exclude some folders, so for example, it will ask for every folder in that directory, ONLY if they are NOT any of the following folders: global, plugins, and css.

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

Any ideas how can I achieve this?

任何想法我怎样才能实现这一目标?

Thanks!

谢谢!

UPDATE:

更新:

This is how the final code looks like:

这是最终代码的样子:

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

回答by Thor

If you have a recent version of bash, you can use extended globs (shopt -s extglob):

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

for d in this_folder/!(global|plugins|css)/   
do    
    plugin=$(basename "$d")
    echo $plugin'?'
    read $plugin
done

回答by choroba

You can use continueto skip one iteration of the loop:

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

for d in this_folder/*    
    do    
        plugin=$(basename $d)
        [[ $plugin =~ ^(global|plugins|css)$ ]] && continue
        echo $plugin'?'
        read $plugin
    done

回答by askmish

If you meant to exclude only the directories named global, css, plugins. This might not be an elegant solution but will do what you want.

如果您打算仅排除名为 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

回答by execjosh

You could use findand awkto build the list of directories and then store the result in a variable. Something along the lines of this (untested):

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

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


Update 2019-05-16:

2019-05-16 更新:

##代码##