takarajapaneseramen.com

Mastering Bash Scripting: A Must-Have Skill for Linux Users

Written on

Chapter 1: Understanding Bash Scripting

If you're a Linux user, it's likely that you often find yourself repeating the same tasks. Why not delegate these actions to your digital assistant? Repetitive processes that follow a certain pattern are ideally suited for automation with scripting. This is where bash scripting comes into the picture, as its straightforward syntax makes it accessible for beginners.

Now, you might wonder why learning bash scripting is worthwhile and what practical applications it offers. Let's delve into that.

Bash, which stands for "Bourne Again SHell," is a command language used in Unix-based environments. If you're using a Mac or Linux, you may be familiar with the 'terminal.' While programmers appreciate it, many newcomers find it daunting. The terminal lacks a graphical interface, presenting text that can seem cryptic to those unfamiliar with shell commands. Essentially, bash is the means by which you communicate with your machine.

Originally developed by Brian Fox in 1989, bash was designed to replace the Bourne shell and is now a fundamental tool for automating tasks in the terminal.

At this point, some of you might be thinking, "Why should I invest time in bash scripting when I can achieve similar results with Python?" I had the same thought and initially pursued Python. However, I quickly encountered frustrations that led me to reconsider my approach.

When I use my computer, I typically engage in three primary activities: writing, coding, or working on my Ph.D. Each task requires me to open a specific set of applications based on my objectives. Whether I'm on my MacBook Pro or my university's Dell laptop, both systems run on Unix, which means the scripts I create will be nearly identical.

Writing

During my writing sessions, I generally launch my preferred email client, listen to music, and have several browser tabs open for Google, Grammarly, Medium, Obsidian, Notion, and my writing software, Ulysses. I aimed to create a script to automatically open these applications and sync my GitHub repository. My Ulysses app syncs effortlessly via iCloud.

Coding

When coding, I typically use Visual Studio Code for my project Notaria. I also need my email client, music for inspiration, and a browser with tabs open for Google and Stack Overflow. Keeping my local repository updated is crucial, requiring me to download the latest changes from GitHub and upload my updates once I finish working in Visual Studio Code.

Working

In my working hours, MATLAB takes up most of my time. Similar to coding, I need music, access to both my personal and university email, and a browser for Google, along with my GitLab repository. Just like in coding, I have to ensure my simulations are current by syncing them as needed.

After some initial struggles with Python, I decided to explore bash scripting, which I had some familiarity with from installing Flutter on both Linux and macOS. After watching a few tutorials, I was inspired to create bash scripts that would streamline my workflow. Within half an hour, I had crafted scripts for each operating system, starting with macOS.

Writing Script Example

#! /bin/bash

open -a music

open -a mail

open -a Obsidian

open -a Notion

cd

cd Documents/Writing

git pull origin master

open -a UlyssesMac

if pgrep -x "Ulysses/Mac" > /dev/null

then

echo "Running"

else

echo "Commit message:"

read commit_m

git add -A

git commit -m "$commit_m"

git push

fi

Coding Script Example

#! /bin/bash

open -a music

open -a mail

cd

cd Documents/Coding/Notaria

git pull origin main

code

if pgrep -x "code" > /dev/null

then

echo "Running"

else

echo "Commit message:"

read commit_m

git add -A

git commit -m "$commit_m"

git push

fi

Working Script Example

#! /bin/bash

open -a music

open -a mail

cd

cd Documents/MATLAB/dg_lvn

git pull origin main

open -a MATLAB_R2021b

if pgrep -x "matlab" > /dev/null

then

echo "Running"

else

git add -A

echo "Commit message:"

read commit_m

git commit -m "$commit_m"

git push

fi

All scripts are conveniently located in my home directory, allowing me to execute them without navigating through folders. To ensure that these scripts run smoothly, I make them executable with the command chmod +x script_name.

So, what does my workflow look like today? I simply open the terminal and run the script with ./script_name.sh. Then I select a music playlist, check my emails, and switch between Ulysses, Visual Studio Code, or MATLAB as needed. Once I complete my tasks, I close everything and enter a commit message. Voilà!

Does bash scripting genuinely enhance my productivity? That might be up for debate. However, it has simplified the process of syncing with GitHub, alleviating one more concern from my mind. Ultimately, the experience has been invaluable.

For those interested in further honing their skills, I encourage you to check out the following videos:

This video discusses why developers and testers should learn Linux and shell scripting, emphasizing its significance in the tech world.

This comprehensive guide for beginners introduces bash scripting and offers valuable insights to get started effectively.

Thank you for reading! If you found this content helpful, consider giving a clap 👏 and follow me on Medium and Twitter for more insightful posts. Should you have any feedback, questions, or collaboration ideas, feel free to reach out via DM on Twitter or email me at [email protected]. Your support means a lot to me!

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Hyper Minds Seeking Equilibrium: Navigating Intelligence and Overwhelm

Exploring the challenges and opportunities of hyper-perception in a fast-paced world.

Unlocking Focus: 5 Essential Books for a Meaningful Life

Discover five transformative books that will sharpen your focus on what truly matters in life and work.

# Asteroids and Earth: Should We Be Concerned About Impacts?

Exploring the reality of asteroid impacts and how to stay informed and prepared without unnecessary panic.

Embrace Action: Overcoming Hesitation to Shape Your Destiny

Discover the power of acting swiftly to transform your life, inspired by Mel Robbins' 5 Second Rule.

# Ten Effective Nonverbal Techniques to Convey Confidence

Explore ten powerful nonverbal strategies to communicate confidence and improve your self-image and presence.

Exploring the Four Colour Map Theorem: A Comprehensive Overview

Discover the essence of the Four Colour Theorem and how it establishes the need for four colours on maps without adjacent territories sharing the same hue.

Boost Your Running Performance: Targeting Calf Muscles Effectively

Strengthening calf muscles is crucial for runners. Learn effective exercises to enhance your performance and prevent injuries.

UFOs and the Roswell Incident: Unveiling the Mystery

A deep dive into the Roswell incident, UFOs, and the skepticism surrounding the infamous weather balloon narrative.