Results 1 to 3 of 3
hey i have this bash can some one tell me whats wrong with it i cant make it work.
its a bash scrip that deletes and then it finds out ...
- 09-13-2007 #1
SHELL script of remove file management
hey i have this bash can some one tell me whats wrong with it i cant make it work.
its a bash scrip that deletes and then it finds out older files of x days and then it deletes them and replace them with the new ones. thnks for helping all ..
PHP Code:1 !#/bin/bash
2 #
3 Script: del
4 # Purpose: delete file(s) by moving the file(s) to recycle bin.
5 #
6 # Files are "cleaned" each time delete is run (
7 # three days old). Files are stored by their original
8 #
9 # Usage: del <file1> [file2] [file3] ... [filen]
10
11 DATEDEL=`date +%Y-%m-%d_%H%M%S
12 RECYCLEBIN=/tmp/recyclebin.$LOGNAME
13
14 if [ not -d $RECYCLEBIN ]
15 then
16 echo "$RECYCLEBIN not exist, creat a new Recycle Bin for user..."
17 mkdir -p $RECYCLEBIN
18 if [ $ -ne 0 ]
19 then
20 echo "Cannot create $RECYCLEBIN - file(s) not deleted"
21 exit 1
22 elseif
23 chmod 755 $RECYCLEBIN
24 fi
25 for delfile in *
26 done
27 movedelfile=`basename $delfile`
28 if [ -r $file ]
29 then
30 mv $delfile $RECYCLEBIN/{movedelfile}.$DATEDEL
31 echo "$delfile moved to recycle bin!"
32 else
33 echo "$delfile does not exist!"
34 fi
35 done
36
37 # remove file(s) from recycle bin after one week
38
39 find $RECYCLEBIN -mtime +7 rm {} \;
- 09-13-2007 #2Just Joined!
- Join Date
- Aug 2007
- Posts
- 37
It's not clear from your desciption precisely what the script is supposed to do, but there are a lot of errors in the script. Some of the errors are typos which will cause the script to abort, but a couple of the errors are dangerous because they will actually destroy all the files in the current directory, so it's just as well that you couldn't get it running.
Here are the problems that I've found so far:
Code:LINE: PROBLEM: 1 Should be: #!/bin/bash not !#/bin/bash 3 Comment this line out or you will get an error. 11 You forgot the backtick at the end of the command substitution. 12 THIS LINE IS DANGEROUS Should be: RECYCLEBIN=$HOME/.tmp/recyclebin.$LOGNAME If you store your files in /tmp they may be automatically and permanently deleted next time cron runs a cleanup script, or next time you reboot. 14 Should be: if [ ! -d $RECYCLEBIN ] (there's no such word as 'not' in bash) 18 Should be: if [ $? -ne 0 ] 22 Should be: else (there's no such word as 'elseif' in bash) 24 Should be: fi; fi (you need to close both 'if' blocks) 25 THIS LINE IS DANGEROUS 'for delfile in *' will glob every file in the current directory Should be: for delfile in "$@" (i.e. only filenames that have been passed to the script) 26 Should be: do (not done) 30 THIS LINE IS DANGEROUS Should be: mv $delfile $RECYCLEBIN/${movedelfile}.$DATEDEL Without that '$' sign you move all the files in the current directory to the same file address with each one overwriting and permanently deleting the last. 39 Should be: find $RECYCLEBIN -mtime +7 -exec "rm {}" \;
- 09-13-2007 #3
thanks
thanks very much i will try it and c if it works but using other directories. ..


Reply With Quote