Click to See Complete Forum and Search --> : Shell script i can't figure out where the proble is


akshay
07-31-2002, 09:10 AM
Hey Guys....

I am really stuck here i am trying to practice shell scripting and i am very new to linux.

Here i have a code and it is not working can any one suggest me where is the problem in the code and rectify the code i would be very very much grateful to you.

1 #!/bin/bash

2 # Script: delete.sh

3 # Author: Kieren Jamieson

4 # Purpose: Provides a safe alternative to "rm" as

5 # each file is moved to a "trashcan" folder

6 # before it is deleted.

7 # Flies are "cleaned" each time delete is run (

8 # three days old). Files are stored by their original

12 Usage: delete.sh <file1> [file2] ... [filen]

13 $JUNKDIR=/tmp/trashcan.$LOGNAME

15 DATE='date +%Y-%m-%d_%H%M%S'

16

17 if [ not -d $JUNKDIR ]

18 then

19 echo "$JUNKDIR does not exist, creating..."

20 mkdir -p $JUNKDIR

21 if [ $? -ne 0 ]

22 then

23 echo "Error making directory $JUNKDIR - file not deleted"

24 exit 1

25 fi

26 chmod 755 $JUNKDIR

27 endif

28 for file in $#

29 do

30 newfile=`basename $file`

31 if [ -r file ]

32 then

33 mv $file $JUNKDIR/${newfile}.$DATE

34 else

35 echo `$file does not exist!`

36 fi

37 end

38

39 # Now check for cleanup

40

41 find $JUNKDIR -mtime +3 -exec rm {} ;



since i couldn't attach the .sh file i have written the code above.

can anyone please help

mrBen
07-31-2002, 09:31 AM
What happens when you run it? What are the errors?

l01yuk
07-31-2002, 09:46 AM
First off it was correct to post your code but you should do so within code tags like so...

echo "This is some code"

You can read up on vB Code from the posting pages.


Anyway, the script...



A couple of things.

You probably don't want the permissions to be 755. Either you want everyone to be able to write to the trashcan directory (777) or you only want one user to be able to use it and that should (IMO) include being able to read deleted files (700).

Your for loop is wrong, $# is the number of fields entered on the command line, not any actual values, so you won't be moving anything.

If you could re-post it using the code tags people will be more able to read the code and help.

Hope this helps a bit.

Strike
07-31-2002, 01:45 PM
In case you aren't doing this for playing with shell scripts but are doing it to fill a need ... there are scripts out there that do just this.

m1nds1llus1on
02-14-2006, 08:54 PM
Hey all, please do not help him debug this. He is a student at MTU in the computer administration course and part of a homework assignment was to debug this exact script. He is trying to cheat.

je_fro
02-14-2006, 08:56 PM
too late now....this was posted in 2002...

m1nds1llus1on
02-14-2006, 08:59 PM
Heh, my bad, didnt look at the dates, guess they have been giving the same hw assignment for the past 4 years.

je_fro
02-14-2006, 09:00 PM
heh...and how did YOU manage to come across this? :D
google to the rescue eh?

m1nds1llus1on
02-14-2006, 09:19 PM
nah, just typed in one line to see if the syntax was correct (figured results would pop up if it was) and this was the only english page to come up

bwkaz
02-15-2006, 08:43 PM
There's an MTU in Australia? (The IP that this was posted from reverse-resolves to ***.nsw.bigpond.net.au, which is an Australian ISP. True, it could have been anonymized, so this isn't foolproof, but I would guess it probably wasn't.)

If you meant MTU as in Michigan Tech University (I'm going to assume you did, based on the good old 141.219 network that I remember oh-so-well ;)), they didn't give me this script at any point when I was there (3-5 years ago). But maybe I wasn't in the right course, or something. I seem to remember a sysadmin course offered over the summer semester that I never bothered doing, because I adminned by own 2 Linux boxes most of the time I was there. Nothing like experience. :D (Plus it was a summer course, and I didn't want to stay there over a summer.)

(Heh -- maybe the MTU professor who does the sysadmin course stole this from an Australian professor or something. For some reason, that wouldn't surprise me at all. :p)

m1nds1llus1on
02-15-2006, 11:38 PM
nope, it is Michigan Tech that i am at. Dunno why it isn't a 141.219 IP cuz im just sittin on ResNet here. Also, its the Computer Administration class (CS3451) that it is for. Not required for CS but is required for my major Computer Systems Sciences.

bwkaz
02-16-2006, 08:25 PM
3451... that number sounds oddly familiar for some reason... Hmm.

Oh well, whatever. I'm pretty sure I never took that class. :)