Skip to content

Instantly share code, notes, and snippets.

@ebsaral
Last active December 20, 2015 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ebsaral/6212510 to your computer and use it in GitHub Desktop.
Save ebsaral/6212510 to your computer and use it in GitHub Desktop.
#!/bin/bash
echo "*** WELCOME TO FTP UPLOAD SCRIPT written by Emin Bugra Saral ***"
echo
# Variables
SOURCE_PATH="$1"
TARGET_PATH="$2"
FTP_SERVER_FILE="$(basename $0)_server.txt" # Info file's name
FTP_SERVER_FILE_DIR="$(pwd)/$FTP_SERVER_FILE" # Info file's directory which is current one
# Upload selector
upload()
{
if [ $1 == "file" ]; then
upload_file
elif [ $1 == "dir" ]; then
upload_dir
fi
}
# Upload a file
upload_file()
{
echo "Attemting to upload the file"
TARGET_PATH=$(basename $TARGET_PATH)
ftp -n $SERVER <<EOF
user $USERNAME $PASSWORD
binary
mkdir $TARGET_PATH
put $SOURCE_PATH $TARGET_PATH/$(basename $SOURCE_PATH)
bye
EOF
}
# Upload a folder (may not work recursively so may not upload folder(s) inside the given folder)
upload_dir()
{
echo "Attempting to upload the folder"
ftp -n $SERVER <<EOF
user $USERNAME $PASSWORD
binary
mkdir $TARGET_PATH
cd $TARGET_PATH
lcd $SOURCE_PATH
prompt
mput *
prompt
cd ..
bye
EOF
}
# Prompt user for info
ask_info()
{
echo -n "Server name (or ip address): "
read SERVER
echo -n "Username: "
read USERNAME
echo -n "Password: "
read -s PASSWORD
}
# Read variables from info file
gather_info()
{
SERVER=`sed -n '1p' $FTP_SERVER_FILE`;
USERNAME=`sed -n '2p' $FTP_SERVER_FILE`;
PASSWORD=`sed -n '3p' $FTP_SERVER_FILE`;
}
# Write variables into info file
write_data()
{
echo $SERVER > $FTP_SERVER_FILE
echo $USERNAME >> $FTP_SERVER_FILE
echo $PASSWORD >> $FTP_SERVER_FILE
}
# Say goodbye
end_script()
{
echo "Execution ended"
echo "Goodbye"
exit
}
# Check if the parameters are satisfied
if [ -z "$SOURCE_PATH" ] || [ -z "$TARGET_PATH" ] ; then
echo "Source and/or target directory is missing."
echo "The usage is: \"$(basename $0) source_dir target_folder\""
else
# Data exists and not empty
if [ -s "$FTP_SERVER_FILE" ]; then
gather_info
echo "Server: $SERVER Username: $USERNAME"
echo "Do you want to update connection information? [y/n]"
read USER_OPTION
if [ $USER_OPTION == "y" ] || [ $USER_OPTION == "Y" ]; then
ask_info
write_data
echo
echo "Information is updated."
echo
fi
# No data file
else
# Create it
touch $FTP_SERVER_FILE
# If you cannot, then quit
if [ ! -f "$FTP_SERVER_FILE" ]; then
echo "Cannot create information file. Check permissions."
end_script
fi
echo "Server info file created at \"$FTP_SERVER_FILE_DIR\""
echo "Please enter connection information"
ask_info
write_data
gather_info
echo "Information is added to \"$FTP_SERVER_FILE\"."
echo "Server: $SERVER, Username: $USERNAME"
echo
fi
echo "Accessing to $SOURCE_PATH"
# Determine the type of source directory and call necessary function
if [ -d "$SOURCE_PATH" ] ; then
echo "Access is successful"
upload dir
elif [ -f "$SOURCE_PATH" ] ; then
echo "Access is successful"
upload file
else
echo "Directory is not valid"
end_script
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment