Week 1 of My Linux & Bash Scripting Journey

Discover the Basics of Linux and Bash Scripting: From Commands to Real-World Problem Solving

·

4 min read

Week 1 of My Linux & Bash Scripting Journey

Introduction

This week marked the start of my journey into Linux and Bash Scripting 🐧💻. Although I have some basic knowledge of Linux, I wanted to go deeper into understanding the Linux operating system and learn how to automate repetitive tasks using Bash scripts.

My focus for this week was to solidify foundational concepts, practice using Linux commands, and write my first few scripts. It was both exciting and challenging, and I learned a lot along the way.


Topics Covered

1️⃣ Command Line Basics
I revisited and practiced fundamental Linux commands and explored text manipulation and permissions:

  • Basic Commands:

    • ls – Listing files and directories.

    • cd – Navigating between directories.

    • mkdir – Creating directories.

    • rm – Removing files and directories.

    • cat and nano – Reading and editing files.

  • Text Manipulation:

    • Used commands like grep, sed, and awk for processing and filtering text.

    • Practiced combining commands with pipes (|) for efficient workflows.

  • File Permissions:

    • Explored permission notations (-rw-r--r--) and numerical values (644, 755).

    • Practiced commands like chmod, chown, and chgrp to modify ownership and permissions.

    • Learned how to manage access control for users, groups, and others.


2️⃣ Bash Scripting
This was my first deep dive into Bash scripting. Here’s what I learned:

  • Introduction to Scripting:

    • Wrote my first script with the #!/bin/bash shebang.

    • Assigned execute permissions using chmod +x.

    • Executed scripts using ./script_name.sh.

  • Variables and User Input:

    • Declared variables and assigned values.

    • Captured user input with read and used it in scripts.

    • Example:

        #!/bin/bash  
        echo "Enter your name:"  
        read name  
        echo "Hello, $name!"
      
  • Positional Arguments:

    • Learned to pass arguments to scripts and accessed them with $1, $2, etc.

    • Example:

        #!/bin/bash  
        echo "First argument: $1"  
        echo "Second argument: $2"
      
  • Output and Input Redirection:

    • Redirected output to files with > and appended with >>.

    • Used < to redirect input to commands or scripts.

  • Conditional Statements:

    • Practiced if, elif, and else conditions.

    • Example:

        #!/bin/bash  
        if [ $1 -gt 10 ]; then  
          echo "Number is greater than 10"  
        elif [ $1 -eq 10 ]; then  
          echo "Number is 10"  
        else  
          echo "Number is less than 10"  
        fi
      
  • Case Statements:

    • Explored case for handling multiple conditions.

    • Example:

        #!/bin/bash  
        case $1 in  
          start) echo "Starting...";;  
          stop) echo "Stopping...";;  
          restart) echo "Restarting...";;  
          *) echo "Invalid option";;  
        esac
      

Challenges Faced & Solutions

🔴 1. Understanding File Permissions

  • Problem: Interpreting permission notations and converting them to numerical values was tricky at first.

  • Solution:

    • Broke down notations to understand type (e.g., - for files, d for directories) and permissions for owner, group, and others.

    • Practiced modifying permissions using commands like:

        chmod 755 file_name  
        chmod +x script_name.sh
      

🔴 2. Writing Error-Free Scripts

  • Problem: Encountered syntax errors in conditionals.

  • Solution:

    • Referred to beginner-friendly tutorials and focused on understanding syntax rules.

    • Started with simple examples before attempting more complex scripts.

🔴 3. Debugging Scripts

  • Problem: It was challenging to identify errors in scripts.

  • Solution:

    • Used echo to check variable values.

    • Enabled step-by-step debugging with set -x to trace execution.

    • Example:

        #!/bin/bash  
        set -x  
        for i in {1..5}; do  
          echo "Number: $i"  
        done  
        set +x
      

Steps I Followed

  1. Plan and Structure:
    Listed the commands and concepts to revisit and organized learning into manageable sections.

  2. Practice:
    Experimented with commands and wrote multiple small scripts.

  3. Debug and Learn:
    I deliberately introduced errors in order to learn common pitfalls and debugging techniques.

  4. Document:
    Kept detailed notes on my learning process, challenges, and solutions to streamline future practice.


Resources Used

  • 🌐 Linux Journey: A fantastic resource for building a solid foundation in Linux concepts.

  • 🌐 SS64 Bash: A concise reference for Bash commands and scripting tips.

  • 📺 YouTube: Watched tutorials for practical demonstrations of Bash scripting techniques.


Next Steps

➡️ Explore advanced Bash scripting concepts like loops, functions, arrays, and traps.
➡️ Dive into real-world automation tasks using Bash.
➡️ Begin integrating Linux and Bash scripting knowledge for practical use cases.


This week has been an amazing start to my Linux and Bash Scripting journey. I’ve learned a lot, tackled challenges, and built a strong foundation for upcoming weeks. Excited to continue! 🚀