Linux Shell Scripting & Automation

Pratik BapatPratik Bapat
3 min read

Challenge 1:

Write a simple Bash script that prints “Hello DevOps” along with the current date and time.

#!/bin/bash
echo "Hello DevOps"
echo "Current date and time: $(date)"

Challenge 2:

Create a script that checks if a website (e.g., https://www.learnxops.com) is reachable using curl or ping. Print a success or failure message.

#!/bin/bash
URL="https://www.learnxops.com"
if curl -s --head --request GET $URL | grep "200 OK" > /dev/null; then
   echo "Website is reachable"
else
   echo "Website is not reachable"
fi

Challenge 3:

Write a script that takes a filename as an argument, checks if it exists, and prints the content of the file accordingly.

#!/bin/bash
FILENAME=$1
if [ -f "$FILENAME" ]; then
    cat "$FILENAME"
else
    echo "File does not exist"
fi

Challenge 4:

Create a script that lists all running processes and writes the output to a file named process_list.txt.

#!/bin/bash
ps aux > process_list.txt

Challenge 5:

Write a script that installs multiple packages at once (e.g., git, vim, curl). The script should check if each package is already installed before attempting installation.

#!/bin/bash
PACKAGES=("git" "vim" "curl")
for PACKAGE in "${PACKAGES[@]}"; do
    if ! dpkg -l | grep -q $PACKAGE; then
        sudo apt-get install -y $PACKAGE
    else
        echo "$PACKAGE is already installed"
    fi
done

Challenge 6:

Create a script that monitors CPU and memory usage every 5 seconds and logs the results to a file.

#!/bin/bash
while true; do
    echo "CPU and Memory usage at $(date)" >> usage_log.txt
    top -b -n1 | head -n 10 >> usage_log.txt
    sleep 5
done

Challenge 7:

Write a script that automatically deletes log files older than 7 days from /var/log.

#!/bin/bash
find /var/log -type f -name "*.log" -mtime +7 -exec rm {} \;

Challenge 8:

Automate user account creation – Write a script that takes the username as an argument, checks, if the user exists, gives the message “user already exists“ else creates a new user, adds it to a “devops“ group, and sets up a default home directory.

#!/bin/bash

# Check if username is provided
if [ $# -ne 1 ]; then
    echo "Usage: $0 <username>"
    exit 1
fi

USERNAME=$1

# Check if the user already exists
if id "$USERNAME" &>/dev/null; then
    echo "User '$USERNAME' already exists."
    exit 0
else
    # Create the user with a home directory
    sudo useradd -m -s /bin/bash "$USERNAME"

    # Add the user to the 'devops' group
    sudo usermod -aG devops "$USERNAME"

    echo "User '$USERNAME' has been created and added to the 'devops' group."
fi

Challenge 9:

Use awk or sed in a script to process a log file and extract only error messages.

#!/bin/bash
LOGFILE=$1
awk '/error/ {print}' $LOGFILE

Challenge 10:

Set up a cron job that runs a script to back up (zip/tar) a directory daily.

#!/bin/bash
BACKUP_DIR="/path/to/backup"
TARGET_DIR="/path/to/target"
tar -czf $BACKUP_DIR/backup_$(date +\%Y\%m\%d).tar.gz $TARGET_DIR

To set up a cron job, add the following line to your crontab (crontab -e):

0 0 * * * /path/to/your/script.sh

Bonus Challenge:

Customize your Bash prompt to display the current user and working directory. (Hint: export PS1="\u@\h:\w$ "), try to make it permanent, so terminal closing and opening don’t default!

export PS1="\u@\h:\w\$ "
source ~/.bashrc

0
Subscribe to my newsletter

Read articles from Pratik Bapat directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pratik Bapat
Pratik Bapat