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!!! |
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." |
Is there a reason you can't do this?
Code:
rm -rfi ~/dustbin/* Code:
rm -rf ~/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 |
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 |
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. |