0

I want to add directories to a variable as such:

   WATCHED_DIR="\
      someDir_1/* \
      someDir_2/* \
      someDir_3/* \

Each directory has a lot of sub directories and most of the time its ok. But now I want to make a variable with a list of sub directories I want to exclude

Such as:

EXCLUDED_DIRS:"\
        someDir_2/docs/ \
        someDir_3/docs/ "

I am unsure how to do the exclusion part.

Update: To clarify what I am trying to accomplish might be better understood with the code snippet below

# Add directories to watch
WATCH_FILES="\
.github/workflows/ci-tests/Examples_tests \
Examples/MCU1/BLE \
Examples/MCU1/Bootloader \
Libraries/libs.mk \
Libraries/Cordio/* \
Libraries/CMSIS/Device/MCU1 \
Libraries/PeriphDrivers/ \\
Libraries/BlePhy/ \
Libraries/Boards/"
#remove unwanted sub-directories
IGNORED_DIRS="\ 
            Libraries/Cordio/docs \
            Libraries/Cordio/controller"

for ignored_dir in $IGNORED_DIRS; do
  # some how remove from watch files
done

When I print out WATCH_FILES I get all the sub-directories inside of Cordio which I think is good because I can go delete it. If I remove the * from Cordio I do not get the sub directories in my string. The desired end result is to end up with WATCH_FILES that does not have the ignored sub directories.

With the clean list of WATCH Files I then do a git diff and see if any of the changed files live in this WATCH Files directories

3
  • 1
    1. Is WATCHED_DIR supposed to contain directory names, file names, or both? ditto for EXCLUDED_DIRS. 2. You should probably use array variables rather than whitespace separated strings. 3. This looks like an XY Problem to me - What are you actually trying to do? How are you going to use the variables when they have correct values? Use them with a find command? Commented Feb 14, 2023 at 0:43
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Feb 14, 2023 at 7:28
  • @cas I updated the question. Commented Feb 14, 2023 at 15:30

1 Answer 1

1

You've mentioned bash so I'm going to suggest using an array. Furthermore, so that we can easily identify directories to remove, I'm going to use an associative array to hold the directories to be ignored. You can crash the results() array into a newline-separated list later if that's really what you need.

Run this code in a directory where the watched and ignored lists match reality. This will allow globs such as Libraries/Cordio/* to expand to a real list of directories. Otherwise it's impossible to get the list of directories that match a wildcard - which in turn means it's not possible to identify the set of directories that is not to be excluded.

#!/bin/bash
shopt=$(shopt -p nullglob)    # Save nullglob setting
shopt -s nullglob             # Enable nullglob

# Directories to watch
declare -a watched=(
    .github/workflows/ci-tests/Examples_tests
    Examples/MCU1/BLE
    Examples/MCU1/Bootloader
    Libraries/libs.mk
    Libraries/Cordio/*
    Libraries/CMSIS/Device/MCU1
    Libraries/PeriphDrivers
    Libraries/BlePhy
    Libraries/Boards
)

# Directories to ignore
declare -a ignored=(
    Libraries/Cordio/docs
    Libraries/Cordio/controller
)

# Reset shopt nullglob
$shopt                        # Revert nullglob

# Work starts here

# Create an associative array for ignored()
declare -A aignored=()
for ignore in "${ignored[@]}"; do aignored["${ignore%/}"]=1; done

# Copy the working directory list, skipping entries in ignored()
declare -a results=()
for watch in "${watched[@]}"; do test -v aignored["${watch%/}"] || results+=("${watch%/}"); done

# Print the resulting array, separated by newlines, in sorted order
printf '%s\n' "${results[@]}" | sort

Output from your example. I've assumed that Libraries/Cordio actually contains four directories: adirectory, docs, controller, and somedirectorytokeep:

.github/workflows/ci-tests/Examples_tests
Examples/MCU1/BLE
Examples/MCU1/Bootloader
Libraries/BlePhy
Libraries/Boards
Libraries/CMSIS/Device/MCU1
Libraries/Cordio/adirectory
Libraries/Cordio/somedirectorytokeep
Libraries/PeriphDrivers
Libraries/libs.mk

This example (in particular the final printf | sort) assumes no directory name contains a newline. If that's possible you can use NULL-terminated strings or process the list of results in some other way.

1
  • flawless!! thanks I appreciate it Commented Feb 23, 2023 at 2:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.