Week 2: Diving Deeper into Linux & Bash Scripting

Exploring Advanced Bash Scripting and Linux Tools: Automation, Networking, and Beyond

ยท

5 min read

Week 2: Diving Deeper into Linux & Bash Scripting

Bash Scripting Essentials

1. Loops

Loops help automate repetitive tasks. This week, I explored for, while, and until loops.

Example: Using a for loop to rename files:

#!/bin/bash
# Script to rename all .txt files to .bak

for file in *.txt; do
    mv "$file" "${file%.txt}.bak"
    echo "Renamed $file to ${file%.txt}.bak"
done

Explanation:

  • The loop iterates over all .txt files.

  • ${file%.txt} removes the .txt extension, appending .bak instead.


2. String Operations

Strings are essential in Bash for processing and manipulating text.

Example: Extract a substring from a string:

#!/bin/bash
string="Learning Bash is fun!"
# Extract substring
substring=${string:9:4}
echo "Extracted substring: $substring"

Explanation:

  • ${string:9:4} extracts 4 characters starting at index 9 (0-based).

3. Functions

Functions make scripts reusable and modular.

Example: A function to calculate factorial:

#!/bin/bash
factorial() {
    num=$1
    result=1
    while [ $num -gt 0 ]; do
        result=$((result * num))
        num=$((num - 1))
    done
    echo $result
}

# Call the function
echo "Factorial of 5: $(factorial 5)"

Explanation:

  • The factorial function calculates the factorial of a number using a while loop.

4. Arrays

Arrays allow storage and manipulation of indexed data.

Example: Iterating through an array:

#!/bin/bash
fruits=("apple" "banana" "cherry")

for fruit in "${fruits[@]}"; do
    echo "Fruit: $fruit"
done

Explanation:

  • ${fruits[@]} accesses all elements in the array.

Linux System Tools

1. grep, sed, and awk

Example: Searching and replacing text in a file:

# Original file content:
# Hello World
# Bash Scripting is fun!

# Replace 'fun' with 'amazing' using sed:
sed -i 's/fun/amazing/' file.txt

Explanation:

  • sed uses the s/pattern/replacement/ syntax for substitution.

  • -i updates the file in place.


2. jq

Parsing JSON is effortless with jq.

Example: Extracting a key-value from JSON:

json='{"name": "Linux", "type": "OS"}'
echo $json | jq '.name'

Output:
"Linux"

Explanation:

  • jq '.name' extracts the value associated with the name key.

Networking Commands

1. curl and wget

Example: Downloading a file with wget:

wget https://example.com/file.zip

Explanation:

  • Downloads the specified file from the given URL.

2. ping and traceroute

Example: Troubleshooting network connectivity with ping:

ping -c 4 google.com

Explanation:

  • Sends 4 ICMP packets to google.com to check connectivity.

Disk & Process Management

1. Disk Management Tools

Example: Checking disk usage:

df -h

Output:

Filesystem            Size  Used Avail Use% Mounted on
C:/Program Files/Git  476G  104G  372G  22% /

Explanation:

  • This command is useful for monitoring disk usage and identifying available storage.

  • The -h flag is particularly helpful as it shows sizes in an easily understandable format (e.g., GB instead of blocks).


2. Process Management Tools

Example: Monitoring processes with top:

top

Output:

top - 15:00:05 up  1:00,  1 user,  load average: 0.25, 0.30, 0.35
Tasks: 120 total,   1 running, 119 sleeping,   0 stopped,   0 zombie
%Cpu(s):  3.0 us,  1.0 sy,  0.0 ni, 96.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  8000000 total,  2000000 free,  3000000 used,  3000000 buff/cache
KiB Swap:  2000000 total,  1500000 free,   500000 used.  4000000 avail Mem 

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
 1001 user      20   0  500000  10000   5000 S   2.0  0.1   0:01.23 firefox
 1002 root      20   0  600000  20000   6000 S   1.0  0.2   0:00.56 chrome
 1003 user      20   0  400000  15000   4000 S   0.5  0.1   0:00.21 gnome-shell

Explanation:

The top command is a powerful and commonly used utility in Linux for monitoring system performance in real-time. It provides an interactive, dynamic overview of the system's processes and resource usage.


User and Group Management

Automating user creation:

Example:

#!/bin/bash
# Add a user with a home directory
useradd -m -s /bin/bash newuser
echo "newuser:password" | chpasswd

Explanation:

  • Adds a new user newuser and sets their password.

Package Management

Automating software installation:

Example: Installing multiple packages:

#!/bin/bash
# Install essential packages
apt update && apt install -y vim curl wget

Explanation:

  • Updates package lists and installs the specified packages in one command.

Automation Scripts

1. Organizing Files by Type:

#!/bin/bash
# Organize files by extension
mkdir -p organized
for file in *.*; do
    ext="${file##*.}"
    mkdir -p "organized/$ext"
    mv "$file" "organized/$ext/"
done

Explanation:

  • Creates directories for each file extension and moves files into their respective folders.

2. Monitoring Directory Changes:

#!/bin/bash
# Monitor directory changes
inotifywait -m /path/to/dir -e create -e delete -e modify |
while read path action file; do
    echo "File $file was $action in $path"
done

Explanation:

  • Uses inotifywait to monitor directory changes in real-time.

Challenges & How I Overcame Them

๐Ÿ”ด Debugging Loops and Strings
I initially struggled with debugging loops and string operations in scripts. However, tools like set -x and good old trial-and-error helped me refine my code and boost my confidence.

โœ… Flexible Scripts
Building dynamic scripts that adapt to various environments was another win. Whether it was handling edge cases or ensuring portability, these lessons were invaluable.


Resources I Used

Here are some of the resources that helped me this week:

  1. ๐ŸŒ Linux Journey โ€“ An interactive way to learn Linux concepts.

  2. ๐ŸŒ JavaTpoint โ€“ A quick reference guide for Bash concepts.

  3. ๐Ÿ“บ YouTube Tutorials โ€“ Visual learning for troubleshooting and real-world examples.


Conclusion

This week taught me the power of automation, text manipulation, and system management with Bash and Linux tools. These concepts are not just practical but also transformative in improving efficiency and productivity.๐Ÿš€

ย