The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-02-2006
Registered User
 

Join Date: Oct 2006
Location: San Jose, CA
Posts: 11
rm question

I want to use the rm command to delete files in a directory. But I want to be able to set a variable that says how many arguments it will delete. So for instance, if I set a variable to null,.. (i.e. MAXFILES="") then it will just delete all the arguments. But if I set MAXFILES=2 for instance, then it will delete only 2 arguments. I'm writing a program to do this, and there are other specifications too,.. but I think I have most of it figured out but I can't seem to get past this one hurdle. Any thoughts? Great thanks in advance to any advice on this.

Btw,... before I start writing the program, I want to be able to practice setting the variable and then using that variable just using the command at the command prompt. In other words, I want to be able to just do something like...

$ MAXFILES=""
$ rm arg1 arg2 arg3
$

or

$ MAXFILES=2
$ rm somethinggoeshere (and it deletes the files per the number of arguments defined in the variable above.)

This way it will hopefully sink in through my thick cranium on how this is working.
Reply With Quote
Forum Sponsor
  #2  
Old 12-02-2006
System Shock's Avatar
Registered User
 

Join Date: May 2006
Location: Tau Ceti V
Posts: 395
Let me get this straight.
You want to set a variable that will limit the number of files you can delete in the command line, ex:
if you set MAXFILES to 3, and you type something like
Code:
 delete file1 file2 file3 file4 file5
only file1 file2, and file3 will be deleted.

Is that what you are looking for?
Reply With Quote
  #3  
Old 12-02-2006
Registered User
 

Join Date: Oct 2006
Location: San Jose, CA
Posts: 11
Quote:
Originally Posted by System Shock
Let me get this straight.
You want to set a variable that will limit the number of files you can delete in the command line, ex:
if you set MAXFILES to 3, and you type something like
Code:
 delete file1 file2 file3 file4 file5
only file1 file2, and file3 will be deleted.

Is that what you are looking for?
Yeah. Something like that. Here is the code I have so far for the program. It kinda sorta works but either I'm doing something wrong or it's missing something or I dunno.


Code:
#!/bin/bash
# Program: myrm

if [ "$#" -le 0 ]
then
        echo "Not enough arguments"
        echo "Usage: myrm [arg1] [arg2] etc..."
        exit 1
fi

MAXFILES=""
FILECOUNT=$(ls | wc -l)

if [ -n "$MAXFILES" ]
then

        if [ "$FILECOUNT" = "$MAXFILES" ]
        then
                for i
                do
                        rm -rf $1
                        shift
                done

        elif [ "$MAXFILES" > "$FILECOUNT" ]
        then
                echo "Would you like to delete these files?"
                read choice

                case "$choice"
                in
                        y)      for i; do rm -rf $1; shift; done;;
                        n)      echo "Thank you, have a nice day";;
                        *)      echo "Invalid choice"
                                exit 2;;
                esac
        fi

elif [ -z "$MAXFILES" ]
then
        for i in $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
        do
                rm -rf $1
                shift
        done
        exit 0


fi
Reply With Quote
  #4  
Old 12-02-2006
reborg's Avatar
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 3,644
two comments.

1. Give the full path to the rm command, it may be aliased.
2. Test for wildcards, if you pass in a quoted wildcard, it may not behave as expected.
Reply With Quote
  #5  
Old 12-02-2006
Registered User
 

Join Date: Dec 2006
Location: Maryland
Posts: 144
why do you want to do this ?

rm file1 file2 will delete file1 and file2 , rm * will delete everything. I can not undestand your requirement or this is a stupid requirement.

Regds,

Kaps
Reply With Quote
  #6  
Old 12-02-2006
Registered User
 

Join Date: Oct 2006
Location: San Jose, CA
Posts: 11
Quote:
Originally Posted by kapilraj
why do you want to do this ?

rm file1 file2 will delete file1 and file2 , rm * will delete everything. I can not undestand your requirement or this is a stupid requirement.

Regds,

Kaps
Yeah,.. I guess when we are first learning something people give us stupid questions to figure out. LOL. Believe me,.. I have way better things I'd rather spend my Saturday night doing but I have to get this finished. Also,.. just remember,.. this is probably really easy for you because you are probably an expert with this but I am just a n00b so there's a lot that doesn't make sense to me yet. But I'm getting better and better at it everday.

Well, anyway,.. I think I have it all figured out for the most part. Now I just have to make it so that if the number of arguments exceed the number specified in MAXFILES, then it will ask me to confirm the deletion. That shouldn't be too hard. Anyway,.. thanks all for the great help.

Here's my revised version which works the way I need it to for the most part.

Code:
#!/bin/bash
# Program: myrm

if [ -z "$MAXFILES" -a "$#" -ge 1 ]
then
        for MAXFILES in $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
        do
                echo "deleted$MAXFILES"
                shift
        done
        exit 1
fi

if [ -n "$MAXFILES" -a "$#" -ge 1 ]
then
        MAXFILES="$*"
        rm -rf $MAXFILES
fi
Then I just do something like this:

Code:
$ MAXFILES=56 ./myrm f1 f2 f3 f4
# then it deletes the 4 files because they are less than the amount specified in MAXFILES.
or
$ MAXFILES= ./myrm f1 f2 f3 f4

blah blah blah
Also,.. I have just an echo command in the top section just to make things easier. I'll go back and put the correct commands in there later. I just mainly needed to figure out HOW to do it and I think I got it now.
Reply With Quote
  #7  
Old 12-03-2006
Registered User
 

Join Date: Oct 2006
Location: San Jose, CA
Posts: 11
Whoo hoo! Finally after working on this all day I finished it. I feel so accomplished now.

Code:
#!/bin/bash
# Program: myrm

if [ "$#" -le 0 ]
then
        echo "Not enough arguments"
        echo "Usage: myrm [arg1] [myarg2] etc.."
fi

if [ -z "$MAXFILES" ]
then
        for i in $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10}
        do
                rm -rf $i
                shift
        done
        exit 1
fi

x=$(ls | wc -l)

if [ -n "$MAXFILES" -a "$#" -lt "$MAXFILES" ]
then
        rm -rf $*
else
        echo "Remove $# files? (y/n)"
        read choice

        case "$choice"
        in
                y)      rm -rf $*;;
                n)      echo "Ok, I will not delete any files then";;
                *)      echo "Invalid option"
                        exit;;
        esac
fi
And here is how it works:

Code:
$ ls
f1  f10  f11  f12  f13  f14  f15  f2  f3  f4  f5  f6  f7  f8  f9  myrm
$ MAXFILES= ./myrm f*
$ ls
f5  f6  f7  f8  f9  myrm
# MAXFILES is set to null and deletes up to 10 files even though I specified more.

$ MAXFILES=2 ./myrm f1 f2 f3 f4 f5
Remove 5 files? (y/n)
y
$ ls
f10  f11  f12  f13  f14  f15  f6  f7  f8  f9  myrm
# MAXFILES is set to 2 but I specified 5 files to delete. So it asks me y/n to confirm.

$ MAXFILES=5 ./myrm f1 f2 f3
$ ls
f10  f11  f12  f13  f14  f15  f4  f5  f6  f7  f8  f9  myrm
$ 
# MAXFILES is set to 5 and since I have specifed less than 5 arguments, it just does it without question.
Anyway,... case closed.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0