LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-04-2010, 02:36 PM   #1
BrinkofMadness
LQ Newbie
 
Registered: Dec 2010
Posts: 6

Rep: Reputation: 0
Angry why is my delete bash script not working?


Hi I am new to linux and struggling a bit to get to grips with it. I am trying to write a bash script that will delete all the files from the dustbin directory, asking the user to confirm the deletiion unless -a is used in which case it should just delete the files. I think I've got a bit lost and don't really know where I am going wrong...I would appreciate any help!!
Here's what I've got

If test ! -f ~/dustbin/*
then
echo "The dustbin directory is empty, no files to delete"
else
for $* ~/dustbin/*
do
if test -f $*
then
echo "Do you want to delete $*?"
echo "Enter y or n"
read ans
if test $ans = y
then
rm $*
echo "$* has been deleted"
fi
done
fi


its driving me mad!!!
 
Old 12-04-2010, 03:38 PM   #2
wpeckham
Senior Member
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 1,797

Rep: Reputation: 619Reputation: 619Reputation: 619Reputation: 619Reputation: 619Reputation: 619
cleanup script

How about something like (just off the top, totally untested):

#!/bin/bash
#

if [ "$1" == "-a" ] ; then
TEST=NO
else
TEST=YES
fi

if [ ! -d ] ; then
echo "dustbin does not exist or is not a folder."
exit 1
fi
pushd ~/dustbin
LIST=`ls`
for foo in $LIST ; do
if [ -f $foo ] ; then
if [ "$TEST" == "YES" ] ; then
rm -i $foo
else
rm -f $foo
fi
else
echo "$foo is not a file I can delete."
fi
done
popd
echo "Done cleaning dustbin."
 
Old 12-04-2010, 03:42 PM   #3
gd2shoe
Member
 
Registered: Jun 2004
Location: Northern CA
Distribution: Debian
Posts: 835

Rep: Reputation: 49
Is there a reason you can't do this?

Code:
rm -rfi ~/dustbin/*
or

Code:
rm -rf ~/dustbin/*
The '-i' means interactive, it will ask before deleting each file.
 
1 members found this post helpful.
Old 12-04-2010, 04:07 PM   #4
gd2shoe
Member
 
Registered: Jun 2004
Location: Northern CA
Distribution: Debian
Posts: 835

Rep: Reputation: 49
Please use code blocks. They're so much easier to read.

I still vote for 'rm -rfi', but this is (arguably) a mild refinement of wpeckham's solution. This does test to ensure subdirectories are not deleted, if you care. Since it runs as a new process, pushd and popd are superfluous.

(still untested)
Code:
#!/bin/bash

dustbin="~/dustbin"

ASK=1
if [ "$1" == "-a" ] ; then
	ASK=0
fi

if [ ! -d $dustbin ] ; then
	echo "dustbin does not exist or is not a folder."
	exit 1
fi

cd $dustbin

for foo in * ; do
	if [ -f $foo ] ; then
		if test $ASK ; then
			rm -i $foo
		else
			rm -f $foo
		fi
	else
		echo "$foo is not a file I can delete."
	fi
done

echo "Done cleaning dustbin."
 
Old 12-05-2010, 08:56 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
A little modification to gd2shoe's code, it will ask if you want to delete folders, but without asking about each file and subfolder inside:

Code:
#!/bin/bash

dustbin="~/dustbin"

ASK=1
if [ "$1" == "-a" ] ; then
	ASK=0
fi

if [ ! -d $dustbin ] ; then
	echo "dustbin does not exist or is not a folder."
	exit 1
fi

cd $dustbin

for foo in * ; do
	if [ -f $foo ] ; then
		if test $ASK ; then
			rm -i $foo
		else
			echo -n 'Delete directory '"$foo"' [y/N]? '
			read $response
			if [ "$response" = y ]; then
				rm -f $foo
			fi
		fi
	else
		echo "$foo is not a file I can delete."
	fi
done

echo "Done cleaning dustbin."
[/QUOTE]
 
Old 12-05-2010, 10:43 AM   #6
BrinkofMadness
LQ Newbie
 
Registered: Dec 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Cheers

Thanks guys, i got it working with a tiny bit of tweeking from your suggestions.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash script to delete all comments after '#'?? dr44mon Linux - Newbie 4 01-30-2009 04:12 PM
Need a bash shell script which will delete lines from file scjohnie Linux - Newbie 1 09-13-2008 09:51 PM
Recursiveli delete in Bash Script combatdoc Programming 2 02-08-2008 10:47 AM
Why I am not figuring the delete.sh bash script? acwbrat Programming 1 11-02-2007 02:37 AM
bash script to delete files c0d3 Programming 9 12-05-2004 11:45 PM


All times are GMT -5. The time now is 11:53 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration