Removing files matching a pattern

I am on ubuntu 11.10 using bash scripts

I want to remove all files matching a string pattern and I am using the following code

 find . -name "*$pattern*" -exec rm -f {} \;

I have encountered a problem when $pattern is empty. In this case all my files in my current directory were deleted. This was not intended. If no pattern is passed, I definitely do not want to delete all my files.

What would be a safe way to tackle file deletion?

Make sure whatever feeds $pattern checks if it is empty before.

You posted lots of code in all your previous threads like shell, perl and C programs iirc. etc.... I am still baffled...

I think this may help you:

ls *pattern* | xargs rm -rf {}
[ ! -z "$pattern" ] && find . -name "*$pattern*" -exec rm -f {} \;