ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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 trying to use getopts in a script which acts like the "rm" command in UNIX, my script is below, its not complete, I have added an extra case statement to deal with precedence and even though it works its not a very good way to go about things, I feel I am under using getopts and that I am wasting porcessor power in the second case statment to check the combination of options and act accordingly.
Here is my script:
Code:
#!/bin/bash
# program to emulate the "rm" command in UNIX.
#
# CREATE TRASH FOLDER
if ! [ -d "$HOME/deleted" ] ; then
mkdir $HOME/deleted
fi
# INITIALIZE VARIABLES
NO_ARGS=0
FLAG_R=""
FLAG_F=""
FLAG_I=""
FLAG_V=""
TRASH=$HOME/deleted
# FUNCTIONS
function errorInvalidOpt() {
echo "rm: invalid option - $1"
echo "try \`rm -help\` for more information"
exit 0
}
function errorTooFew() {
echo "rm: too few arguments"
echo "try \`rm --help' for more information"
}
function errorNoSuch() {
echo "rm: cannot remove $* : no such file or directory"
}
function writePro () {
echo -n "rm: remove write-protected file \`$*'?"
read ANSWER
if [ "$ANSWER" = "y" ] && [ "$FLAG_V" = "v" ] ; then
mv $OPTS $@ $TRASH 2>/dev/null
echo "removing \`$*'"
else
mv $OPTS $@ $TRASH 2>/dev/null
delete $@
fi
}
function verbose () {
mv $@ $TRASH 2>/dev/null
echo "removing \`$*'"
}
function intVerbose () {
echo -n "rm: remove $* ?"
read ANSWER
if [ "$ANSWER" = "y" ] ; then
mv $@ $TRASH 2>/dev/null
echo "removing \`$*'"
fi
}
function int () {
echo -n "rm: remove $* ?"
read ANSWER
if [ "$ANSWER" = "y" ] ; then
mv $@ $TRASH 2>/dev/null
fi
}
function delete() {
while :
do case $OPTS in
v|ivf|vf|ifv|vif) verbose $@
break
;;
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`
# FLOW CONTROL
OPTS=$FLAG_R$FLAG_F$FLAG_I$FLAG_V
if [ "$#" -eq "$NO_ARGS" ] ; then
errorTooFew $@
elif ! [ -f "$1" ] && ! [ -d "$1" ]; then
errorNoSuch $@
elif ! [ -w "$1" ] ; then
writePro $@
else
delete $@
fi
The way I worked it was to flag each option as it was parsed by getopts and then add all flags which were actioned to a varible called $OPTS and then send that to a second case statement to check the combination and act act accordingly.
How can I use getopts to determine which option is last on the command line and has precedence where contradiction is concerned, I will be improving thr rest of the script and hope to reduce a lot of code and make it neater, for now I need help on the precedence before I fix the rest of the script.
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900
Rep:
Doesn't getopts just proceed options in order of their appearance, so you can have a variable for every conflict and toggle it one side or other on every encounter of any of two options?
Doesn't getopts just proceed options in order of their appearance, so you can have a variable for every conflict and toggle it one side or other on every encounter of any of two options?
Hi thanks for the reply,
could you give me and example of what you mean, I think I have something similar in the case statment but its messy, I can figure out a simpler way to do it.
while getopts :if o; do
case $o in
i) I_F_FLAG=I
;;
f) I_F_FLAG=F
;;
esac;
done
thanks again, but for more options wouldnt that mean only the last option took precedence and the others would be ignored?
like if I use, myabe 3 option arguments and 2 of them need to be actioned like interactive and verbose wouldnt that only process the last one?
like if i use options -fiv it should say
rm : remove filename?
and then I enter y/Y
and it should then say
removing filename
but if the I_F_FLAG holds only 1 value then I cant do more than one thing for multiple options
its fried my brain thinking of ways round this, the extra case statment works with a little more work but i want to reduce the code and processing time as much as possible.
while getopts :if o; do
case $o in
i) I_F_FLAG=I
;;
f) I_F_FLAG=F
;;
esac;
done
Hey, I think this might work (need to test it):
Code:
while getopts :rRfvi o
do case $o in
r|R) FLAG_R=r ; PREC=r
;;
f) FLAG_F=f ; PREC=f
;;
v) FLAG_V=v ; PREC=v
;;
i) FLAG_I=i ; PREC=i
;;
*) echo "rm: invalid option -$1"
echo "try \`rm --help' for more information"
exit 0
then I can keep my flags and PREC for precedence will only have the value of the last option, I can then use that value in my functions to determine which option was last and act accordingly (i think).
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900
Rep:
No, you can't. At least in general case. Consider 'rm -ririv aaa;'. I meant, if you had -i/-f and -v/-q pairs, you would make FLAG_I_F and FLAG_V_Q. If some option -r doesn't conflict with anything, make FLAG_R.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.