ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
#! /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.....
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
Last edited by perfect_circle; 02-22-2006 at 10:15 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.