view bin/xcp @ 0:2aa53823fb21

new repo
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Sat, 06 Jun 2026 10:19:04 +0100
parents
children
line wrap: on
line source

#!/usr/bin/bash
usage=$'xcp host:[dir] files... [-r newName] # down
 or
xcp files... [-r newName] host:[dir] # up
-r will rename the file after transfer, must be only one file!
 dir is either absolute or will start from home'
case $1 in
 -*|'') echo "$usage" 1>&2; exit 1;;
esac
case "$1" in
 *:*)
 remote="$1"; shift
 download=1
 ;;
esac
if [ "$remote" ] ; then
    bb=$(($# - 1))
    if [ "${!bb}" = "-r" ]; then
	newname=${!#}
	set -- "${@: 1: $#-2}"
	if [ $# -ne 1 ]; then
	    echo "one file only allowed with rename!" "$@" 1>&2
	    exit 3
	fi
    fi
else
 case "${!#}" in
  *:*) remote="${!#}"
       set -- "${@: 1: $#-1}"
       bb=$(($# - 1))
       if [ "${!bb}" = "-r" ]; then
	   newname=${!#}
	   set -- "${@: 1: $#-2}"
           if [ $# -ne 1 ]; then
	      echo "one file only allowed with rename!" "$@" 1>&2
	      exit 3
           fi
       fi
       ;;
  *) echo "no remote spec.???" 1>&2; echo $usage 1>&2; exit 2;;
 esac
fi
IFS=: read host dir < <(echo "$remote")
cd="set cmd:fail-exit true; "
if [ "$dir" ] ; then
  cd="${cd}cd \"$dir\"; "
fi
if [ "$download" ] ; then
 # get files from host
 if [ "$newname" ] ; then
     doit="${cd}get $(printf \ \'%s\' "$@") -o '$newname'; quit"
 else
     doit="${cd}mget $(printf \ \'%s\' "$@"); quit"
 fi
else
 # put files to host
 if [ "$newname" ] ; then
     doit="${cd}put $(printf \ \'%s\' "$@") -o '$newname'; quit"
 else
     doit="${cd}mput $(printf \ \'%s\' "$@"); quit"
 fi
fi

echo lftp -e "$doit" sftp://${host}: 1>&2
lftp -e "$doit" sftp://${host}:
res="$?"
if [ "$res" = 0 ]
then
 echo success
else
 echo failed: $res
fi