moaz-scp.sh 1.4 KB

12345678910111213
  1. #!/usr/bin/env bash
  2. if [ "$#" -ne 1 ];then
  3. echo "Error: Invalid number of arguments." >&2
  4. echo "Usage: $0 {install|uninstall}" >&2
  5. exit -1
  6. fi
  7. if [ "$1" == "install" ];then
  8. echo 'function m_scp { [ "$#" -ne 2 ] && ([ "$#" -ne 4 ] || [ "$1" != "-P" ]) && (echo "Invalid options." >&2;echo "Usage: m_scp [-P port] /local/path example.com:/remote/path" >&2;echo "OR m_scp [-P port] example.com:/remote/path /local/path" >&2;return -1;);PORT=22;if [ "$1" == "-P" ];then;([ "$(($1+0))" != "$1" ] || [ "$1" -gt 65535 ] || [ "$1" -lt 0 ]) && (echo "Error: \"$1\" is not a valid port number." >&2;exit -1);PORT=$2;shift 3;fi;if [ $(grep "[:]" <<< "$1" 2>/dev/null) != "" ];then;HOSTNAME=$(sed -e "s/.*^//" -e "s/\:.*//" <<< "$1");REMOTEPATH=$(sed -e "s/.*\://" -e "s/\$.*//" <<< "$1");LOCALPATH=$2;[ -d "${LOCALPATH}" ] && LOCALPATH = "${LOCALPATH}/$(basename ${REMOTEPATH})";(ssh -p ${PORT} "${HOSTNAME}" cat "${REMOTEPATH}" > "${LOCALPATH}" && echo Success.) || (echo Failed. >&2;exit -1);exit 0;elif [ $(grep "[:]" <<< "$1" 2>/dev/null) != "" ];then;HOSTNAME=$(sed -e "s/.*^//" -e "s/\:.*//" <<< "$2");REMOTEPATH=$(sed -e "s/.*\://" -e "s/\$.*//" <<< "$2");LOCALPATH=$1;(ssh -p ${PORT} "${HOSTNAME}" dd of="${REMOTEPATH}" < "${LOCALPATH}" && echo Success.) || (echo Failed. >&2;exit -1);exit 0;fi; }' >> ~/.bashrc
  9. elif [ "$1" == "uninstall" ];then
  10. grep -v 'function m_scp' < ~/.bashrc > ~/.bashrc.1;mv ~/.bashrc.1 ~/.bashrc
  11. fi
  12. exit 0