Last active
February 28, 2017 05:52
-
-
Save castellanprime/5aff1a8f6b49d4402c46f23720e36295 to your computer and use it in GitHub Desktop.
Helper library for my user-defined function (cpRecent/ mvRecent)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @license: | |
* The MIT License (MIT) | |
* | |
* Copyright (c) 2017 David Okusanya | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* | |
* | |
* | |
* | |
*/ | |
#!/bin/bash | |
# File: helperlib.sh | |
# Brief: Library for cpRecent, mvRecent commands | |
# Error codes | |
no_args="You need to pass in an argument" | |
invalid_option="Invaild option:" | |
no_directory="No directory found" | |
# Return values | |
fullpath= | |
directories= | |
numfiles= | |
interactive= | |
typeset -a files | |
typeset -A filelist | |
# Advise that you use relative paths | |
__returnfullpath(){ | |
local npath | |
if [[ -d $1 ]]; then | |
cd "$(dirname $1)" | |
npath="$PWD/$(basename $1)" | |
npath="$npath/" #Add a slash | |
npath="${npath%.*}" #Delete . | |
fi | |
fullpath=${npath:=""} | |
} | |
__usage(){ | |
cat <<End-Of-Message | |
_______________________________________________________________________________ | |
Copies/Moves the n most recent file(s) in a directory to another directory where | |
n is user specified. | |
<cpRecent/mvRecent> [-d "D1,D2"] [-n NUM] [-ih] | |
-d "D1,D2" The directory that is copied/moved from is D1 while | |
the directory that is copied/moved to is D2 | |
The directories would need to be in relative paths | |
-n This specifies an integer num of files | |
-i This allows the user to edit the name of the file | |
to be copied or moved | |
-h Shows this entire wall of text | |
An example of how to use the command: | |
cpRecent -d "Documents,." -n 3 | |
(This means that copy the three(3) most recent files in Documents to | |
the folder I am currently in) | |
_______________________________________________________________________________ | |
End-Of-Message | |
} | |
__processoptions(){ | |
OPTIND=1 | |
while getopts ":d:n:ih" opt; do | |
case $opt in | |
d ) IFS=',' read -r -a directories <<< "$OPTARG";; | |
n ) numfiles=$OPTARG;; | |
i ) interactive=1;; | |
h ) __usage; return 1;; | |
\? ) echo "$invalid_option -$OPTARG" >&2 ; return 1;; | |
: ) echo "$no_args"; __usage >&2 ; return 1;; | |
* ) __usage >&2; return 1;; | |
esac | |
done | |
shift "$((OPTIND-1))" | |
# Check for errors | |
(( ${#directories[@]} != 2 )) && echo "$invalid_option Number of directories must be 2" && return 2 | |
__returnfullpath "${directories[0]}" | |
directories[0]="$fullpath" | |
__returnfullpath "${directories[1]}" | |
directories[1]="$fullpath" | |
if [[ -z ${directories[0]} || -z ${directories[1]} ]]; then | |
echo $no_directory | |
return 3 | |
fi | |
[[ numfiles != *[!0-9]* ]] && echo "$invalid_option Number of files cannot be a string" && return 4 | |
(( $numfiles == 0 )) && echo "$invalid_option Number of files cannot be zero" && return 4 | |
return 0 | |
} | |
__getrecentfiles(){ | |
local num="-"$numfiles"" | |
# Get the requested files in directory(skips directories) | |
if [[ -n "$(ls -t ${directories[0]} | head $num)" ]]; then | |
# For some reason using local -a or declare -a does not seem to split the string into two | |
local tempfiles=($(ls -t ${directories[0]} | head $num)) | |
for index in "${!tempfiles[@]}"; do | |
echo $index ${tempfiles[index]} | |
[[ -f "${directories[0]}${tempfiles[index]}" ]] && files+=("${tempfiles[index]}") | |
done | |
fi | |
return 0 | |
} | |
__processlines(){ | |
local name | |
local answer | |
if [[ -n $interactive ]]; then | |
for index in "${!files[@]}"; do | |
name=${files[index]} | |
read -n 1 -p "Old name: $name. Do you wish to change the name(y/n)?" answer | |
# Need to leave a space in between the variables | |
echo | |
[[ "$answer" == "y" ]] && read -p "Enter new name:" name | |
local dirFrom="${directories[0]}${files[index]}" | |
local dirTo="${directories[1]}$name" | |
filelist+=(["$dirFrom"]="$dirTo") | |
done | |
else | |
for index in "${!files[@]}"; do | |
local dirFrom="${directories[0]}${files[index]}" | |
local dirTo="${directories[1]}${files[index]}" | |
filelist+=(["$dirFrom"]="$dirTo") | |
done | |
fi | |
return 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment