Added new cn-scp.sh and enabled extglob

This commit is contained in:
David Thurstenson 2019-12-16 14:49:59 -06:00
parent 3e260b5876
commit 17e0a109e0
2 changed files with 66 additions and 0 deletions

View File

@ -14,6 +14,9 @@
# If a directory is given without any command, CD into it. # If a directory is given without any command, CD into it.
shopt -s autocd shopt -s autocd
# Enable extglob
shopt -s extglob
### OTHER CONFIG ### ### OTHER CONFIG ###

63
.bashrc.d/cn-scp.sh Normal file
View File

@ -0,0 +1,63 @@
cn-scp() {
shopt -q extglob; local extglob_set=$?
if ((extglob_set)); then
echo "cn-scp: extglob must be set for this function to work properly."
return 1
fi
local source="$1"
local dest="$2"
local sep=":"
local host sourcefile destfile
if [[ -z $source ]]; then
echo "Error: No source provided"
return 1
elif [[ -z $dest ]]; then
echo "Error: No destination provided"
return 1
fi
# Host determination:
local loopcount=0
for i in "$source" "$dest"; do
case "$i" in
# Will match if $sep exists in string once
*@("$sep")* )
# Check if $host is unset
if [[ -z $host ]]; then
host="${i%%:*}"
sourcefile="${source#*:}"
destfile="${dest#*:}"
# If this is the first run of the loop, the remote is the source
if [[ "$loopcount" == 0 ]]; then
operation="get"
# If this is the second run of the loop, the remote is the dest
elif [[ "$loopcount" == 1 ]]; then
operation="put"
# If this is anything other than the first or second loop, GTFO
else
echo "Error: Loop counting error."
return 1
fi
continue
else
echo "Error: Remote path for *both* source and destination is not supported."
return 1
fi
;;
*)
((loopcount++))
continue
;;
esac
((loopcount++))
done
# END: Host determination
printf '%q %q %q' "$operation" "$sourcefile" "$destfile" | cnsftp "$host"
}