Loop for input section of rename script For the input section of a script, I wrote a snippet using a while loop and an if statement to check whether there are matches for the $search. Only if there are matches does it move on to the input of $replace; if there are no matches it asks for $search again.

 while true; do
     echo "Please enter token to be replaced: "
     read -e -r search
     match="$(find . -maxdepth 1 -path "*${search}*" -printf "." | wc -c)"
     if [ "${match}" -gt 0 ]; then
         echo "${match} file(s) found"
         break
     else
         echo "no matches found!"
         echo
     fi
 done

 echo "Please enter replacement: "
 read -e -r replace

# I used the following command to check if the output was right
# echo "rename 's/$search/$replace/g' ./*"


In this snippet I used advice I got from a question I asked on Stack Overflow.

This code is going to be used as the input section of a rename script. In the last line I added an echo "rename... command to check if the output works as expected.

The script is meant to process all files matching the $search criteria in the current directory. Therefore I used find . -maxdepth 1....

The script of which this snippet is part of got reviewed here earlier.

Does this code match general standards?
If you want to match by the same pattern as rename,
then it's probably best to use perl.
To be really pedantic and support filenames with embedded newlines,
you could use -print0 of find,
and then in perl split the output by \0 characters and filter with the grep function:

find . -maxdepth 1 -print0 | perl -ne "print scalar grep /$search/, split /\0/"


Putting in your script:

 while true; do
     echo "Please enter token to be replaced: "
     read -e -r search
     match=$(find . -maxdepth 1 -print0 | perl -ne "print scalar grep /$search/, split /\0/")
     if [ "${match}" -gt 0 ]; then
         echo "${match} file(s) found"
         break
     else
         echo "no matches found!"
         echo
     fi
 done