Hi,
I am re-writing a script I wrote which emulated the "rm" command, in my orginal script I had problems with precedence, I did find a way round it by creating a seperate case statements which checked the options and performed the actions accordingly, does anyone know if I can use getopts better to help with precedence? and save on using the extra case statement.
So instead of this part of my code:
function delete() {
while :
do case $OPTS in
v|ivf|vf|ifv|vif) verbose $@
break
;;
fi|vfi|fvi|iv|vi|fiv) intVerbose $@
break
;;
f|fv|if) mv -f $@ $TRASH 2>/dev/null
break
;;
i) int $@
break
;;
r)mv $@ $TRASH 2>/dev/null
break
;;
*)mv $@ $TRASH 2>/dev/null
break
esac
done
}
# GETOPTS
while getopts :rRfvi o
do case $o in
r|R) FLAG_R=
;;
f) FLAG_F=f
;;
v) FLAG_V=v
;;
i) FLAG_I=i
;;
*) errorInvalidOpt
esac
done
shift `expr $OPTIND - 1`
OPTS=$FLAG_R$FLAG_F$FLAG_I$FLAG_V
I can manage it within getopts by just using this:
while getopts :rRfvi o
do case $o in
r|R) FLAG_R=r #what can I add to my getopts to set the right flags
;; # in order with rm's precedence
f) FLAG_F=f
;;
v) FLAG_V=v
;;
i) FLAG_I=i
;;
*) errorInvalidOpt
esac
done
shift `expr $OPTIND - 1`
I have ommitted the rest of my code but I am trying to emulate the "rm" and I am providing other functions to move the files to a trash folder rather than delete for good.
So, given this option argument: -fi it should not force but should interact but if I enter -if it should not interact but should force, how do I utilise getopts to allow for this withour the case statement in the top code?
Thanks
Jack