sanjar-notes / swe-tools

Software engineering culture and tools
0 stars 0 forks source link

Bash doesn't have scopes, declare variables with `local`, imp for recursion #21

Open sanjarcode opened 8 months ago

sanjarcode commented 8 months ago
cleanEmptyFolders() {
    local _location="$1"
    local _hidden="${2}"

    local dir # if this is missed, all variables are shared across activation records (stack frames)
    for dir in $(childDirs "$_location"); do
        if [[ ! $_hidden && $(basename "$dir") == .* ]]; then
            continue # Ignoring hidden folders
        fi

        echo "Dir: $dir"

        echo "Exploring: $dir"
        cleanEmptyFolders "$dir" "$_hidden"

        if [[ ! $(ls -A "$dir") ]]; then
            echo "After check dir: $dir"
            rmdir "$dir"
            # echo "Empty folder '$dir' deleted."
        fi
    done
}
sanjarcode commented 8 months ago

I am thinking of switching to a sane lang for scripting - like Python (classic scripting) or JS (I'm fluent). But bash quirks would remain, even if not very relevant.