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!
- Temporarily skip the challenge and continue with subsequent Guided Labs in the Linux learning path.
- 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.shin the~/projectdirectory. - 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 +xcommand.
Requirements
- The script must be named
log_manager.sh. - The script must be located in the
~/projectdirectory. - The first line must be
#!/bin/bash. - The script must use the
echocommand 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
nanoto create and edit the file. For example:nano log_manager.sh. - The
echocommand is used to display a line of text. - Use
chmod +x filenameto make a script executable. The+xadds 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.shscript. - Define a variable named
LOG_DIRand 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
readcommand to capture the user's input into a new variable calledBACKUP_FILENAME. - Add a final
echocommand 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_DIRset to/home/labex/project/app_logs. - The script must use the
readcommand 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_FILENAMEvariable.
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
readcommand 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.shscript. - Add an
ifstatement to check if the directory specified by the$LOG_DIRvariable 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
ifstatement. - The condition must check for the existence of a directory using the
-dtest operator. - If the directory does not exist, the script must
echothe 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
ifstatement isif [ condition ]; then ... else ... fi. - The test
[ -d "$DIRECTORY_PATH" ]returns true if$DIRECTORY_PATHexists and is a directory. exit 1is 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
backupsinside~/projectwhere the log files will be copied. - Modify your
log_manager.shscript. - Inside the
ifblock (where the directory is confirmed to exist), add aforloop. - The loop should iterate over every file ending in
.loginside the$LOG_DIRdirectory. - Inside the loop, use the
cpcommand to copy each log file into the~/project/backupsdirectory. - For each file copied, print a message like "Copied [filename]".
Requirements
- You must first create the
~/project/backupsdirectory from the command line. - The script must use a
forloop. - The loop must iterate through files in
$LOG_DIRthat match the*.logpattern. - The
cpcommand must be used inside the loop to copy files to~/project/backups. - An
echostatement 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
mkdircommand. - A
forloop for files can be written asfor file in /path/to/*.log; do ... done. - The
cpcommand syntax iscp <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
ifstatements. - Automating repetitive tasks with
forloops. - 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!



