How to generate directory list and perform actions for each list item?

Asked

Viewed 167 times

1

I am trying to build a Shell Script that stores directories exists at the root of /Volumes and perform an iteration on these, ignoring only the directory: "Preboot" and "Macintosh H"

  • 1

    "Interaction" or "iteration"? If "interaction" could explain what kind of "interaction" you want?

  • for d in /Volumes/*; do if [ "$d" != "/Volumes/Preboot" -a "$d" != "/Volumes/Macintosh H" ]; then itera "$d"; fi; done

1 answer

0


Quick response

for d in /Volumes/*; do
  if [ "$d" != "/Volumes/Preboot" -a "$d" != "/Volumes/Macintosh H" ]; then
    itera "$d";
  fi;
done

Explanation

for will iterate over a given set of words. In this case, we are passing /Volumes/*, which will be expanded glob and therefore the iteration argument of for will all content not hidden from /Volumes.

In condition, I am ensuring that the variable $d not even the briefcase Preboot inside /Volumes nor Macintosh H inside /Volumes also. I don’t recall any operation of relevance to test. Could have used case/esac also to ignore these options.

The test cmd1 -a cmd2, specifically -a, is the operation and of command test. Therefore, the example operation of the beginning of this paragraph will be true if cmd1 and cmd2 are true.

Disclaimer

As I am without computer, I was left with doubt when the expansion glob generates options with spaces in the middle, but I believe that even if this generates a bug in the code it is almost trivial to fix it.

  • 1

    Thanks for the help, final result: #! /bin/bash for d in /Volumes/; do if [ "$d" != " /Volumes/Preboot" -a "$d" != " /Volumes/Macintosh HD" ]; then cd "$d" && find . -type f ( -iname "._" -o -name ". Ds_store" ) -exec rm "{}" ; echo "Removing files ". _*" and ". Ds_store" from: $d"; fi; done .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.