Skip to content

Latest commit

 

History

History
148 lines (96 loc) · 3.37 KB

01.Practical scripts for common tasks (log analysis, data processing, system monitoring).md

File metadata and controls

148 lines (96 loc) · 3.37 KB

Practical Linux Scripts for Common Tasks

1. Log Analysis Script:

#!/bin/bash

# Log Analysis Script

log_file="/var/log/syslog"

# Count occurrences of specific events in the log
error_count=$(grep -c "ERROR" "$log_file")
warning_count=$(grep -c "WARNING" "$log_file")

# Display results
echo "Log Analysis Report"
echo "-------------------"
echo "Total Errors: $error_count"
echo "Total Warnings: $warning_count"

This script analyzes the syslog for the counts of ERROR and WARNING messages.

2. Data Processing Script:

#!/bin/bash

# Data Processing Script

input_file="input_data.txt"
output_file="processed_data.txt"

# Process data (example: convert tabs to spaces)
sed 's/\t/ /g' "$input_file" > "$output_file"

echo "Data Processing Complete. Output saved to $output_file"

This script uses sed to replace tabs with spaces in a data file.

3. System Monitoring Script:

#!/bin/bash

# System Monitoring Script

# Get system information
cpu_usage=$(top -b -n1 | grep "Cpu(s)" | awk '{print $2}')
memory_usage=$(free -m | awk '/Mem:/ {print $3}')

# Display system information
echo "System Monitoring Report"
echo "------------------------"
echo "CPU Usage: $cpu_usage%"
echo "Memory Usage: $memory_usage MB"

This script provides a simple system monitoring report displaying CPU and memory usage.

4. Backup Script:

#!/bin/bash

# Backup Script

source_directory="/path/to/source"
backup_directory="/path/to/backup"
backup_file="backup_$(date +"%Y%m%d_%H%M%S").tar.gz"

# Create a compressed backup of the source directory
tar -czf "$backup_directory/$backup_file" -C "$source_directory" .

echo "Backup Complete. File saved to $backup_directory/$backup_file"

This script creates a timestamped compressed backup of a source directory.

5. Network Connectivity Check Script:

#!/bin/bash

# Network Connectivity Check Script

remote_host="www.google.com"
timeout_seconds=5

# Check network connectivity
if ping -c 1 -W "$timeout_seconds" "$remote_host" &> /dev/null; then
    echo "Network is reachable."
else
    echo "Network is unreachable."
fi

This script checks if a remote host is reachable using the ping command.

6. User Account Management Script:

#!/bin/bash

# User Account Management Script

new_user="newuser"

# Check if user exists
if id "$new_user" &> /dev/null; then
    echo "User $new_user already exists."
else
    # Create a new user
    useradd "$new_user"
    echo "User $new_user created successfully."
fi

This script checks if a user exists and creates the user if it doesn't.

7. File Cleanup Script:

#!/bin/bash

# File Cleanup Script

directory="/path/to/files"
file_extension=".tmp"

# Remove temporary files
find "$directory" -type f -name "*$file_extension" -delete

echo "Cleanup Complete. Removed files with extension $file_extension."

This script removes files with a specific extension in a given directory.

These scripts serve as examples for various common tasks in Linux scripting. Customize them according to your specific requirements and integrate error checking, logging, and additional functionality as needed.