LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   why is my delete bash script not working? (https://www.linuxquestions.org/questions/linux-newbie-8/why-is-my-delete-bash-script-not-working-848364/)

BrinkofMadness 12-04-2010 01:36 PM

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

wpeckham 12-04-2010 02:38 PM

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

gd2shoe 12-04-2010 02:42 PM

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.

gd2shoe 12-04-2010 03:07 PM

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


MTK358 12-05-2010 07:56 AM

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]

BrinkofMadness 12-05-2010 09:43 AM

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


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