Last active
March 23, 2018 17:16
-
-
Save mihids/09497310bc4719f7820b to your computer and use it in GitHub Desktop.
shell script samples for studying loops and creating removing folders/files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LOGIN_FILE="login" | |
space=" " | |
for i in `seq 1 10`; | |
do | |
CURR_FILE="$LOGIN_FILE""$i" | |
rm $CURR_FILE 2> /dev/null | |
touch $CURR_FILE | |
cat header.in | tr -d '\n' >> $CURR_FILE | |
touch temp.in | |
printf %s%s "Cli$i" "${space:${#i}+3}" >> temp.in | |
cat temp.in | tr -d '\n' >> $CURR_FILE | |
rm temp.in | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
clear | |
_DEBUG="on" | |
function DEBUG() | |
{ | |
[ "$_DEBUG" == "on" ] && $@ | |
} | |
if [ $# -ne 1 ];then | |
echo "Please pass the version number as the first parameter of the script" | |
exit 1 | |
fi | |
declare -a comp_names_arr=( | |
"LSEDM_SYNAPSE_GW" | |
"LSEDM_SOLA_GW" | |
"LSEDM_RTF_GW" | |
"LSEDM_EOD" | |
"LSEDM_MonitorServer" | |
) | |
THIS_DIR=$PWD | |
SVN_IP=10.124.125.185 | |
BINARY_LOCATION="dist/Release/GNU-Linux-x86" | |
BIN_FOLDER="bin" | |
CONFIG_FOLDER="config" | |
SVN_RELEASE_LOCATION="https://subversion.lseg.stockex.local/svn/ClearingGateway/CGTWY_RITA/branches/Drop2_beta/" | |
INSTALLATION_DIR="LSEDM_CGTWY" | |
COMMON_FILES="CommonFiles" | |
INSTALLER="Installer" | |
SHELL_SCRIPTS="ShellScripts" | |
idx=0 | |
INSTALLATION_DIR=$INSTALLATION_DIR"_"$1 | |
mkdir $INSTALLATION_DIR | |
cd $INSTALLATION_DIR | |
echo $1 > version.txt | |
for i in "${comp_names_arr[@]}" | |
do | |
echo "Checking out component $i" | |
mkdir -p $i | |
cd $i | |
rm -rf * | |
svn_modified_path=$SVN_RELEASE_LOCATION$i | |
echo $svn_modified_path | |
svn_modified_path=${svn_modified_path/subversion.lseg.stockex.local/$SVN_IP} | |
svn_modified_path=$svn_modified_path" ." | |
svn checkout $svn_modified_path | |
if [ $? -eq 0 ];then | |
make clean | |
make | |
if [ $? -eq 0 ];then | |
echo "Build Successful" | |
mkdir bin | |
#bin_name=$(echo $i | tr '[:upper:]' '[:lower:]') | |
#cp $BINARY_LOCATION/$bin_name bin/"${lsedm_names_arr[idx]}" | |
cp $BINARY_LOCATION/* bin/ | |
mv $BIN_FOLDER ../ | |
mv $CONFIG_FOLDER ../ | |
rm -rf * | |
rm -rf .dep.inc | |
rm -rf .svn | |
mv ../$BIN_FOLDER . | |
mv ../$CONFIG_FOLDER . | |
else | |
echo "Build Failed for component $i" | |
exit 1 | |
fi | |
else | |
echo "Checkout failed for $i" | |
exit 1 | |
fi | |
cd .. | |
let idx++; | |
done | |
svn_modified_path=$SVN_RELEASE_LOCATION$COMMON_FILES | |
echo $svn_modified_path | |
svn_modified_path=${svn_modified_path/subversion.lseg.stockex.local/$SVN_IP} | |
svn checkout $svn_modified_path | |
if [ $? -ne 0 ];then | |
echo "Checkout failed for $COMMON_FILES" | |
exit 1 | |
fi | |
#copy cdc configs in componenets to CommonFiles folder | |
for i in "${comp_names_arr[@]}" | |
do | |
if [ -d "$i" ]; then | |
countfiles=$(ls -1 "$i"/config/*.cgc 2>/dev/null | wc -l) | |
if [ $countfiles != 0 ]; then | |
echo "Moving all cgc files in $i/config folder to CommonFiles" | |
mv "$i"/config/*.cgc CommonFiles | |
if [ $? -ne 0 ];then | |
echo "Moving cgc file(s) to CommonFiles folder failed" | |
exit 1 | |
fi | |
fi | |
fi | |
done | |
svn_modified_path=$SVN_RELEASE_LOCATION$SHELL_SCRIPTS | |
echo $svn_modified_path | |
svn_modified_path=${svn_modified_path/subversion.lseg.stockex.local/$SVN_IP} | |
svn checkout $svn_modified_path | |
if [ $? -ne 0 ];then | |
echo "Checkout failed for $SHELL_SCRIPTS" | |
exit 1 | |
fi | |
svn_modified_path=$SVN_RELEASE_LOCATION$INSTALLER | |
echo $svn_modified_path | |
svn_modified_path=${svn_modified_path/subversion.lseg.stockex.local/$SVN_IP} | |
svn checkout $svn_modified_path | |
if [ $? -ne 0 ];then | |
echo "Checkout failed for $INSTALLER" | |
exit 1 | |
fi | |
mv $INSTALLER/* . | |
rm -rf $INSTALLER | |
chmod +x install.sh | |
cd .. | |
tar -zvcf $INSTALLATION_DIR".tar.gz" $INSTALLATION_DIR | |
cd $THIS_DIR | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
clear | |
_DEBUG="on" | |
function DEBUG() | |
{ | |
[ "$_DEBUG" == "on" ] && $@ | |
} | |
declare -a comp_names_arr=( | |
"LSEDM_SYNAPSE_GW" | |
"LSEDM_SOLA_GW" | |
"LSEDM_RTF_GW" | |
"LSEDM_EOD" | |
"LSEDM_MonitorServer" | |
) | |
declare -a env_variables_arr=( | |
"LSEDM_MESSAGE_DUMP_PATH" | |
"LSEDM_MESSAGE_DUMP_ARCHIVAL_PATH" | |
"LSEDM_STORING_PATH" | |
"LSEDM_LOG_ARCHIVAL_PATH" | |
) | |
ENV_VAR_SET_SCRIPT="envvareditor" | |
INSTALL_SCRIPT_DIR="install_scripts" | |
THIS_DIR=$PWD | |
INSTALLATION_NAME="LSEDM_CGTWY" | |
VERSION_FILE="version.txt" | |
BACKUP_DIR="Backups" | |
INSTALL_DIR="Installation" | |
COMMON_FILES="CommonFiles" | |
SHELL_SCRIPTS="ShellScripts" | |
now=$(date +'%Y%m%d_%H%M%S') | |
########################################################################################################### | |
#function to run the environment variable editor | |
function runEnvEditor(){ | |
if [ ! -d $INSTALL_SCRIPT_DIR ] ; then | |
echo -e "ERROR - Missing directory $INSTALL_SCRIPT_DIR " | |
exit 1 | |
else | |
cd $INSTALL_SCRIPT_DIR | |
if [ ! -r $ENV_VAR_SET_SCRIPT ] ; then | |
echo -e "ERROR - Missing $ENV_VAR_SET_SCRIPT \n" | |
exit 1 | |
else | |
chmod +x ./$ENV_VAR_SET_SCRIPT | |
./$ENV_VAR_SET_SCRIPT | |
if [ $? -ne 0 ]; then | |
echo $ENV_VAR_SET_SCRIPT" failed" | |
fi | |
source ~/.bashrc | |
cd .. | |
fi | |
fi | |
} | |
########################################################################################################### | |
#function to check whether all environment variables are present | |
function checkEnvVarsSet() | |
{ | |
if [ -z $LSEDM_INSTALLATION_PATH ]; then | |
echo "LSEDM_INSTALLATION_PATH environment variable is not set" | |
return -1 | |
fi | |
for j in "${env_variables_arr[@]}" | |
do | |
if [ -z ${!j} ]; then | |
echo "${j} environment variable is not set " | |
return -1 | |
fi | |
done | |
return 0 | |
} | |
########################################################################################################### | |
#function to backup the existin installation if necessary | |
function backupExistingInstallation() | |
{ | |
line=$(head -n 1 $LSEDM_INSTALLATION_PATH/$INSTALL_DIR/$VERSION_FILE) | |
echo "Do you wish to back up version "$line"? Please enter option 1 or 2" | |
select yn in "Yes" "No"; do | |
case $yn in | |
Yes ) | |
if [ ! -d "$LSEDM_INSTALLATION_PATH/$BACKUP_DIR" ]; then | |
mkdir -p $LSEDM_INSTALLATION_PATH/$BACKUP_DIR | |
fi | |
cd $LSEDM_INSTALLATION_PATH/$BACKUP_DIR | |
backup_dir=$INSTALLATION_NAME"_"$line"_"$now | |
mkdir $backup_dir | |
cp -rf $LSEDM_INSTALLATION_PATH/$INSTALL_DIR $backup_dir | |
cp -rf $LSEDM_INSTALLATION_PATH/$COMMON_FILES $backup_dir | |
rm -rf $LSEDM_INSTALLATION_PATH/$INSTALL_DIR/* | |
if [ $? -eq 0 ] ; then | |
cd $LSEDM_INSTALLATION_PATH/$BACKUP_DIR | |
echo -e "\narchiving the backup..." | |
tar -zcf $backup_dir".tar.gz" $backup_dir | |
if [ "$?" -eq "0" ] | |
then | |
echo -e "SUCCESS !\n" | |
echo -e "Backup archived as ${backup_dir}.tar.gz in $LSEDM_INSTALLATION_PATH/$BACKUP_DIR directory. \n" | |
rm -rf $LSEDM_INSTALLATION_PATH/$BACKUP_DIR/$backup_dir | |
else | |
echo -e "FAILED !\n" | |
echo -e "ERROR - Failed to archive the Installation backup directory $backup_dir !\n" | |
fi | |
else | |
echo -e "ERROR : Backup failed ! \n" | |
exit 1 | |
fi | |
break;; | |
No ) break;; | |
esac | |
done | |
} | |
################## Start of the Exexcution ############################################################### | |
########################################################################################################### | |
#if any of Environment variable is not set direct the user to create environment variables | |
checkEnvVarsSet | |
if [ "$?" -ne "0" ] | |
then | |
runEnvEditor | |
fi | |
#check again envrionement variables are correctly set by the Env var editor | |
checkEnvVarsSet | |
if [ "$?" -ne "0" ] | |
then | |
echo -e "ERROR - Environment variables are not correctly set, terminating the installation ! \n" | |
exit -1 | |
fi | |
########################################################################################################### | |
#getting the installing version | |
if [ ! -r $VERSION_FILE ]; then | |
echo -e "ERROR - The version.txt file is missing, The installation package is not complete ! \n" | |
exit 1 | |
fi | |
INSTALLING_VERSION=$(head -n 1 $VERSION_FILE) | |
echo -e "\nInstalling LSEDM_CLEARING_GATEWAY version: $INSTALLING_VERSION .....\n" | |
echo -e "\nInstalling LSEDM_CLEARING_GATEWAY to : $LSEDM_INSTALLATION_PATH \n" | |
########################################################################################################### | |
#Checking write permissions on installation path | |
mkdir -p $LSEDM_INSTALLATION_PATH/Dummy | |
if [ $? -ne 0 ]; then | |
echo -e "ERROR - Please enable write permission for $LSEDM_INSTALLATION_PATH \n" | |
exit 1 | |
fi | |
rm -rf $LSEDM_INSTALLATION_PATH/Dummy | |
########################################################################################################### | |
#creating the directories in installation path. Back if there are any existing ones | |
if [ ! -d $LSEDM_INSTALLATION_PATH/$INSTALL_DIR ]; then | |
mkdir -p $LSEDM_INSTALLATION_PATH/$INSTALL_DIR | |
echo $LSEDM_INSTALLATION_PATH/$INSTALL_DIR" created" | |
else | |
if [ ! -r $LSEDM_INSTALLATION_PATH/$INSTALL_DIR/$VERSION_FILE ]; then | |
echo "No version file found in ""$LSEDM_INSTALLATION_PATH/$INSTALL_DIR/$VERSION_FILE"", Fresh installation" | |
else | |
echo -e "Existing installation found..." | |
backupExistingInstallation | |
fi | |
fi | |
########################################################################################################### | |
#copying component binaries and configs, | |
cd $THIS_DIR | |
for i in "${comp_names_arr[@]}" | |
do | |
echo "copying "$i "to "$LSEDM_INSTALLATION_PATH/$INSTALL_DIR | |
cp -rf $i $LSEDM_INSTALLATION_PATH/$INSTALL_DIR/ | |
done | |
########################################################################################################### | |
#copying version files, common files and Shell scripts | |
cp $VERSION_FILE $LSEDM_INSTALLATION_PATH/$INSTALL_DIR | |
if [ ! -d $LSEDM_INSTALLATION_PATH/$COMMON_FILES ]; then | |
mkdir -p $LSEDM_INSTALLATION_PATH/$COMMON_FILES | |
echo $LSEDM_INSTALLATION_PATH/$COMMON_FILES" created" | |
cp -rf $COMMON_FILES/* $LSEDM_INSTALLATION_PATH/$COMMON_FILES/ | |
else | |
echo "CommonFiles folder exists, copying only new files" | |
yes n | cp -irf $COMMON_FILES/* $LSEDM_INSTALLATION_PATH/$COMMON_FILES/ 2>/dev/null | |
fi | |
cp -rf $SHELL_SCRIPTS $LSEDM_INSTALLATION_PATH/$INSTALL_DIR | |
cd $LSEDM_INSTALLATION_PATH/$INSTALL_DIR/$SHELL_SCRIPTS/ | |
find . -type f -exec chmod +x '{}' \; | |
########################################################################################################### | |
#creating, log, log arhive, message dump and message dump archive directories | |
echo -e "\n" | |
env_var_no=0 | |
for j in "${env_variables_arr[@]}" | |
do | |
if [ ! -d ${!j} ]; then | |
echo "Creating directory "${!j} | |
mkdir -p ${!j} | |
fi | |
for i in "${comp_names_arr[@]}" | |
do | |
mkdir -p ${!j}/${i} | |
if [ $env_var_no -eq 0 ] ; then | |
echo "Creating directory "${!j}/${i}/incoming | |
mkdir -p ${!j}/${i}/incoming | |
echo "Creating directory "${!j}/${i}/outgoing | |
mkdir -p ${!j}/${i}/outgoing | |
fi | |
# echo "${!j}/${comp_names_arr[i]}/outgoing" | |
done | |
let env_var_no++ | |
done | |
cd $THIS_DIR | |
echo -e "\nInstallation successfully completed !\n" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment