Below is my script code.which shows the environment name and then fetch the file from the ftp server but I am facing one issue.The script should be run in both way.We can pass the arguments with script and select the environment name then file name.Here the issue is I am not able to list the files which are residing on the ftp server server so that the user can select the file.
#!/bin/bash
HOST='ftp.npq.net'
USER='xyz'
PASSWD='abc'
UNZIP='unzip $FILE'
LIST='ls -al'
#File_List='`ls -al`'
export Env="?TBD?";
## Set the following variable.
##
set_Env ()
{
## Fetching the Env from where the Build deliverable requiered (last option).
prompt_Env;
}
prompt_Env ()
{
echo -e "\nSelect the Enviornment Name\n";
sleep 1;
select Env in dv pf qa ig pp pn exit;
do
if [[ -n ${Env} ]]; then
echo -e "\n\n - You selected: \"${Env}\"\n"
break;
else
echo -e "\n\n - Invalid selection \"${REPLY}\"...Please try again.\n";
fi
done
if [[ ${Env} == "exit" ]]; then
summary=$(
echo -e "\n#################### FTP process - Aborted ####################\n";
echo -e "Warning: User gracefully selected \"${Env}\" option to abort the script operation. Exiting with exit_code=151.";);
echo "${summary}";
exit 0;
fi
}
#prompt_env;
#echo -e "Enter the Name of the file:";
#read FILE;
#DIRECTORY=TRAM/"${Env}"
#if [[ -z ${1} ]]; then
#prompt_env;
#fi
#set "${@}";
for ((i=1;$#>=$i;i++))
do
argn="\${"$i"}";
argn="$(eval echo $argn)";
case "${argn%%=*}=" in
"Env=")
Env="${argn#*=}";
Env="${Env:=?TBD?}";
;;
esac
done
## Check arguments and set them if reqd.
## -------------------------------------
for arg in "Env=${Env}";
do
## Get Var=Val pair
var="${arg%=*}";
val="${arg#*=}";
## Check if mandatory params are not valid, set them if reqd.
if [[ "${val}" == "?TBD?" ]]; then
set_${var};
fi
done
DIRECTORY=TRAM/"${Env}"
echo -n "ENTER the name of the file:";
read FILE
ftp -n $HOST << EOT
ascii
user $USER $PASSWD
cd $DIRECTORY
$LIST
get $FILE
bye
EOT
unzip $FILE
rm $FILE