0% found this document useful (0 votes)
19 views2 pages

Beginner Linux Shell Scripting Projects

The document contains a collection of Linux shell scripts for various system administration tasks. It includes scripts for gathering system information, monitoring disk usage, backing up files, updating packages, managing users, and cleaning log files. Each script is designed to automate specific functions to enhance system management efficiency.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

Beginner Linux Shell Scripting Projects

The document contains a collection of Linux shell scripts for various system administration tasks. It includes scripts for gathering system information, monitoring disk usage, backing up files, updating packages, managing users, and cleaning log files. Each script is designed to automate specific functions to enhance system management efficiency.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Linux Shell Scripts Project

1. System Info Script


#!/bin/bash

echo "System Information"


echo "------------------"
echo "Hostname: $(hostname)"
echo "Uptime: $(uptime -p)"
echo "Kernel Version: $(uname -r)"
echo "CPU Info: $(lscpu | grep 'Model name' | cut -d ':' -f2)"
echo "Memory Usage:"
free -h

2. Disk Usage Alert Script


#!/bin/bash

THRESHOLD=80
df -H | grep '^/dev/' | while read line; do
usage=$(echo $line | awk '{print $5}' | sed 's/%//')
partition=$(echo $line | awk '{print $1}')
if [ $usage -gt $THRESHOLD ]; then
echo "Warning: $partition is at ${usage}% usage"
fi
done

3. Backup Script
#!/bin/bash

SOURCE_DIR="/home/youruser/Documents"
BACKUP_DIR="/home/youruser/backups"
DATE=$(date +%F)

mkdir -p $BACKUP_DIR
tar -czf $BACKUP_DIR/backup-$[Link] $SOURCE_DIR

echo "Backup completed for $SOURCE_DIR at $BACKUP_DIR/backup-$[Link]"

4. Package Update Script


#!/bin/bash

echo "Updating package list..."


sudo apt update && sudo apt upgrade -y
echo "System update complete.

5. User Management Script


#!/bin/bash

echo "User Management"


echo "1. Add User"
echo "2. Delete User"
echo "3. List Users"
read -p "Choose an option [1-3]: " choice

case $choice in
1)
read -p "Enter username to add: " user
sudo adduser $user
;;
2)
read -p "Enter username to delete: " user
sudo deluser $user
;;
3)
cut -d: -f1 /etc/passwd
;;
*)
echo "Invalid option."
;;
esac

6. Log Cleaner Script


#!/bin/bash

LOG_DIR="/var/log"
DAYS=7

echo "Cleaning log files older than $DAYS days from $LOG_DIR"
sudo find $LOG_DIR -type f -mtime +$DAYS -exec rm -f {} \;
echo "Log cleanup complete.

Common questions

Powered by AI

The user management script interacts with the Linux system by utilizing system commands such as 'adduser' and 'deluser' to add or delete specified usernames. These commands require appropriate permissions, often executed with 'sudo', to modify user accounts. Potential risks include incorrect user deletions due to human error, lack of input validation leading to unauthorized account access, and permission escalation by malicious actors imitating legitimate users. Regular audits and well-defined permissions can mitigate these risks .

The user management script potentially faces security concerns related to improper handling of user input, such as command injection. An attacker could input malicious code instead of a username, resulting in unauthorized system access or data breach. To mitigate this, input validation and sanitization should be implemented, such as checking for special characters that may lead to injection attacks. Additionally, using parameterized queries or secure APIs for user management can further prevent unauthorized actions .

The backup script ensures data integrity by creating a compressed archive of the specified source directory, preserving the file structure and contents on the specified date. It stores backups in a predefined directory, effectively maintaining a snapshot of the data at a particular point in time. Improvements for added reliability could include implementing encryption for the backup files, automating verification checks to ensure successful backup creation, and using external storage to prevent data loss in case of local disk failure .

The package update script is essential in scenarios where system security and software reliability are critical, such as on servers managing sensitive data or performing crucial operations. Regular updates ensure the latest security patches and software improvements are applied. However, its limitations in a dynamic environment include potential compatibility issues with other installed software, disruption of services during updates, and the need for manual intervention when updates require configuration changes that cannot be automated .

The Linux shell script determines when to issue a disk usage alert by comparing the current disk usage percentage of each partition against a predefined threshold, set at 80%. It reads each partition's usage and checks if it exceeds the threshold, issuing a warning if true. To improve accuracy, the script could be optimized by dynamically adjusting the threshold based on historical usage patterns or including a real-time notification system that alerts users of potential issues before reaching the threshold .

The system info script provides administrators with a quick summary of critical system details, including hostname, system uptime, kernel version, CPU information, and memory usage. This information is essential for monitoring system health and performance. By consolidating this data into a single script, it enhances operational efficiency, enabling administrators to quickly diagnose issues, verify system configuration, and perform routine checks without manually retrieving information from various sources .

The log cleaner script is critical for maintaining long-term system health by preventing log files from consuming excessive disk space, which can degrade system performance. By removing files older than a specified duration, it ensures ample space for newer data and reduces disk fragmentation. However, if misconfigured to delete necessary log files too frequently or indiscriminately, it could negatively impact server performance by removing important diagnostic information quickly needed to troubleshoot issues, debilitating efficient problem-solving processes .

The log cleaner script is designed to manage disk space by removing old log files that are older than a specified number of days, thus freeing up space and reducing clutter. The backup script, on the other hand, focuses on preserving important data by compressing and saving it to a designated backup directory. They complement each other by ensuring the system remains both clean and backed up: the log cleaner maintains available disk space, which is crucial for storing backups, while the backup script ensures critical data remains safe even if old log files are removed by the log cleaner .

The choice of an 80% threshold in the disk usage alert script reflects a proactive approach to system management, aiming to address potential storage issues before they cause critical failures. While the threshold allows room for further usage, considerations when setting it should include the typical usage patterns, the criticality of the system, and the rate of data growth. Lower thresholds might be appropriate in environments with high data inflow or critical applications where storage availability is paramount. Balancing alert frequency with actual risk of outages is essential .

To ensure effective data recovery in the event of major system failures, strategies to implement alongside the backup script include maintaining offsite or cloud-based backups to protect against local disasters, creating an automated backup schedule to ensure regular data snapshots, and implementing redundancy through RAID configurations or mirrored drives to protect against hardware failures. Additionally, regular testing of backup restoration processes ensures that recovery is efficient and data is accessible when needed most .

You might also like