S
S
Sergei Nazarenko2021-05-06 21:09:18
bash
Sergei Nazarenko, 2021-05-06 21:09:18

How to implement list condition in bash?

There is a working script in Bash that iterates over directories in a loop and if the desired one does not exist inside these directories, then it creates it. But there is also a list of directories to be ignored. Everything works, but does not suit the long condition or. I tried to substitute in the cycle this way and that, but something does not work. Because it only compares the first value and ends up listing all the subdirectories along with the ones to be ignored.

Working version of the script.

function iterate() {
    for entry in [email protected]; do
        if ; then
            if [ ! -d “$entry/content” ]; then
                echo $entry
                 mkdir -p $entry/content/folderA
            fi
         fi
done
}

iterate /folder/*
iterate /folder/*/*


That's actually the question of how to get rid of the long if or or or .... and substitute the value from the array into the condition, or even better from the blacklist file?

Tried doing something like this
BLIST = "
*folder1*
*folder2*
*folder3*
*folder4*
"

function iterate() {
    for entry in [email protected]; do
        for BL in ${BLIST}; do
        if ; then
            if [ ! -d “$entry/content” ]; then
                echo $entry
                 mkdir -p $entry/content/folderA
            fi
         fi
        done
done
}

iterate /folder/*
iterate /folder/*/*


Displays the entire list along with prohibited
PS: I can’t figure out how to do it right

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2021-05-06
@nazares

just use break/continue

#!/bin/bash
LIST=( 1 2 3 4 5 6 7 8 9 10 )
BLIST=( 1 4 7 )

for entry in ${LIST[*]}; do
    for bl_dir in ${BLIST[*]}; do  && continue 2; done
    echo $entry
done

bash.exe a.sh
2
3
5
6
8
9
10

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question