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 ]
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 ]
The other answers are correct, here's why: [ is a command. If you type "[!", the shell looks for a command by that name.
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 :)
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"
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.