LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need help with shell scripts (https://www.linuxquestions.org/questions/programming-9/need-help-with-shell-scripts-418351/)

gudum35 02-22-2006 08:12 AM

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 :

#!/bin/bash
for i in $(ls)
do
        if [ '$i' != '$1' ; then
                rm $i
        fi
done

At this point it only supports one argument and I doesn't work with file names including spaces.

Any help is appreciated.

satinet 02-22-2006 09:00 AM

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

perfect_circle 02-22-2006 09:05 AM

better try this in the for loop:
Code:

for i in *
also, in order to deal with spaces, always protect your variables with "". SO instead of
Code:

rm $i
try
Code:

rm "$i"
Quote:

except the files listed as arguments
Do you want the script to deal with more that one arguments?
$1 is only the first argument

gudum35 02-22-2006 09:07 AM

Quote:

Originally Posted by perfect_circle
Do you want the script to deal with more that one arguments?
$1 is only the first argument

Yeah I want it to be able to deal with any number of arguments. How would I do that?

satinet 02-22-2006 09:43 AM

how about this:
Code:

#! /bin/bash -
for each in `ls`
do
echo $@ |grep $each > /dev/null 2>&1 # finds out if file is excluded
if [ $2 "$?" -gt 0 -a -e $each ]    # checks if files is exluded by the option passwd to the script and if it's actually a file
then
rm $each
fi
done

i reckon that will work, but it won't get round the spaces issue

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.....

perfect_circle 02-22-2006 10:14 AM

DO NOT USE THIS:
Code:

for each in `ls`
THIS WILL SEPERATE FILES CONTAINING SPACES IN PARTS:
Code:

skalkoto@darkstar:~/temp$ touch "I am A File"
skalkoto@darkstar:~/temp$ ls
I\ am\ A\ File
skalkoto@darkstar:~/temp$ for each in `ls`; do echo "$each" ; done
I\
am\
A\
File
skalkoto@darkstar:~/temp$ for each in * ; do echo "$each" ; done
I am A File
skalkoto@darkstar:~/temp$

with the * it works. With `ls` it doesn't

satinet 02-22-2006 10:30 AM

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

bigearsbilly 02-23-2006 03:43 AM

chmod -w "$@"
rm *

satinet 02-23-2006 04:00 AM

how does that help if you are root?

perfect_circle 02-23-2006 05:14 AM

Quote:

Originally Posted by satinet
how does that help if you are root?

That's a good question.;)

satinet 02-23-2006 05:15 AM

and what's the answer???

perfect_circle 02-23-2006 06:32 AM

Quote:

Originally Posted by satinet
and what's the answer???

I guess the answer is:
it doesn't help.....


All times are GMT -5. The time now is 07:59 PM.