+6  A: 

You forgot to put a space after [ and before ] on the line saying:

if [! -d $f]

AND tests are created using -a, -o is equal to OR:

if [ ! -d $f -a -f $f ]
if [ ! -d $f -o -f $f ]
Mikael S
Wow... I feel dumb thanks for that. haha.
에이바
Here's what happens after the spaces have been removed:ava@kosh:~/test$ ./delDir d1./delDir: 39: rm-r: not foundFile: d1 was removed../delDir: 39: rm-r: not foundFile: delDir was removed../delDir: 39: rm-r: not foundFile: delDir2 was removed../delDir: 39: rm-r: not foundFile: e1 was removed../delDir: 39: rm-r: not foundFile: e2 was removed../delDir: 39: rm-r: not foundFile: make_d1 was removed.Name Links Owner Datefind: d1: No such file or directoryfind: delDir: No such file or directoryfind: delDir2: No such file or directorye1 1
에이바
You need to add a space between the `rm` and the `-r`; bash sees this a command `rm-r` and not `rm -r`.
George Shore
+3  A: 

Try putting a space between the [ and !.

singingwolfboy
A: 

The other answers are correct, here's why: [ is a command. If you type "[!", the shell looks for a command by that name.

Richard Pennington
+1  A: 

I'm augmenting other answers here ... every time I see a bash question start with #! /bin/sh I have to jump in, its a moral imperative.

Keep in mind that /bin/sh points to the POSIX invocation of bash, or a completely different "posixically correct" shell like dash. /bin/sh is often a symbolic link that causes bash to alter its behavior to be more POSIX compliant. Hence, lots of goodies won't work as usual, or you may find your code being parsed by another shell.

In other words, /bin/sh == POSIX_ME_HARDER, but .. yikes! == is a bashism :)

If you want bash, use #!/bin/bash

Beyond that, singingwolfboy's answer should fix your immediate problem :)

Tim Post
Oh I see. Thanks for the tip, I'll have to use that.
에이바
+1  A: 

The others got the spacing issue with the [, but I noticed a couple other things.

You probably need a space in your rm command in line 23:

rm -r $f

Also, it's usually good practice to double quote file paths. This will allow your script to handle filenames with spaces and other special characters correctly.

rm -r "$f"
amrox
Thanks for the info Amrox, I added the quotes as per your suggestion. I'll have to make use of them in the future.
에이바
+1  A: 

Another issue is that at the start of the script you do

files = `ls`

but then you cd to a different directory and try to loop round $files deleting them - of course this will not work since you have changed directories.

move the ls line to after you have changed directory.

Dave Kirby
Ah... okay I see. I'm trying that now.
에이바
Ah that fixed it, thanks Dave.
에이바