[SOLVED] why is my delete bash script not working?
Linux - NewbieThis 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
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.
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
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."
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."
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."
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.