diff --git a/.bashrc b/.bashrc index a1cd7c2..34db32e 100644 --- a/.bashrc +++ b/.bashrc @@ -14,6 +14,9 @@ # If a directory is given without any command, CD into it. shopt -s autocd +# Enable extglob +shopt -s extglob + ### OTHER CONFIG ### diff --git a/.bashrc.d/cn-scp.sh b/.bashrc.d/cn-scp.sh new file mode 100644 index 0000000..aac01a5 --- /dev/null +++ b/.bashrc.d/cn-scp.sh @@ -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" + +}