One command ftp with error reporting

In the previous post I’ve wrote a one line ftp control using expect. It looked pretty good and it worked. Then I stumbled upon curl … and I had to rewrite the script. With curl the entire script could be made much simpler and faster. Here it is:


#!/bin/bash
#
# Curl ftp wrapper
#
# ftp to/from <system> as user ftp
 
verbose=0
 
usage() {
    echo "Transfer files using ftp"
    echo "usage: `basename $0` [-v] get <system> <remote file> <local file>"
    echo "       `basename $0` [-v] put <system> <local file> <remote file>"
    exit 1
}
 
if [ "$1" == "-h" -o "$1" == "-?" ]; then
    usage
    exit 1
fi
 
if [ "$1" == "-v" ]; then
    verbose=1
fi
 
num_arg=`expr $# - $verbose`
if [ $num_arg != 4 ]; then
    usage
fi
 
args=("$@")
cmd=${args[`expr $verbose + 0`]}
target=${args[`expr $verbose + 1`]}
file1=${args[`expr $verbose + 2`]}
file2=${args[`expr $verbose + 3`]}
 
if [ "$cmd" != "get" ] && [ "$cmd" != "put" ]; then
    echo "Invalid command '$cmd'"
    usage
fi
if [ "$cmd" == "put" ] && [ ! -e $file1 ]; then
    echo "Source file $file1 doesn't exist"
    exit 1
fi
 
# set the verbose variable
curl_verbose="-s"
if [ $verbose == 1 ]; then
    curl_verbose="-v"
fi
 
if [ $cmd == "get" ]; then
    if [ $verbose == 1 ]; then
        echo "Copying file from $target:$file1 to $file2"
    fi
 
    curl $curl_verbose ftp://$target/$file1 --user ftp:ftp -o $file2
 
else
    if [ $cmd == "put" ]; then
        if [ $verbose == 1 ]; then
            echo "Copying file from $file1 to ftp://$target/$file2"
        fi
 
        curl $curl_verbose -T $file1 ftp://$target/$file2 --user ftp:ftp
 
    else
        echo "Invalid command"
        if [ $verbose == 1 ]; then
            usage
        fi
        exit 1
    fi
fi


References:

Advertisements
This entry was posted in Computers, Linux and tagged , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s