Thread: Bash script to move file to the trash
i'm trying create bash script move files trash, same thing "kioclient move file1 file2 file3 trash:/" more easily. script far:
i plan move script /usr/local/bin can run "trash file1 file2 file3" run "rm file1 file2 file3". question is, how add option make verbose, how add option make recursive? currently, if pass on parameters don't cause script exit (like --help does), try send parameter trash. example, if add verbose feature command "trash -v file1 file2 file3" make verbose, try send "-v" trash, impossible isn't file.code:#!/bin/bash ifs=" " # needed? # make sure file throw away specified if [ "$1" = "" ]; echo -e "trash: missing operand\ntry 'trash --help' more information" exit 1 fi # if --help passed on script, display if [ "$1" = "-h" -o "$1" = "--help" ]; cat << _eof usage: trash file(s)... move file(s) trash (same 'kioclient move file(s) trash:/'). -h, --help display , exit _eof exit 0 fi # go through each file, , if it's directory tell user # can't trashed (todo: impliment --recursive option bypass this, # , --verbose option explain what's happening) target in "$@"; if [ -f $target ]; kioclient move $target trash:/ else echo "trash: omitting directory '$target'" fi done exit 0
i'm relatively new bash scripting of gets confusing me.
no, mangling ifs should not necessary. i've never seen situation there wasn't cleaner solution ifs hacks. master yoda said "quicker, easier, more seductive; not stronger."
magic bit missing shift.
shift command (actually bash shell builtin believe) "rotates" down command line arguments process multiple or none @ cleanly before moving on doing job.
structure this:
note structure doesn't allow arguments after first file. allows user delete file named "--verbose" or "--force" without further complication. alternative, , gnu `rm` does, test leading dash way delete such file full or relative path "./--verbose" - 1 step fancier , allow lone double dash terminate arguments , move on filenames no matter how strange names .code:#pseudo script command line arg processing until [ -z "$1" -o -f "$1" ] case "$1" in "-v"|"--verbose") #be verbose true shift ;; "-f"|"--force") #forceful true shift ;; *) #unrecognized arg shift ;; esac done
in other words, bash logic `rm` behaviour this:
code:while [ "${1:0:1}" = "-" ] #... done
Forum The Ubuntu Forum Community Ubuntu Official Flavours Support General Help [kubuntu] Bash script to move file to the trash
Ubuntu
Comments
Post a Comment