![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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 Is that what you are looking for? |
|
#3
|
|||
|
|||
|
Quote:
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
|
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
Quote:
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
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 |
|
#7
|
|||
|
|||
|
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
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. |
|||
| Google The UNIX and Linux Forums |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|