automatically prefix files with holding directory name

I wish to prefix files in a subdirectory with the name of that directory.
I have a working shell script to do this invoked by script.sh <filenames>
#!/bin/bash
for f in "$1" ;
do mv "$f" "${PWD##*/}""_""${f/_*_/_}";
done
or recursively
#!/bin/bash
for i in $(ls); do # runs through the 'items' in this dir
if [ -d $i ]; then # if this is a dir
fname=${i##*/} # pick up the dir name which will be used as prefix
echo $fname
cd $i # move into the dir
for z in *; do # loop over files
# echo $z
mv $z ${fname}_${z} # put the prefix to the file.
done
cd ..
fi
done
But trying to embody in a Hazel rule (go into subfolders, execute script on any file NOT folder doesn't work.
The Hazel embedded script is:
mv "$1" "${PWD##*/}""_""${1/_*_/_}";
I know I am missing something key - grateful for pointers.
I have a working shell script to do this invoked by script.sh <filenames>
#!/bin/bash
for f in "$1" ;
do mv "$f" "${PWD##*/}""_""${f/_*_/_}";
done
or recursively
#!/bin/bash
for i in $(ls); do # runs through the 'items' in this dir
if [ -d $i ]; then # if this is a dir
fname=${i##*/} # pick up the dir name which will be used as prefix
echo $fname
cd $i # move into the dir
for z in *; do # loop over files
# echo $z
mv $z ${fname}_${z} # put the prefix to the file.
done
cd ..
fi
done
But trying to embody in a Hazel rule (go into subfolders, execute script on any file NOT folder doesn't work.
The Hazel embedded script is:
mv "$1" "${PWD##*/}""_""${1/_*_/_}";
I know I am missing something key - grateful for pointers.