I am effectively making a recycling bin via some scripts I made.
The first script is pretty much an alternative to the rm command (instead of actually deleting a file, it moves it to a deleted folder).
I've managed to allow the script to move multiple files to the deleted folder:
sh moveToBin file1 file2 fil3 (similar to: rm file1 file2 file3)
The start of my first script is:
#!/bin/bash for param in "$@" do .. .. (main part of my code)
One by one, each parameter (file) is moved to the deleted folder. I am now trying to incorporate adding switch -parameters, but I'm not quite sure how to incorporate that.
The above works for
sh moveToBin file1 file2 file3 but how do I incorporate the possibility that the first argument (only the first) COULD be a switch
-i (ask to delete),
-v (confirm deletion),
-iv (ask to delete then confirm deletion).
Hence the switch only applies to
$1. I tried out something called getopts but I'm not familiar with the use. Once a switch is used, this applies to
$2 onwards i.e.
sh moveToBin -i file1 file2
this asks to delete file1, and after I decide, it then asks to delete file2 I thought of something like this, but I doubt it will work. any help?
counter=1 for param in "$@" do while [[ if $param = "-*" && counter -eq1]]; do getopts "iv" arg; case "$arg" in i) read -p "want to delete $param ?" ans if [ ans =~ ^[Y][y] ] then #run main code for that param value fi;; v) #run main code for that param value echo "file @param deleted";; esac counter=$((counter+1)) continue done #run main code for that param value done
The while loop condition means that it is the first parameter and that this parameter starts with a hyphen.