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.
I want to emulate the "rm" function in UNIX so that I can send files to a specified folder rather than delete them completely, a bit like a recycle bin. I plan to write a script then set "rm" as an alias t my script, a quesry I have is how do I find out which options have precedence over others? I need to work with options -r, -R, -f, -v, and -i is there any difference between -r and -R?
I will be using getopts for my script once I figure out how to use it.
There's been heaps of little scripts posted here that to just that;
move stuff to somewhere else.
As for the options: -r and -R are equivalent for rm
As for the precedence: if options contradict each other the last one
on the command-line gets the win.
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900
Rep:
Well, asking about precedence is useless - try it out. Note that /bin/rm, bash rm and zsh rm can behave differently. Logically (and for /bin/rm) precedence belongs to later switch. In zsh - to -f over -i (and -v is unknown).
#!/bin/bash
touch /$HOME/deleted
GARBAGE=/$HOME/deleted
while getopts rRfvi option; do
case $option in
r) while [ $# -gt "0" ]
do
mv $1 $GARBAGE
shift
done
;;
R)
echo "testing"
;;
f)
echo "testing"
;;
v)
echo "testing"
;;
i)
echo "testing"
;;
*)
echo -n "some error code to go here"
;;
esac
done
So if a user selects allias (which will be rm) and the provides option -r and then a file as an argument the script should move the file or directory to my location stored in garbage, the problem is that the 'mv' command seems to be getting the option "-r" passed to it from case and resulting in error, can anyone help me with why this might be?
I am making some progress, I have it set right now to read the argument after the the options, i still have a long way to go but I am on the right track I think, one thing I need to stop is everytime I run my script it gives an error that /$HOME/deleted is all ready created, how do I implement a stop so that it check first and then ingores if this evaluates as true?
Here is my code so far:
Code:
#!/bin/bash
function moveFiles () {
mv -f $OPTARG $GARBAGE
}
mkdir $HOME/deleted/
GARBAGE=$HOME/deleted/
NO_ARGS=0
SEPLIST="/t"
if [ $# -eq "$NO_ARGS" ]
then
echo "rm: too few arguments"
echo "Try \`rm --help\` for more information"
fi
while getopts :r:R:f:v:i option; do
case $option in
r) SEPLIST=$OPTARG
moveFiles
;;
R)
SEPLIST=$OPTARG
moveFiles
;;
f)
echo "testing"
;;
v)
echo "testing"
;;
i)
echo "testing"
;;
[?])
echo "invalid option -- $option"
echo "try \`rm --help\` for more information"
;;
esac
done
i still need to add quite alot but can you answer the above question for now please, also notice the last part of case, I cant figure out why it will only ever spit out invalid option --"?" instead of the actually input from the user, is there awya round this?
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900
Rep:
if ![ -d "$HOME/deleted" ] ; then ... fi;
Yes, and it seems to me that you have too little quotes. Test frequently on paths like "~/a/a/a a a/a a a/a a a" in presence of "~/a/a/a" . Try once and you'll understand (or switch to zsh).
Hello, I have managed to fix getopt but I have a problem, the file argument that comes though getopts should be assigned to $1, but when I try send it to safeDelete() as below it complains that I am missing an argument, can any one explain why?
ta
Mike
Code:
#!/bin/bash program to emulate the "rm" command in UNIX.
#
##Initialise trash folder
if ! [ -d "$HOME/deleted" ] ; then
mkdir $HOME/deleted
fi
##Initialise variables
NO_ARGS=0
FLAG_R=0
FLAG_F=0
FLAG_I=0
FLAG_S=0
TRASH=$HOME/deleted
##functions
function safeDelete() {
mv $1 $TRASH
}
##getopts option handling
while getopts :rRfvi o
do case $o in
r|R) FLAG_R=1 ;;
f) FLAG_F=1 ;;
s) FLAG_S=1 ;;
i) FLAG_I=1 ;;
esac
done
shift `expr $OPTIND - 1`
##flow control
if [ $# -eq "$NO_ARGS" ] ; then
echo "rm: too few arguments"
echo "try \`rm --help\` for more information"
fi
if [ $FLAG_R -eq 1 ] ; then
safeDelete
fi
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900
Rep:
After function call $1 is function parameter, not global. So you need to a) pass "$1" to safeDelete b) read man bash c) understand that your code will do weird things on "a a" and "a".
#!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=0
FLAG_F=0
FLAG_I=0
FLAG_V=0
TRASH=$HOME/deleted
# FUNCTIONS
function errorInvalidOpt() {
echo "rm: invalid option - $o"
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 okDelete() {
echo -n "rm: remove $* ?"
read ANSWER
if [ "$ANSWER" = "y" ] ; then
mv $@ $TRASH
fi
}
function notifyDelete() {
if [ -f ] ; then
echo "removing \`$*'"
mv $@ $TRASH
fi
}
function okNotify() {
echo -n "rm: remove $* ?"
read ANSWER
if [ "$ANSWER" = "y" ] ; then
notifyDelete $@
fi
}
function delete() {
if ! [ -f ] ; then
errorNoSuch;
else
mv $@ $TRASH
fi
}
# GETOPTS
while getopts :rRfvi o
do case $o in
r|R) FLAG_R=1 ;;
f) FLAG_F=1 ;;
v) FLAG_V=1 ;;
i) FLAG_I=1 ;;
*) errorInvalidOpt
esac
done
shift `expr $OPTIND - 1`
# FLOW CONTROL
if [ "$#" -eq "$NO_ARGS" ] ; then
errorTooFew
elif [ $FLAG_R -eq 0 ] && [ $FLAG_F -eq 0 ] && [ $FLAG_I -eq 0 ] && [ $FLAG_V -eq 0 ] ; then
delete $@
elif [ $FLAG_R -eq 1 ] && [ $FLAG_F -eq 1 ] && [ $FLAG_I -eq 1 ] && [ $FLAG_V -eq 1 ] ; then
okNotify $@
elif [ $FLAG_R -eq 1 ] && [ $FLAG_F -eq 1 ] && [ $FLAG_I -eq 1 ] ; then
okNotify $@
elif [ $FLAG_F -eq 1 ] && [ $FLAG_I -eq 1 ] && [ $FLAG_V -eq 1 ] ; then
okNotify $@
elif [ $FLAG_R -eq 1 ] && [ $FLAG_I -eq 1 ] && [ $FLAG_V -eq 1 ] ; then
okNotify $@
elif [ $FLAG_R -eq 1 ] && [ $FLAG_V -eq 1 ] ; then
notifyDelete $@
elif [ $FLAG_V -eq 1 ] && [ $FLAG_I -eq 1 ] ; then
okNotify $@
elif [ $FLAG_F -eq 1 ] && [ $FLAG_I -eq 1 ] ; then
notifyDelete $@
elif [ $FLAG_R -eq 1 ] ; then
delete $@
elif [ $FLAG_V -eq 1 ] ; then
notifyDelete $@
elif [ $FLAG_I -eq 1 ] ; then
okDelete $@
elif [ $FLAG_F -eq 1 ] ; then
delete $@
fi
how do I get rid of all the "if then else" statements and out them in a fuction based on the options flaggged?
any help would be great, I have submitted my work and passed but I know there is an easier way that what I am doing with all them statments, I just cant figure out how.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.