Would like help on shell scripting and renaming files.

My issue is I have too many similar rules in Hazel using the in-built file matching and the rename with pattern > replace text is cool but I wish I could use regex patterns in there versus explicitly stating every character that I want to be substituted/removed.
I'd like to learn how to create a basic shell script to check for a match of two specific file names, example here "eXPRS Plan of Care - Services Delivered Form" and "eXPRS Plan of Care - Mileage Driven Form" if no match happens the script exits and does not do anything. If a match happens act on the file(s) that match and strip desired characters with regex pattern using command-line tools. In this case, I just want to remove anything other than [a-zA-Z] (including spaces and hyphen) and lowercase the basename and extension. I liked the tr -cd [:alpha:] command I saw elsewhere on the internet but couldn't figure out how to use it to rename files with Hazel.
So, based on the little looking around I've done, I need to wrap my mind around $1 , basename, possibly handle file extension, and how to pipe/redirect commands to act on the file names and to actually rename them.
I'm able to use basename, but I'm struggling to do my own name matching in the script.
My test code so far:
And my output:
I'd like to learn how to create a basic shell script to check for a match of two specific file names, example here "eXPRS Plan of Care - Services Delivered Form" and "eXPRS Plan of Care - Mileage Driven Form" if no match happens the script exits and does not do anything. If a match happens act on the file(s) that match and strip desired characters with regex pattern using command-line tools. In this case, I just want to remove anything other than [a-zA-Z] (including spaces and hyphen) and lowercase the basename and extension. I liked the tr -cd [:alpha:] command I saw elsewhere on the internet but couldn't figure out how to use it to rename files with Hazel.
So, based on the little looking around I've done, I need to wrap my mind around $1 , basename, possibly handle file extension, and how to pipe/redirect commands to act on the file names and to actually rename them.
I'm able to use basename, but I'm struggling to do my own name matching in the script.
My test code so far:
- Code: Select all
#!/bin/bash
FILENAME=$(basename "$1")
echo The Filename is: "$FILENAME" >> /Users/adamlogan/Desktop/basenameexerciseoutput.txt
if [ "$FILENAME"='eXPRS Plan of Care - Services Delivered Form.pdf']
then
echo ""$FILENAME" matches the rule" >> /Users/adamlogan/Desktop/basenameexerciseoutput.txt
else
echo ""$FILENAME" does not match the rule" >> /Users/adamlogan/Desktop/basenameexerciseoutput.txt
fi
And my output:
- Code: Select all
The Filename is: eXPRS Plan of Care - Services Delivered Form.pdf
eXPRS Plan of Care - Services Delivered Form.pdf does not match the rule