I will create two simple Bash shell scripts. The first one creates a backup of a given directory, and the second one monitors the disk usage of a given partition.
Script 1: backup_directory.sh
```
#!/bin/bash
# Check if the correct number of arguments is provided
if [ "$#" -ne 2 ]; then
echo "Usage: backup_directory.sh <directory_to_backup> <backup_destination>"
exit 1
fi
# Get the arguments
directory_to_backup=$1
backup_destination=$2
# Check if the given directory exists
if [ ! -d "$directory_to_backup" ]; then
echo "ERROR: The given directory does not exist."
exit 1
fi
# Create the backup
current_date=$(date "+%Y-%m-%d-%H-%M-%S")
backup_name=$(basename "$directory_to_backup")_$current_date.tar.gz
tar -czvf "$backup_destination/$backup_name" "$directory_to_backup"
echo "Backup of $directory_to_backup created at $backup_destination/$backup_name"
```
Script 2: check_disk_usage.sh
```
#!/bin/bash
# Check if the correct number of arguments is provided
if [ "$#" -ne 2 ]; then
echo "Usage: check_disk_usage.sh <partition> <percent_threshold>"
exit 1
fi
# Get the arguments
partition=$1
percent_threshold=$2
# Get the disk usage
disk_usage=$(df -h "$partition" | awk 'NR==2 {print $5}' | sed 's/%//')
# Check if the disk usage is greater than the threshold
if [ "$disk_usage" -gt "$percent_threshold" ]; then
echo "WARNING: The disk usage of $partition is greater than $percent_threshold%: $disk_usage%"
else
echo "The disk usage of $partition is $disk_usage% which is below the threshold of $percent_threshold%"
fi
```
These scripts can be saved with the mentioned names (e.g., `backup_directory.sh` and `check_disk_usage.sh`). To run the scripts, the user must first make them executable by running:
```
chmod +x backup_directory.sh
chmod +x check_disk_usage.sh
```
After making the scripts executable, they can be run by providing the required arguments, as shown below:
1. To run the `backup_directory.sh` script:
```
./backup_directory.sh /path/to/directory/that/needs/backup /path/to/backup/destination
```
2. To run the `check_disk_usage.sh` script:
```
./check_disk_usage.sh /dev/sda1 90
```
*Note: Replace `/dev/sda1` with your partition name and `90` with your desired percentage threshold.
2. Create two Bash shell scripts that will perform some work (of your choice).
1 answer