for loop for each folder in the directory, excluding some of them

Thank you for your help!

I have this code in bash:

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

It works like a charm. For each folder inside the "this_folder" echo as a question and store the input to a variable with the same name.

But now I would like to exclude some folders, so, for example, it will query each folder in this directory ONLY if they are NOT one of the following folders: global, plugins and css.

Any ideas how I can achieve this?

Thanks!

UPDATE:

Here's what the latest 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 
+7
source share
4 answers

If you have the latest version of bash, you can use extended globes ( shopt -s extglob ):

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

You can use continue to skip one loop iteration:

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

If you wanted to exclude only directories named global, css, plugins. It may not be an elegant solution, but it will do what you want.

 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 
+1
source

You can use find and awk to build a list of directories, and then save the result in a variable. Something like this (not verified):

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

Update 2019-05-16:

 while read -rd; do # ... done < <(gfind -maxdepth 1 -type d -printf "%f\n" | awk '!match($0,/^(global|plugins|css)$/)') 
-1
source

All Articles