I recently made a blog post about automating tasks with bash scripts and why it can be useful. I just finished a script that can (mostly) automate my setup in Linux.
The script is called setup.sh, and can be found on my Github repo.
Virtualbox: My Testing Environment
I wanted to test my script in a controlled environment, and decided to use Virtualbox to setup a virtual environment to work in. Virtualbox also gave me the option to create snapshots of my virtual machine. When my script made changes to my machine, I could easily go back to a previous version and undo the changes.
Here is a good resource if you want to learn more about virtual machines.
Vagrant: Automated Virtual Machines
I didn't want to go through the process of setting up a virtual machine and having to install an operating system manually, so I automated this with Vagrant. I didn't know anything about Vagrant before this project, but I found a video on YouTube that did a good job at helping me get started: Automated virtual machine deployment with Vagrant
Vagrant Boxes
Vagrant uses "boxes", which is a package format that is used to setup the environment. Here is the one I used:
Using the command vagrant init ubuntu/focal64 created a Vagrantfile with the necessary configurations to get my machine up and running.
After making changes to my Vagrantfile, I tried running the vagrant up command, and got an error:
Vagrant was having trouble starting the virtual machine from the command line, so I started it manually through the Virtualbox GUI, and that worked.The setup script is simple; lets take a look at it:
1. First I start by updating my repositories. Repositories are a useful resource in Linux distributions that allow us to search and download applications. Making sure these are up to date is important before upgrading our system. The sudo command allows a user to run a command with root privileges. Windows users might know this as Administrator privileges. Apt is the package manager for my operating system.2. After updates are done, I install any software that I need.
3. These two commands look confusing, so here is a breakdown at whats going on:
the ln command can create links between files, or in other words, a shortcut. My original file vimrc is located in the directory (folder) called dotfiles, which is inside a directory called linux_files. The symbol ~/ at the beginning of the command just refers to my users home directory (Here you'll have your pictures, downloads, documents, etc). Here is the command in plain English:
ln command /path/to/original_file /path/to/my_shortcut.
The echo command prints out text to my terminal. It can be a useful way to give yourself context to what's going on.
Here are some additional resources if you want to learn more:
Running The Script
At the beginning of this blog post, I said that this script would mostly automate my setup. The reason for this is because when I install Oh My Zsh, it has to run its own script that the user has to interact with. I have the installation towards the end of my script, so it isn't a big deal for now. After making revisions, the script was ready to run:
The script ran successfully! That's it for now. Give bash scripting a shot; you might like it.







Comments
Post a Comment