Skip to content

Task 3

You want to automate the process of generating daily reports with the current date.

Solution

To solve this problem, you can create a Bash script that uses the date command to get the current date and saves it to a variable. Then, use that variable to generate the report filename.

Step 1: Write the Report Script

Create a new Bash script, let's call it generate_report.sh, and add the following content:

#!/bin/bash

# Script to generate daily reports with the current date

# Get the current date
current_date=$(date +%Y-%m-%d)

# Generate the report filename
report_filename="report_${current_date}.txt"

# Create the report
echo "Daily Report - $current_date" > "$report_filename"
echo "---------------------------------" >> "$report_filename"
echo "Add your report content here." >> "$report_filename"

# Print success message
echo "Report generated: $report_filename"

This script performs the following tasks: - Uses the date command with the %Y-%m-%d format to get the current date. - Generates the report filename using the current date. - Creates the report file with a header containing the report date. - Adds placeholder content to the report file (you can replace it with your actual report content).

Step 2: Make the Script Executable

Make the script executable using the following command:

chmod +x generate_report.sh

Step 3: Run the Script

Execute the script to generate the daily report:

./generate_report.sh

Now, the script will generate a daily report file with the current date in the filename, making it easy to track and organize your reports.

This solution automates the process of generating daily reports, saving you time and ensuring consistency in report naming and content.