LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-22-2006, 08:12 AM   #1
gudum35
LQ Newbie
 
Registered: Jun 2005
Posts: 9

Rep: Reputation: 0
Question 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.
 
Old 02-22-2006, 09:00 AM   #2
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 14.2
Posts: 1,491

Rep: Reputation: 50
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
 
Old 02-22-2006, 09:05 AM   #3
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
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
 
Old 02-22-2006, 09:07 AM   #4
gudum35
LQ Newbie
 
Registered: Jun 2005
Posts: 9

Original Poster
Rep: Reputation: 0
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?
 
Old 02-22-2006, 09:43 AM   #5
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 14.2
Posts: 1,491

Rep: Reputation: 50
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.....

Last edited by satinet; 02-22-2006 at 09:47 AM.
 
Old 02-22-2006, 10:14 AM   #6
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
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

Last edited by perfect_circle; 02-22-2006 at 10:15 AM.
 
Old 02-22-2006, 10:30 AM   #7
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 14.2
Posts: 1,491

Rep: Reputation: 50
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

Last edited by satinet; 02-22-2006 at 10:43 AM.
 
Old 02-23-2006, 03:43 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
chmod -w "$@"
rm *
 
Old 02-23-2006, 04:00 AM   #9
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 14.2
Posts: 1,491

Rep: Reputation: 50
how does that help if you are root?
 
Old 02-23-2006, 05:14 AM   #10
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Quote:
Originally Posted by satinet
how does that help if you are root?
That's a good question.
 
Old 02-23-2006, 05:15 AM   #11
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 14.2
Posts: 1,491

Rep: Reputation: 50
and what's the answer???
 
Old 02-23-2006, 06:32 AM   #12
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Quote:
Originally Posted by satinet
and what's the answer???
I guess the answer is:
it doesn't help.....
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripts baruah Linux - Certification 1 02-02-2006 01:19 PM
shell scripts rocketgo Linux - General 10 12-01-2003 05:20 AM
shell scripts... roofy Linux - Newbie 13 05-06-2003 06:06 PM
Shell scripts anon227 Linux - Software 1 01-03-2003 03:11 PM
shell scripts gui10 Programming 10 10-28-2001 02:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration