Labels

Android (1) bash (2) boost (2) C (34) C++ (2) cheatsheet (2) CLion (6) css (3) Debian (33) DL (17) Docker (1) Dreamweaver (2) Eclipse (3) fail2ban (4) git (5) GitHub (4) Hacking (3) html (8) http (1) iOS (1) iPad (1) IRC (1) Java (30) javascript (3) Linux (164) Mac (19) Machine Learning (1) mySQL (47) Netbeans (4) Networking (1) Nexus (1) OpenVMS (6) Oracle (1) Pandas (3) php (16) Postgresql (8) Python (9) raid (1) RedHat (14) Samba (2) Slackware (45) SQL (14) svn (1) tar (1) ThinkPad (1) Virtualbox (3) Visual Basic (1) Visual Studio (1) Windows (2)

Thursday 29 September 2022

Updating a bash script from a repository

The following are two methods of updating a bash script from a repository


Firstly, both methods below need the following;

APP_NAME="some_name"
 
# SELF UPDATE STUFF
REPO_URL="https://gist.github.com/xxxxx/"
APP_REPO_ID="xxxxxxxxxxxxx"
OLD_APP_PREFIX="OLD"

We can also add a RUN_PATH variable as required

RUN_PATH="/usr/bin"

This is used in the example of the manual check version below.

To initiate a manual check and update;

The actual update routine;

# SELF UPDATE
function self_update()
{
echo ""
echo "Attempting to get the latest version of $APP_NAME"
git clone $REPO_URL$APP_REPO_ID.git
echo ""
if [ -f "$APP_REPO_ID/$APP_NAME" ]; then
echo "Preparing new version of $APP_NAME"
chmod +x $APP_REPO_ID/$APP_NAME
mv $RUN_PATH/$APP_NAME $RUN_PATH/$OLD_APP_PREFIX.$APP_NAME
cp $APP_REPO_ID/$APP_NAME $RUN_PATH/$APP_NAME
rm -Rf $APP_REPO_ID
echo "Please restart $APP_NAME"
echo ""
exit 0
else
echo "ERROR: Unable to retrieve and/or verify $APP_NAME"
echo "Continuing with existing version..."
fi
}

The check;

if [ -f "$RUN_PATH/$OLD_APP_PREFIX.$APP_NAME" ]; then
echo "--------------------------------------"
echo "Version: $VER successfully installed"
echo "--------------------------------------"
rm $RUN_PATH/$OLD_APP_PREFIX.$APP_NAME
echo ""
read -n 1 -s -r -p "Press any key to continue..."
fi

We need a way to initiate the update, such as an arguament when running the script as below;

if [ "$1" = "update" ]; then
self_update
exit 0
fi


To initiate an automatic check and update;

The actual update routine;

# SELF UPDATE
function self_update()
{
echo ""
echo "Attempting to get the latest version of $APP_NAME"
git clone $REPO_URL$APP_REPO_ID.git
echo ""
if [ -f "$APP_REPO_ID/$APP_NAME" ]; then
echo "Preparing new version of $APP_NAME"
chmod +x $APP_REPO_ID/$APP_NAME
mv $APP_NAME $OLD_APP_PREFIX.$APP_NAME
cp $APP_REPO_ID/$APP_NAME .
rm -Rf $APP_REPO_ID
echo "Please restart $APP_NAME"
echo ""
exit 0
else
echo "ERROR: Unable to retrieve and/or verify $APP_NAME"
echo "Continuing with existing version..."
fi
}

The decision;

function decision()
{
echo ""
echo "============================================================================"
echo "$1"
echo ""
#echo " PRESS c TO CONTINUE, s TO SKIP OR q to QUIT [c/s/q]?"
read -p " PRESS c TO CONTINUE, s TO SKIP OR q to QUIT [c/s/q]? " response
if [ "$response" = "c" ] || [ "$response" = "C" ]; then
echo "----------------------------------------------------------------------------"
return 1

elif [ "$response" = "s" ] || [ "$response" = "S" ]; then
echo "----------------------------------------------------------------------------"
return

else
echo "----------------------------------------------------------------------------"
echo ""
echo "ABORTED BY USER"
exit 0
fi
}

The check;

if [ -f "$OLD_APP_PREFIX.$APP_NAME" ]; then
echo "Skipping update, we already have the latest and greatest..."
rm $OLD_APP_PREFIX.$APP_NAME
else
decision "WOULD YOU LIKE TO GET THE LATEST VERSION OF $APP_NAME FROM IT'S GITHUB REPOSITORY?"
if [ "$?" = 1 ]; then
self_update
fi
fi

No comments:

Post a Comment

Note: only a member of this blog may post a comment.