Bash Installer/Checker
This bash script checks all installations and installs programs if they are missing
In this script I used many key features of bash which helped me learn a lot about it:
- If statements: These were necessary to simply check if things were installed
- Checking for files and directories: I used this in conjunction with if statements to determine if both repositories & programs were installed
- Git Command Line Interface: In order to use git in my script, I had to use the Git Command Line Interface to update and clone the repository
- Putting command output to files: Primarily I did not want to clog the output so I put the output from commands I was running into /dev/null
-
Piping from one command to another: A lot of times I had to parse/check the output from a specific command so I had to pipe it to the
grep
orawk
commands - Variables: Storing data such as version numbers were useful to keep in variables
- Running commands in Bash Scripts: In order to conduct installs in the case that packages weren't installed, I had to run commands inside my bash script, and I also sometimes had to put results from commands into variables
echo "Welcome to CSA Installer. Note that installations may require sudo permissions & password to be entered"
# make sure in home directory of user
cd ~
# check anaconda installation
if command -v anaconda >/dev/null; then
anaconda_version=`conda --version | awk -F " " '{print $2}'`
echo "Anaconda installed (Version $anaconda_version)"
else
echo "Installing Anaconda"
wget https://repo.anaconda.com/archive/Anaconda3-2022.05-Linux-x86_64.sh
./Anaconda3-2022.05-Linux-x86_64.sh
fi
# check python 3 installation
if python3 --version | grep "Python 3" >/dev/null; then
python3_version=`python3 --version | awk -F " " '{print $2}'`
echo "Python $python3_version installed"
else
echo "Installing Python 3"
sudo apt install python3 python3-pip
fi
# check python 2 installation
if python2 --version 2>&1 | grep "Python 2" >/dev/null; then
python2_version=`python2 --version 2>&1 | awk -F " " '{print $2}'`
echo "Python $python2_version installed"
else
echo "Installing Python 2"
sudo apt install python2
fi
#check java installation and java version
java_version=`java --version | head -n 1 | awk -F " " '{print $2}' | awk -F "." '{print $1}'`
if [[ $java_version -ge 11 ]]; then
echo "Java installed with Version 11 or Better (Version $java_version)"
else
echo "Installing Java Version 11 (Version too low or Java not installed)"
sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt update
sudo apt install openjdk-11-jdk
fi
#check jupyter installation
if [[ -f ~/anaconda3/bin/jupyter ]]; then
jupyter_version=`jupyter --version | grep jupyter_core | awk -F ": " '{print $2}'`
echo "Jupyter installed (Version $jupyter_version)"
else
echo "Installing jupyter"
~/anaconda3/bin/conda install jupyter
fi
#check bash kernel installation
if ~/anaconda3/bin/jupyter kernelspec list | grep bash >/dev/null; then
echo "Bash Kernel Installed"
else
echo "Installing Bash Kernel"
pip3 install bash_kernel
python3 -m bash_kernel.install
fi
#check java kernel installation
if ~/anaconda3/bin/jupyter kernelspec list | grep java >/dev/null; then
echo "Java Kernel Installed"
else
echo "Installing Java Kernel"
wget https://github.com/SpencerPark/IJava/releases/download/v1.3.0/ijava-1.3.0.zip
unzip ijava-1.3.0.zip
python3 install.py --user
fi
#create vscode directory if necessary
if [[ ! -d ~/vscode ]]; then
echo "Creating vscode directory"
mkdir vscode
fi
#clone APCSA repository if necessary
if [[ ! -d ~/vscode/APCSA ]]; then
echo "Cloning APCSA repository"
cd vscode
git clone https://github.com/nighthawkcoders/APCSA 2>/dev/null
cd ..
fi
#update APCSA repository
echo "Updating APCSA repository"
cd vscode/APCSA
git pull 2>/dev/null
cd ../..