Bash Scripting " [!: not found " errors, and how to write an or statement

bash, scripting, syntax-error, unix

Solution

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.

Problem

EDIT: Here is my updated code: ``` #!/bin/sh files=`ls` if [ $# -ne 1 -o -f $1 ] then echo "Usage: $0 <directory>" exit 1 fi if [ ! -e $1 ] then echo "$1 not found" exit 1 elif [ -d $1 ] then cd $1 for f in $files do if [ ! -d "$f" ] then if [ ! -s "$f" ] then rm -r "$f" echo "File: $f was removed." else continue fi fi done echo "Name\t\tLinks\t\tOwner\t\tDate" for f in $files do find "$f" -type f -printf "%f\t\t %n\t\t %u\t %TH %Tb %TY\n" done exit 0 fi ``` I fixed all of the spacing issues, changed #!bin/sh to #/bin/bash, and added quotes to "$f". However I'm still getting lots of errors. ava@kosh:~/test$ ./delDir d1 rm: cannot remove `d1': No such file or directory File: d1 was removed. rm: cannot remove`delDir': No such file or directory File: delDir was removed. rm: cannot remove `delDir2': No such file or directory File: delDir2 was removed. rm: cannot remove`e1': No such file or directory File: e1 was removed. rm: cannot remove `e2': No such file or directory File: e2 was removed. rm: cannot remove`make_d1': No such file or directory File: make_d1 was removed. Name\t\tLinks\t\tOwner\t\tDate find: d1: No such file or directory find: delDir: No such file or directory find: delDir2: No such file or directory find: e1: No such file or directory find: e2: No such file or directory find: make_d1: No such file or directory ne1 2 ava 22 Nov 2009 ne2 2 ava 22 Nov 2009 Does anyone know what else I'm doing wrong? Here's my code: ``` #!/bin/sh files=`ls` if [ $# -ne 1 ] then echo "Usage: $0 <directory>" exit 1 fi if [ ! -e $1 ] then echo "$1 not found" exit 1 elif [ -d $1 ] then cd $1 for f in $files do if [! -d $f] then if [ ! -s $f ] then rm-r $f echo "File: $f was removed." else continue fi fi done echo "Name\t\tLinks\t\tOwner\t\tDate" for f in $files do find $f -type f -printf "%f\t\t %n\t\t %u\t %TH %Tb %TY\n" done exit 0 fi ``` Here are my questions: If I execute the script with something that is NOT an ordinary file AND is NOT a directory I want it to say "Usage: Filename directory" (see line 5). I know I can do this with 2 if statements but is it possible to create an or statement for this in bash? When I run the script I keep getting errors like this: ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found ./delDir: 39: [!: not found Name Links Owner Date find: d1: No such file or directory find: delDir: No such file or directory find: delDir2: No such file or directory e1 1 ava 22 Nov 2009 e2 1 ava 22 Nov 2009 find: make_d1: No such file or directory ne1 2 ava 22 Nov 2009 ne2 2 ava 22 Nov 2009 I believe I am getting these errors because the for loop is first looking for the file that the user typed in (the directory it changed into) and cannot find it. How can if fix this? 3. Are there any more errors you can see?

Original source