J. R. Swab

How To Increase Efficiency With Bash Automation

Categories: [Technology]
Tags: [open-source], [linux], [bash]

As you well know by now, I am a big nerd. Not the broken glasses and suspender-wearing kind but the learn as much about computers as I can kind. The more I learn, the more I find myself moving away from large desktop environments and window managers to the terminal. The terminal is fast, efficient, and does not hog system resources.

It is to a point now that if I can do it in the terminal then I do it in the terminal. Of course, some tasks are not well suited for a terminal. Just because I can read a webpage via the terminal does not mean it's the best tool for the job. The majority of websites use so much CSS and javascript that it becomes a nightmare to navigate through.

That being said, most of my workflow is more efficient in the terminal via a headless server. No need for a super expensive laptop, all I need is one that can connect to the server of my choice. After a bit of tweaking the environment is exactly as I like it to be. Every aspect down to the color scheme and keyboard layout.

However, over time this simple tweaking has become a healthy chunk of my time to both set up and update. New servers need all the goods, and even with all the dotfiles in place on my GitLab account I still need to take time setting everything up. Installing packages and symlinking configuration files takes more time the more I use the terminal.

Automation Through Bash Scripting

We'll cover Bash in a mini-series here soon so if you are new to you'll have a good starting point. Since I only ever use Linux, I can use these types of scripts on all my machines. As you can see on my GitLab page, I already have dotfiles for my desktop and laptop (which will merge soon) and most recently a repository for my server configuration.

This is because I have only recently gotten into heavily customizing my terminal experience to improve my productivity. I guess we can say that forcing myself to learn Vim started off the journey in its current form since that got me into editing the .vimrc file. Add in the move to i3wm for my desktop computer, and the ball is now rolling in full force.

This bash script is still in the works, so there are only a few lines I am able to share with you, but I think it will get your gears turning. Having the mindset to automate as much of my computing life as possible saves a lot of time. With this time saved, I am able to blog, read more Linux books, and have a life.

JK, I have no life...

Parts of my script

if [[ ! -d ~/.config/nvim/colors/ ]]; then
   mkdir ~/.config/nvim/colors/

Here I tell the script that if the directory ~/.config/nvim/colors/ does not exist then create the directory by running mkdir ~/.config/nvim/colors/. Then I do it again for the .bashrc. file.

if [[ ! -f ~/.bashrc ]]; then
   touch ~/.bashrc

The reason why I have the script create these files and directories (and more as I flesh this out) is to make sure the needed information is there for the symlinks I automate at the end of the bash script. After this I have the script add two lines to the end of the .bashrc file.

echo "alias vim='nvim'" >> .bashrc
echo "alias tmux='tmux -2'" >> .bashrc
source ~/.bashrc

These lines may be replaced with a symlink to a completed .bashrc file as I tweak the script because as I add more to the server's .bashrc it could make the script a bit messy. The next section I have so far is to install git and pull down a Vim, plugin manager.

sudo apt install git

git clone https://github.com/VundleVim/Vundle.vim.git ~/.config/nvim/bundle/Vundle.vim

I plan to have the script check the distribution of Linux before installing git so that I do not have to edit this file between servers running Ubuntu and Arch. Then we have the symlinks:

ln -sf ~/dotfiles/vim/colors/molokai.vim ~/.config/nvim/colors/molokai.vim
ln -sf ~/dotfiles/vimrc.main ~/.config/nvim/init.vim
ln -sf ~/dotfiles/tmuxConfig.main ~/.tmux.conf

A symlink tells the computer to use the first file in each line and make the second file link back to the first. This is handy when customizing the configuration files for many programs. Instead of having to edit them every time by hand we can have a master copy to edit and tweak over time.

If you have anyquestions feel free to ask on Mastodon or DM me on Discord.

Ways to support the blog.

If you are an email kind of nerd you can sign up for mine here. You can donate to this site from my Liberapay account if you so choose. If you want a more passive way to support this site, use this link when shopping on Amazon; it kicks some of Amazon's profit to me at no extra cost to you.