Need help with shell scripts
Hi,
I want to write a shell script that deletes everything in the working directory except the files listed as arguments. I'll use this script on Mac OS X so it would be useful if it supported file names with spaces. Code:
Here is what I have : Any help is appreciated. |
there is a variable "$@" that gives you all the parameter you select on a line...
the basic problem i see here is that when you put the names of the files in that you dont want to delete, there is no way for the shell to tell that the spaces are not different variables... you are also using $1 which means the first parameter that is passwd. http://tille.xalasys.com/training/ba...#sect_03_02_05 |
better try this in the for loop:
Code:
for i in * Code:
rm $i Code:
rm "$i" Quote:
$1 is only the first argument |
Quote:
|
how about this:
Code:
#! /bin/bash - test -e checks if it's a file $? is the return value of the last command. so if the file is is not one that was passed to the script "$@" and its actually a file, it will get deleted. works.... edit: that said you could use get opt and put a -f or someting for each file name..... |
DO NOT USE THIS:
Code:
for each in `ls` Code:
skalkoto@darkstar:~/temp$ touch "I am A File" |
forgive my mistake... it was just off the top of my head you know.
#! /usr/local/bin/bash for each in * do echo $@| grep $each > /dev/null 2>&1 if [ "$?" -gt 0 -a -e "$each" ] then rm $each fi done don't know what i was doing with that if statement there. lol |
chmod -w "$@"
rm * |
how does that help if you are root?
|
Quote:
|
and what's the answer???
|
Quote:
it doesn't help..... |
All times are GMT -5. The time now is 07:59 PM. |