Bash – automatic downloads script from ftp server and creating alias to it.
Home page Ubuntu C++ Hardware HTML i javascript Java Software SQL

Bash – automatic downloads script from ftp server and creating alias to it.

Added: 2012-07-27

Now a lot of information and programs are stored on an ftp server. Sometimes you need to download the script (file) from the server and create an alias to it. You can do it using command wget and alias, typing in terminal.

Below is script that downloads file to destination /home/[id -un]/bash_script and creates alias to it. The script have two parameters first is link to script on server and this parameter is needed. Second parameter is optional and it is name alias to script. If you do not specify a second parameter, the file will be downloaded only.

If script name is alias_wget.bash, you can use it:

alias_wget.bash [linkToFile] [(optional) nameAlias]

#!/bin/bash

echo "The script download file with bash command and creates an alias to it.";
echo "";

PATH_="/home/"`id -un`"/bash_script";

if [ ! -d $PATH_ ]; then
	echo "Creating folder '"$PATH_"'...";
	mkdir $PATH_
	if [ "$?" -ne "0" ]; then
		echo "Can not create folder!!!"
		exit 240;
	fi
fi

if [ "$1" = "" ]; then
	echo "";
	echo "The link is not specified!!!";
	exit 102;
fi

NAME_FILE=`basename $1`;

wget -O $PATH_/$NAME_FILE $1
if [ "$?" -eq "0" ]; then
       	echo "Download file is ended.";
else 
        echo "Can not download file '"$1"'!!!";
       	exit 44;
fi

chmod 755 $PATH_/$NAME_FILE

if [ "$2" != "" ]; then
echo "Creating alias...";
sudo sh -c "echo alias $2=\'$PATH_/$NAME_FILE\' >> ~/.bash_aliases"
source ~/.bashrc
fi

echo "End script.";

exit 0;