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.