DAY 10: The Script Artisan

LinuxBeginner
Practice Now

Introduction

Welcome, aspiring system administrator! You've just joined a bustling tech company as a Junior DevOps Engineer. Your senior colleague is on a well-deserved vacation, and a critical task has just landed on your desk. The main application server is generating numerous log files, and they are quickly consuming disk space. Your mission, should you choose to accept it, is to become The Script Artisan.

You need to create an automated shell script to manage these log files. This isn't just about running a few commands; it's about building a robust, reusable tool. You'll start with a simple script and progressively add features like variables, user input, error checking, and loops. By the end of this challenge, you will have crafted a script that can back up and manage files, proving your value to the team and saving the day!

Let's get scripting!

Important Notice
The upcoming challenges may exceed the scope of the Quick Start with Linux course.
If you encounter difficulties during the challenge:
  1. Temporarily skip the challenge and continue with subsequent Guided Labs in the Linux learning path.
  2. Discuss with Labby or view the solution.

Writing a Simple Shell Script

Your first task is to lay the foundation. Every great script starts with a single line. You need to create the script file and add a basic command to ensure it's working correctly. This initial step confirms your setup is correct and you're ready to build more complex logic.

Tasks

  • Create a new shell script file named log_manager.sh in the ~/project directory.
  • Add a "shebang" line (#!/bin/bash) at the very top of the script. This tells the system which interpreter to use.
  • Add a command to print the message "Log Manager Initialized." to the screen.
  • Make the script executable using the chmod +x command.

Requirements

  • The script must be named log_manager.sh.
  • The script must be located in the ~/project directory.
  • The first line must be #!/bin/bash.
  • The script must use the echo command to display the required message.
  • The script must have execute permissions (use chmod +x log_manager.sh).

Examples

After creating and making the script executable, the complete process should look like this:

First, verify the script file exists:

ls ~/project/
log_manager.sh

Set execute permissions on the script:

chmod +x ~/project/log_manager.sh

Now run the script:

./log_manager.sh

The script should display:

Log Manager Initialized.

You can also verify the script content is correct:

cat ~/project/log_manager.sh

The file should contain:

#!/bin/bash

echo "Log Manager Initialized."

Hints

  • You can use a command-line text editor like nano to create and edit the file. For example: nano log_manager.sh.
  • The echo command is used to display a line of text.
  • Use chmod +x filename to make a script executable. The +x adds execute permissions for the file owner.

Adding Variables and User Input

A hardcoded script isn't very flexible. To make your script more dynamic and reusable, you'll now introduce variables. You'll define a variable for the log directory and prompt the user for a name for the backup archive. This makes the script adaptable to different situations without changing the code itself.

Tasks

  • Modify your log_manager.sh script.
  • Define a variable named LOG_DIR and assign it the path /home/labex/project/app_logs.
  • Add a line to print "Enter the backup filename: " to prompt the user for input.
  • Use the read command to capture the user's input into a new variable called BACKUP_FILENAME.
  • Add a final echo command to confirm the settings, displaying a message like "Backing up logs to: [filename]", where [filename] is the value entered by the user.

Requirements

  • The script must contain a variable LOG_DIR set to /home/labex/project/app_logs.
  • The script must use the read command to get input from the user.
  • The user input must be stored in a variable named BACKUP_FILENAME.
  • The final output must use the $BACKUP_FILENAME variable.

Examples

When you run the modified script, it should prompt for user input and display the confirmation message. Here's what the interaction should look like:

./log_manager.sh
Log Manager Initialized.
Enter the backup filename: my_backup_2024.tar.gz
Backing up logs to: my_backup_2024.tar.gz

The script should pause at the "Enter the backup filename:" prompt, waiting for the user to type a filename and press Enter. After the user provides input, it displays the confirmation message using the entered filename.

Hints

  • To define a variable, use the syntax VARIABLE_NAME="value". There should be no spaces around the = sign.
  • To use a variable's value, prefix its name with a $ sign, like $MY_VARIABLE.
  • The read command pauses the script and waits for the user to type something and press Enter.

Implementing Conditional Logic

A good script anticipates problems. What happens if the log directory you specified doesn't exist? The script would fail. To make your script more robust, you need to add conditional logic. You'll use an if statement to check if the log directory exists before attempting any operations.

Tasks

  • Modify your log_manager.sh script.
  • Add an if statement to check if the directory specified by the $LOG_DIR variable exists.
  • If the directory exists, the script should proceed as before (prompt for filename, etc.).
  • If the directory does not exist, the script should print an error message "Error: Log directory not found." and exit immediately with a non-zero status code.

Requirements

  • The script must use an if statement.
  • The condition must check for the existence of a directory using the -d test operator.
  • If the directory does not exist, the script must echo the specified error message.
  • If the directory does not exist, the script must terminate using exit 1.

Examples

When the log directory exists, the script should run normally:

./log_manager.sh
Log Manager Initialized.
Log directory found. Proceeding...
Enter the backup filename: test_backup.tar.gz
Backing up logs to: test_backup.tar.gz

If the log directory doesn't exist, the script should display an error and exit:

./log_manager.sh
Log Manager Initialized.
Error: Log directory not found.

In this case, the script terminates immediately after displaying the error message, without prompting for user input or proceeding with any backup operations.

Hints

  • The syntax for a basic if statement is if [ condition ]; then ... else ... fi.
  • The test [ -d "$DIRECTORY_PATH" ] returns true if $DIRECTORY_PATH exists and is a directory.
  • exit 1 is a standard way to signal that a script has terminated with an error.

Looping Through File Operations

Now for the core of the automation! You need to process the files within the log directory. You'll use a for loop to iterate through all files ending with .log in the app_logs directory. For this challenge, you will simply copy them to a new backup directory.

Tasks

  • First, create a directory named backups inside ~/project where the log files will be copied.
  • Modify your log_manager.sh script.
  • Inside the if block (where the directory is confirmed to exist), add a for loop.
  • The loop should iterate over every file ending in .log inside the $LOG_DIR directory.
  • Inside the loop, use the cp command to copy each log file into the ~/project/backups directory.
  • For each file copied, print a message like "Copied [filename]".

Requirements

  • You must first create the ~/project/backups directory from the command line.
  • The script must use a for loop.
  • The loop must iterate through files in $LOG_DIR that match the *.log pattern.
  • The cp command must be used inside the loop to copy files to ~/project/backups.
  • An echo statement must report the name of each file as it is copied.

Examples

After completing this step, when you run the script with a backup filename, it should process all log files and display output similar to:

./log_manager.sh
Log Manager Initialized.
Log directory found. Proceeding...
Enter the backup filename: full_backup.tar.gz
Backing up logs to: full_backup.tar.gz
Copied /home/labex/project/app_logs/access.log
Copied /home/labex/project/app_logs/debug.log
Copied /home/labex/project/app_logs/error.log
Backup complete.

The script should iterate through each .log file in the directory, displaying a "Copied" message for each file. The exact filenames will depend on the log files present in your app_logs directory.

After the script completes, you can verify the files were copied by checking the backups directory:

ls ~/project/backups/
access.log  debug.log  error.log

Hints

  • You can create a directory using the mkdir command.
  • A for loop for files can be written as for file in /path/to/*.log; do ... done.
  • The cp command syntax is cp <source> <destination>.
  • The loop variable (e.g., file) will hold the full path to the file on each iteration.

Summary

Congratulations, Script Artisan! You have successfully built a complete, functional shell script from scratch. You took on a real-world problem—managing log files—and solved it with the power of automation.

In this challenge, you have mastered several fundamental concepts of shell scripting:

  • Creating and structuring a script with a shebang.
  • Using variables to make your script dynamic.
  • Capturing user input with read.
  • Implementing robust error-checking with if statements.
  • Automating repetitive tasks with for loops.
  • Managing file permissions and executing your script.

Your script can now automatically check for the existence of log directories, process all log files, and copy them to a backup location. This is a huge step in your journey as a DevOps engineer or System Administrator. The skills you've practiced here are the foundation for writing much more complex automation scripts that can manage servers, deploy applications, and process data. You've proven you can handle responsibility and deliver a working solution. Well done!

✨ Check Solution and Practice✨ Check Solution and Practice✨ Check Solution and Practice✨ Check Solution and Practice