new to shell scripting: whats wrong with my if loop

#!/bin/bash

for file in $HOME/[ A-Za-z ]*;
do
if [ -z $file ]; then
rm -i $file > /dev/null
echo "$?"
echo "$file has been deleted"
fi
done

Been trying to learn shell scripting for a week or so now, when i run the script it doesnt display an error message, seems like it runs fine, however it doesnt delete any files. can anyone tell me whats wrong with it please?

thanks.

read the man page for your shell, the -z option is not what you are looking for in the if statement.

thanks for the prompt reply. so would i run the command man bash? I was reading a book and it said to use the -z option to check wether the string is empty.

Correct on both counts.

Think about it, if the string is empty what will the command
rm -i $file be doing?

remove the empty file[touch file i created to test the script] is what im trying to do , shouldnt it do just that?

No, you are misunderstaning what $file is, try putting echo $file instead of rm -i $file to see if it's an empty string.

#!/bin/bash

for file in $HOME/[ A-Za-z ]*
do
   echo $file
done

Is $file an empty string?

oh man im lost.

ok, in simple terms, $file is the filename, not the file contents.

^ exactly so wouldnt -z check the file content and then erase the file if its empty? or is it -s?

Now you're getting very close.

-s is : if the file exist and size is > 0.

So you need to test that you have a file ( and not a directory )
Then you need to check the size = 0.

if [[ ! -s $file ]] ; then

will be true if either the file doesn't exist or it's empty, so if you couple that with a test to see if you have a file you will be ready to go.

thanks man, its strange though why isnt there an option that checks to see if the file is empty instead of having to negate the statement.