LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 01: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, 02:38 PM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,623

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
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, 02: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, 03: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, 07:56 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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, 09: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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:27 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