Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

for-loop

Thank you very much in advance for helping!

I have this code in 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.

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.

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
like image 896
RafaelGP Avatar asked Oct 26 '12 11:10

RafaelGP


People also ask

How do you exclude a directory?

Go to Start > Settings > Update & Security > Windows Security > Virus & threat protection. Under Virus & threat protection settings, select Manage settings, and then under Exclusions, select Add or remove exclusions. Select Add an exclusion, and then select from files, folders, file types, or process.

How do I loop all files in a directory?

To loop through a directory, and then print the name of the file, execute the following command: for FILE in *; do echo $FILE; done.

How do I loop through a specific directory in bash?

The syntax to loop through each file individually in a loop is: create a variable (f for file, for example). Then define the data set you want the variable to cycle through. In this case, cycle through all files in the current directory using the * wildcard character (the * wildcard matches everything).

How do I exclude a directory in Linux?

Method 1 : Using the option “-prune -o” We can exclude directories by using the help of “path“, “prune“, “o” and “print” switches with find command. The directory “bit” will be excluded from the find search!


1 Answers

You can use continue to skip one iteration of the loop:

for d in this_folder/*    
    do    
        plugin=$(basename $d)
        [[ $plugin =~ ^(global|plugins|css)$ ]] && continue
        echo $plugin'?'
        read $plugin
    done
like image 100
choroba Avatar answered Oct 12 '22 22:10

choroba