bashautomatorpositional-parameter

Run a bash script within Automator using positional parameters


This is my first time using Automator and it seems like a pretty nifty tool. I am running into an issue however at the very end of the automation. The goal of my automator workflow is to specify a path, then create a directory Highschool1, for example, within that path.
From here, I want it to search for any files within the specified path that include "Highschool1" in the name, and move them into the new Highschool1 directory. Here is how my bash script works within terminal:

mkdir "/Users/tommy/Desktop/TestShow1/WG/Highschool1"

This creates the directory as intended. Then:

find /Users/tommy/Desktop/TestShow1/WG -name 'Highschool1' -prune -o -type f -name '*Highschool1*' -exec mv -- {} /Users/tommy/Desktop/TestShow1/WG/Highschool1 \;

This finds the files I want while excluding the new Highschool1 directory, and then moves the found files into that Highschool1 directory. It is all working as intended at the base.

It's when I try to apply this script within my automation using positional parameters that it stops working.

Automation Part 1

Automation Part 2

-I stuff a variable called "HighschoolName" with the input "Highschool1"
-Then I stuff a variable called "pathA" with the input, which is the path I chose: "/Users/tommy/Desktop/TestShow1/WG"
-Then I call back my HighschoolName variable and begin with the positional parameters.

This is the final script I used:

mkdir "$1/$2"

find /$1 -name '$2' -prune -o -type f -name '*$2*' -exec mv -- {} /$1/$2 \;

This creates the directory Highschool1 where I want it, but fails to move any files into it. It gives me no error message either. It simply acts as if the script was run successfully. Does anyone have any idea what the problem could be?


Solution

  • Read about quoting

    In:

    find /$1 -name '$2' -prune -o -type f -name '*$2*' -exec mv -- {} /$1/$2 \;
    

    '$2' will not interpolate the variables, you need to use "$2" (same for '*$2*')