1

I've written a shell script (called Trash.sh) that when run will display all the files in my 'dustbin' directory and ask the user if they wish to delete each file one by one. The script is as follows:

#if directory is empty, display message
if test ! -f ~/dustbin/* 
then
echo "Directory is empty"
#if directory is not empty, then display each item and ask to delete
else
for resfile in ~/dustbin/* #for each file in directory, store it in resfile variable
do
    if test -f $resfile ; then #if a file exists
    echo "Do you want to delete $resfile?"
    echo "Enter y or n"
    read ans #store user input in ans variable
    if test $ans = y ; then
    rm $resfile
    echo "File $resfile was deleted"
    fi
    fi
done
fi

This works fine for me. When a user types sh Trash.sh the script will run fine, each file will be displayed and teh user will be asked to delete it or not. However what I would like to add is the option for the user to type sh Trash.sh -a and for all the files in the directory to be automatically deleted without confirmation.

I'm a bit stuck at how to achieve this. Any ideas?

ps - I'm using Mac OS X 10.6.4 and doing everything through Terminal

  • Please don't replace your original with a new version based on an answer. It makes answers not make any sense. It's preferable, if it's necessary at all, to add the revised version, clearly marked, below the original. – Dennis Williamson Nov 11 '10 at 16:05

5 Answers5

2

Try:

if [ x"$1" = x"-a" ]; then
  rm -rf ~/dustbin/*;
  exit 0;
fi

at the top of your script

knittl
  • 246,190
  • 53
  • 318
  • 364
  • You probably meant: `[ x"$1" = "x-a" ]` – glenn jackman Nov 11 '10 at 14:37
  • @glenn: Either will work since the quotes are removed when the strings are evaluated. (So would `[ "x$1" = "x-a" ]`) It's even evident in the part to the left of the equal sign. If you use this archaic idiom, it's probably better to use a parallel structure (`x` outside the quotes on both sides) so it's easier to see what's being done. With modern shells, [as long as the variable is quoted](http://stackoverflow.com/q/3869072/26428#3870055), the `x` is no longer necessary. – Dennis Williamson Nov 11 '10 at 16:13
  • @dennis: i was missing the x on the right side at first, i've edited my question since – knittl Nov 11 '10 at 16:15
  • Sorry (to you, too, glenn), I should've looked at the edit history. – Dennis Williamson Nov 11 '10 at 17:41
2

I would suggest capturing the -a option with getopts. Then test for it later and use rm -f (to force, see man page). The shell on Mac OS is a bit different than what I'm used to, but handling options in bash on Linux looks something like this:

force_all='f';
while getopts "ahv" arg
do
    case $arg in
        a) force_all='t';;
        v) echo "$SCRIPT: $VERSION"; exit;;
        h|*) echo "$SCRIPT: $HELP"; exit;;
    esac
done

This example shows how to implement -v for version and -h for help.

bogeymin
  • 685
  • 1
  • 9
  • 27
2

The first place to start would be to learn how to do argument parsing in bash scripts.

The short answer is to use the variables $1, $2, $N ($0 is the name of the bash script you are executing). $* is a string containing all arguments. This is how you get arguments in both bash scripts and your custom bash functions. In your case, a simple test like

if [[ $1 == "-a" ]]
then
  #profit!
fi

The long answer is to use and learn getopts: http://www.mkssoftware.com/docs/man1/getopts.1.asp

whaley
  • 16,075
  • 10
  • 57
  • 68
2

I would also suggest to use pure rm for asking for removal:

rm -riv ~/dustbin/*

And as stated in other answers remove without question:

rm -rfv ~/dustbin/*
mpapis
  • 52,729
  • 14
  • 121
  • 158
1

Check if $1 is -a. If so, delete all the files then exit.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358