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 it enables us to automate daily tasks on Linux servers or machines.
There are many tasks that we can automate with the help of Linux shell scripting, saving us time that can be used for 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?
Table of Contents
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.
- Jdoodle bash compiler
- Replit bash compiler
- Online gdb bash compiler
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
- ls – Lists files and directories in the current directory.
- pwd – Prints the current working directory.
- rm – Removes files or directories.
- touch – Creates an empty file or updates the timestamp of a file.
- cp – Copies files or directories.
- mv – Moves or renames files and directories.
- grep – Searches for a pattern in a file or input stream.
- mkdir – Creates a new directory.
- cd – Changes the current directory.
- cat – Displays the content of files.
- zip – Compresses files into a zip archive.
- unzip – Extracts files from a zip archive.
- tar – Archives files and directories into a single file (often with compression).
- vim – Opens a file in the Vim text editor.
- sed – Stream editor for filtering and transforming text.
- head – Displays the first few lines of a file.
- tail – Displays the last few lines of a file.
- diff – Compares two files line by line.
- locate – Quickly finds file paths by searching a prebuilt index.
- find – Searches for files and directories in real-time based on conditions.
- sudo – Executes a command with superuser privileges.
- chmod – Changes file or directory permissions.
- chown – Changes ownership of a file or directory.
- ps – Lists running processes.
- useradd – Adds a new user account.
- passwd – Changes a user’s password.
- userdel – Deletes a user account.
- df – Shows disk space usage of file systems.
- du – Displays disk usage of files and directories.
- top – Monitors system processes and resource usage in real-time.
- uptime – Displays how long the system has been running.
- hostname – Shows or sets the system’s hostname.
- date – Displays or sets the current date and time.
- systemctl – Manages systemd services.
- kill – Sends a signal (usually terminate) to a process.
- ping – Checks connectivity to a host using ICMP echo requests.
- wget – Downloads files from the web via HTTP or FTP.
- curl – Transfers data to or from a server using various protocols.
- scp – Securely copies files between systems over SSH.
- sync – Efficiently syncs files and directories between locations.
- ip – Shows/manages IP addresses, routes, and network interfaces.
- netstat – Displays network connections and listening ports.
- traceroute – Traces the path packets take to a network host.
- nslookup – Queries DNS records for a domain.
- dig – Performs detailed DNS lookups.
- history – Shows previously executed commands in the shell.
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.
- Write a shell script that checks if the disk space usage on the server exceeds 75%. If it does, the script should send an email alert.
#!/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
- If the server’s RAM or swap memory is full, how can we automatically clear it using a script?
#!/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
- We want to monitor a specific Java process on a server. If the process goes down or stops running for any reason, the server should automatically send us an email alert notifying us of the issue.
#!/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
- How can we create a shell script to take a backup of the server automatically?
#!/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
- If the server load reaches or exceeds 70%, we want to be notified by email.
#!/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.
FAQs
Q1: What is a Linux shell script, and why should I use one?
A Linux shell script is a text file containing a sequence of commands that automate repetitive tasks. Instead of typing each command manually, a shell script executes them in one go, saving you time and reducing human error.
Q2: Can I automate daily tasks like backups or server checks using shell scripts?
Absolutely! Shell scripts are commonly used to automate tasks like file backups, server health checks, log rotation, user management, and more. With tools like cron, you can even schedule them to run at regular intervals.
Q3: Are these shell scripts safe to run on production systems?
Yes, if they’re well-tested. Always validate the script on a development or staging environment before deploying to production. Scripts that modify files or services should include safety checks and error handling.
Q4: Do I need root access to run these Linux scripts?
Not always. Most maintenance or monitoring scripts can run with regular user permissions. However, tasks that modify system settings, install packages, or restart services may require sudo access.
Q5: How can I schedule these scripts to run automatically?
You can use the cron utility to schedule shell scripts. For example, adding a line to crontab -e like 0 2 * * * /path/to/script.sh will run it every day at 2 AM.
Q6: Which Linux distributions are compatible with these scripts?
Most scripts written in bash will work across major distributions like Ubuntu, Debian, CentOS, Fedora, and Arch, as long as the required commands (like rsync, df, or grep) are installed.
Q7: How can I modify these shell scripts for my use case?
Simply open the .sh file in a text editor like nano or vim and adjust paths, filenames, or variables to suit your needs. Just ensure proper syntax and test after making changes.
Q8: What happens if one of the commands in the script fails?
By default, the script will continue running unless you use set -e at the top. To handle failures gracefully, add error-checking code using if statements or exit codes (e.g., if [ $? -ne 0 ]; then … fi).
Q9: Can I run these scripts on a remote server using SSH?
Yes, you can run scripts remotely using ssh user@remotehost ‘bash -s’ < script.sh, or copy the script using scp and then execute it via SSH. It’s a common practice in DevOps workflows.
Q10: What are some best practices for writing shell scripts?
* Use #!/bin/bash at the top
* Add comments for clarity
* Validate input parameters
* Use set -euo pipefail for safer execution
* Always test in a non-production environment first
Discover more from DevOpsHowTo.Com
Subscribe to get the latest posts sent to your email.
1 thought on “Stop Wasting Time: Use These 6 Linux Shell Scripting to Supercharge Your Workflow”