My script templates and more


Tips and tricks

- Use $0 to see the name of the script.
for example: running echo "$0" in a script called test.sh will output 'test.sh'

- Use $# to see the number of aruments passed to a script.
for example: running echo "$#" in a script called test.sh and running it like this: ./test.sh --arg1 --arg2 will output '2' because 2 arguments where passed when running the script.

Quick uninstall script template

NOTES:
- change APP with the name of the app
- change command 1, command 2 etc. with the commands to you want to run.

#!/bin/bash

##########################################################
# script by Itai-Nelken - https://github.com/Itai-Nelken #
##########################################################

echo "uninstalling APP..."
command 1
command 2
command 3

read -p "do you want to DO SOMETHING (y/n)?" choice
case "$choice" in 
  y|Y ) command 4 ;;
  n|N ) command 5 ;;
  * ) echo "invalid";;
esac
echo "DONE!"

Quick install script template

NOTES:
- change APP with the name of the app
- change command 1, command 2 etc. with the commands to you want to run.

#!/bin/bash

##########################################################
# script by Itai-Nelken - https://github.com/Itai-Nelken #
##########################################################

echo "installing APP..."

#determine if host system is 64 bit arm64 or 32 bit armhf
if [ ! -z "$(file "$(readlink -f "/sbin/init")" | grep 64)" ];then
  #install 64bit version
  echo "installing arm64 version..."
  commands 1
  command 2
elif [ ! -z "$(file "$(readlink -f "/sbin/init")" | grep 32)" ];then
  #install 32bit version
  echo "installing armhf version..."
  command 3
  command 4
else
  echo -e "$(tput setaf 1)$(tput bold)Can't detect OS architecture! something is very wrong!$(tput sgr 0)"
  exit
fi
echo "DONE!"

Example for checking if package is installed, if yes install it else do something else. command -v works as well.

NOTES:
- change PACKAGE with real package name.
- change sudo apt install -y PACKAGE with any commands needed to install the package.

if ! command -v PACKAGE > /dev/null; then
   sudo apt install -y PACKAGE
else
    echo "PACKAGE already installed..."
fi

Example functions for Downloading .deb files from latest github release

NOTES:
- example: change *.armv7l.deb to *.txt to download only .txt files from latest release.

armv7l:

function download_latest_release_armhf() {
  echo "Downloading latest armhf release..."
  curl -s https://api.github.com/repos/$1/releases/latest \
  | grep "browser_download_url.*armv7l.deb" \
  | cut -d : -f 2,3 \
  | tr -d \" \
  | wget -qi -
}

arm64:

function download_latest_release_arm64() {
  echo "Downloading latest arm64 release..."
  curl -s https://api.github.com/repos/$1/releases/latest \
  | grep "browser_download_url.*armv64.deb" \
  | cut -d : -f 2,3 \
  | tr -d \" \
  | wget -qi -
}

Usage: $ download_latest_release_ARCH "github-user-name/repo-name" change ARCH with or armv7l or arm64 depending on function you want to use

#determine if host system is 64 bit arm64 or 32 bit armhf
if [ ! -z "$(file "$(readlink -f "/sbin/init")" | grep 64)" ];then
  arch=64
elif [ ! -z "$(file "$(readlink -f "/sbin/init")" | grep 32)" ];then
  arch=32
else
  error "Failed to detect OS CPU architecture! Something is very wrong."
fi

Example updater for project on github

NOTES:
- change $DIRECTORY to your projects local folder cloned with git.
- change GITHUB-USER-NAME with your actual github user name
- change repo-name & REPO-NAME with your repo's name.
- error function is the function that prints errors in red and exits.


#!/bin/bash

#error function
function error {
  echo -e "\e[91m$1\e[39m"
  exit 1
}

echo "Checking for updates..."
cd $DIRECTORY
localhash="$(git rev-parse HEAD)"
latesthash="$(git ls-remote https://github.com/GITHUB-USER-NAME/repo-name HEAD | awk '{print $1}')"

if [ "$localhash" != "$latesthash" ] && [ ! -z "$latesthash" ] && [ ! -z "$localhash" ];then
    echo "Out of date, updating now..."
    git clean -fd
    git reset --hard
    git pull https://github.com/GITHUB-USER-NAME/REPO-NAME.git HEAD || error 'Unable to update, please check your internet connection'
else
    echo "Up to date."
fi