Site icon DevOpsHowTo

Stop Wasting Time: Use These 6 Linux Shell Scripting to Supercharge Your Workflow

Linux Shell Scripting Online shell script compiler Linux commands

Today, we will explain why Linux shell scripting is an important tool for DevOps professionals. For many years, shell scripting has been used as an important tool in the Linux environment.


If there is Linux, we will need Linux shell scripting. Shell scripting is also important for DevOps professionals because whenever we must do daily work on any Linux server/machine, we can automate that work with the help of shell scripting.

There are many tasks that we can automate with the help of Linux shell scripting and save our time, and use the saved time for some other work. For a DevOps professional, whether it is from the infrastructure side or the application side, shell scripting is very important.

Today, through this blog, we are going to tell you about the best online shell script compilers, bash/shell tutorials, and some basic shell scripts that are essential for a DevOps professional. For example, what is fi in a shell script?

What is a Linux Shell script?

Linux Shell scripting is a way by which you can automate any task. You just must keep in mind some Linux commands and put them in a file, and save the file with the name .sh, like myshellscript.sh. These scripts can maintain your server/system, install or remove software, backup files, and even clean up all the logs or RAM from the server by monitoring your service’s disk or RAM. Bash (Bourne Again Shell) is the most used shell for scripting in Linux.

Best online bash/shell script compilers

The best thing about learning or practicing shell scripting is that now you don’t even need a Linux machine or server for it. There are many online compilers available in the market on which you can practice and run shell/bash scripting from your browser itself.

Above, we have provided some of the best online bash/shell compilers. You don’t need to set up your local Linux environment.

Learn Linux commands

Discover why mastering Linux is essential for every NOC and DevOps engineer in 2025. Learn the skills, tools, and commands that matter the most.

Basic shell script for your practice

The more you practice shell scripting, the more you will understand scripting, and it will be easy to apply logic. We can start with basic scripts as well. The good part is that these scripts are very commonly used in a production environment.

#!/bin/bash

# Set threshold (in percentage)
THRESHOLD=75

# Set email details
EMAIL="you@example.com"
SUBJECT="Disk Space Alert on $(hostname)"
BODY="/tmp/disk_alert.txt"

# Get disk usage for root '/'
USAGE=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')

if [ "$USAGE" -ge "$THRESHOLD" ]; then
    echo "Disk usage on $(hostname) has reached $USAGE%." > $BODY
    echo "Please check the disk space immediately." >> $BODY
    mail -s "$SUBJECT" "$EMAIL" < $BODY
fi
#!/bin/bash

# Thresholds (in percentage)
RAM_THRESHOLD=80
SWAP_THRESHOLD=80

# Email Alert Setup
EMAIL="you@example.com"
SUBJECT="Memory Alert on $(hostname)"
BODY="/tmp/mem_alert.txt"

# Get RAM and SWAP usage
RAM_USAGE=$(free | awk '/Mem:/ { printf("%.0f"), $3/$2 * 100 }')
SWAP_USAGE=$(free | awk '/Swap:/ { if ($2 > 0) printf("%.0f"), $3/$2 * 100; else print 0 }')

# Clear cache function
clear_ram() {
    echo "Clearing RAM cache..."
    sync; echo 3 > /proc/sys/vm/drop_caches
}

# Clear swap function
clear_swap() {
    echo "Clearing swap memory..."
    swapoff -a && swapon -a
}

# Main Logic
if [ "$RAM_USAGE" -ge "$RAM_THRESHOLD" ]; then
    echo "RAM usage is at ${RAM_USAGE}%. Clearing cache." > $BODY
    clear_ram
    mail -s "$SUBJECT - RAM Cleared" "$EMAIL" < $BODY
fi

if [ "$SWAP_USAGE" -ge "$SWAP_THRESHOLD" ]; then
    echo "SWAP usage is at ${SWAP_USAGE}%. Clearing swap." > $BODY
    clear_swap
    mail -s "$SUBJECT - Swap Cleared" "$EMAIL" < $BODY
fi
#!/bin/bash

# Define the process keyword (adjust this to match your Java process name or main class)
PROCESS_KEYWORD="MyJavaApp"

# Email settings
EMAIL="you@example.com"
SUBJECT="ALERT: Java Process Down on $(hostname)"
BODY="/tmp/java_process_alert.txt"

# Check if the process is running
if ! pgrep -f "$PROCESS_KEYWORD" > /dev/null; then
    echo "The Java process with keyword '$PROCESS_KEYWORD' is not running on $(hostname)." > "$BODY"
    echo "Please check the server immediately." >> "$BODY"
    mail -s "$SUBJECT" "$EMAIL" < "$BODY"
fi
#!/bin/bash

# Directories to back up
SOURCE_DIRS="/var/www /etc"

# Backup destination
BACKUP_DIR="/backups"
DATE=$(date +%F-%H%M)
HOSTNAME=$(hostname)
BACKUP_FILE="$BACKUP_DIR/${HOSTNAME}-backup-$DATE.tar.gz"

# Log file
LOG_FILE="/var/log/server_backup.log"

# Create backup directory if it doesn't exist
mkdir -p "$BACKUP_DIR"

# Perform the backup
echo "[$(date)] Starting backup..." >> "$LOG_FILE"
tar -czf "$BACKUP_FILE" $SOURCE_DIRS 2>> "$LOG_FILE"

if [ $? -eq 0 ]; then
    echo "[$(date)] Backup successful: $BACKUP_FILE" >> "$LOG_FILE"
else
    echo "[$(date)] Backup FAILED" >> "$LOG_FILE"
fi
#!/bin/bash

# Load threshold (adjust as needed)
THRESHOLD=0.70

# Email details
EMAIL="you@example.com"
SUBJECT="ALERT: High Server Load on $(hostname)"
BODY="/tmp/server_load_alert.txt"

# Get 1-minute average load
LOAD=$(uptime | awk -F 'load average:' '{ print $2 }' | cut -d',' -f1 | sed 's/ //g')

# Convert load to floating point comparison
if (( $(echo "$LOAD >= $THRESHOLD" | bc -l) )); then
    echo "High server load detected on $(hostname)" > $BODY
    echo "Current load is: $LOAD" >> $BODY
    echo "Threshold is: $THRESHOLD" >> $BODY
    mail -s "$SUBJECT" "$EMAIL" < $BODY
fi

One of the most asked questions by beginners is

What is fi in Shell Script?

In bash scripting, fi is used to close a if statement. Think of it as the reverse of if. You start a condition with the if keyword and end it with the fi keyword.

For example: 

num=15
if [ $num -gt 10 ]; then
  echo "Number is greater than 10"
fi

Output: 
Number is greater than 10

What is a for loop in a Linux shell scripting?

A for loop is used to iterate over a list of items and execute a block of code for each item. It’s useful for automating repetitive tasks.
Suppose you have a list of items, and you want to do some work on those items, then we can get our work done using the for loop. The for loop will keep working as long as the condition is true; as soon as the condition becomes false, the loop will stop doing its work.

Example:

for i in {1..5}
do
   echo "Number: $i"
done

Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5

Conclusion

In summary, Linux shell scripting is a powerful tool in Linux that helps automate many tasks with ease. We explored how different types of shell scripts can be useful in real-world production environments. We also covered essential concepts like using online shell script compilers, if conditions, and for loops. By using shell scripts, you not only simplify your daily tasks but also save valuable time that can be used on other important work, ultimately boosting your productivity.

Exit mobile version