Name: Abigail Hall
Date: 06/28/24
Objectives
Run commands more efficiently using advanced features of the Bash shell, shell scripts, and Red Hat Enterprise Linux utilities.
Run repetitive tasks with for loops.
Evaluate exit codes from commands and scripts.
Run tests with operators and create conditional structures with if statements.
Create and apply regular expressions to match data in text files.
Use grep to search files and data from piped commands.
Sections
1. Write Simple Bash Scripts
Guided Exercise:
Create a Simple Bash Script:
Open a terminal and use a text editor to create a new file named simple_script.sh.
use code
nano simple_script.sh
Add the following content to the file:
use code
#!/bin/bash
echo "Hello, World!"
Save the file and exit the editor.
Make the Script Executable:
Change the file permissions to make the script executable:
use code
chmod +x simple_script.sh
Run the Script:
Execute the script to verify its output:
use code
./simple_script.sh
2. Loops and Conditional Constructs in Scripts
Guided Exercise:
Create a Script with a For Loop:
Create a new script named loop_script.sh:
use code
nano loop_script.sh
Add the following content to the file:
use code
#!/bin/bash
for i in {1..5}
do
echo "Iteration $i"
done
Save and exit the editor.
Make the script executable:
use code
chmod +x loop_script.sh
Run the script:
use code
./loop_script.sh
Create a Script with Conditional Constructs:
Create a script named conditional_script.sh:
use code
nano conditional_script.sh
Add the following content to the file:
use code
#!/bin/bash
number=10
if [ $number -gt 5 ]
then
echo "The number is greater than 5."
else
echo "The number is 5 or less."
fi
Save and exit the editor.
Make the script executable:
use code
chmod +x conditional_script.sh
Run the script:
use code
./conditional_script.sh
3. Match Text in Command Output with Regular Expressions
Guided Exercise:
Use Regular Expressions with grep:
Create a text file named sample.txt with sample data:
use code
nano sample.txt
Add some text lines to the file:
use code examples
apple
banana
cherry
apple pie
banana split
Save and exit the editor.
Search for Text with grep:
Use grep to search for lines containing the word "apple":
use code
grep "apple" sample.txt
This command will output:
apple
apple pie
Use Regular Expressions for Pattern Matching:
Search for lines that start with a vowel using a regular expression:
use code
grep "^[aeiou]" sample.txt
This will return any lines beginning with a vowel.
Advanced Commands and Techniques
Evaluate Exit Codes:
Test a command and check its exit code:
use code
ls /nonexistent-directory
echo $?
The exit code 2 indicates that the directory does not exist.
Test Operators in Scripts:
Modify conditional_script.sh to use more operators:
use code
#!/bin/bash
number=10
if [ $number -gt 5 ] && [ $number -lt 15 ]
then
echo "The number is between 5 and 15."
elif [ $number -eq 10 ]
then
echo "The number is exactly 10."
else
echo "The number is outside the specified range."
fi
Save and run the updated script.
Use htop for Monitoring:
Install htop if not already installed:
use code
sudo yum install htop
Run htop to monitor system performance interactively:
bash
Copy code
htop
Results
By following these exercises, I have gained practical experience in writing and executing Bash scripts, using loops and conditionals, applying regular expressions, and managing system resources. These skills will significantly improve my efficiency in managing and automating tasks on Unix-like systems.
Conclusion
This lab provided hands-on experience with advanced Bash features and Red Hat Enterprise Linux utilities, enhancing my ability to perform repetitive tasks, evaluate command outcomes, and apply text-processing techniques. Mastery of these tools is essential for efficient system administration and scripting.
Future Improvements
Explore more complex Bash scripting techniques and utilities.
Integrate scripts with cron jobs for automated tasks.
Experiment with advanced regular expressions for complex text processing.
Set Up a Deferred Command:
Use the at command to schedule a command to run once at a future time. For example, schedule a message to be displayed after 5 minutes:
use code
echo "echo 'Task executed at $(date)'" | at now + 5 minutes
Verify the scheduled job with the atq command:
use code
atq
Remove a Scheduled Job:
Use the atrm command to remove a scheduled job if needed:
use code
atrm <job_id>
Edit the User's Crontab File:
Open the user's crontab file for editing:
use code
crontab -e
Add a cron job to run a script every day at 2:30 AM:
cron
use code
30 2 * * * /path/to/your/script.sh
Save and exit the editor.
List User's Cron Jobs:
Verify the scheduled cron jobs:
use code
crontab -l
Remove All User's Cron Jobs:
Remove all cron jobs for the current user:
use code
crontab -r
Edit the System-Wide Crontab File:
Open the system-wide crontab file for editing:
use code
sudo nano /etc/crontab
Add a system-wide cron job to run a script every Sunday at midnight:
cron
use code
0 0 * * 0 root /path/to/your/script.sh
Save and exit the editor.
Use the cron.d Directory:
Create a new cron file in the /etc/cron.d directory:
use code
sudo nano /etc/cron.d/weekly-job
Add the following content to run a script every week:
cron
use code
0 0 * * 0 root /path/to/your/weekly_script.sh
Save and exit the editor.