Example: Internet connectivity check using ping and conditional statements.
Benefits for RHCSA & Sysadmins
Essential for automation in real-world tasks.
Common scripts: disk usage checks, log rotation, service management, network configuration.
Works across multiple servers for consistency.
Advantages of Learning Shell Scripting
Builds confidence with command line.
Saves time and prevents errors.
Easier transition to other automation tools (Python, Ansible, CI/CD).
Practical skill for Linux administration.
✅ Key Takeaway: Shell scripting is not about programming—it’s about working smarter by automating repetitive tasks. For RHCSA, mastering scripting is critical.
echo"Hello, this is my first shell script in RHEL 9.6"
Save & Exit: Ctrl+X, press Y, then Enter.
Make Executable: chmod +x firstscript.sh.
Run Script:
./firstscript.sh
Or bash firstscript.sh.
Output: Hello, this is my first shell script in RHEL 9.6.
Demo 2: Variables in Shell Scripts
Create Script: nano variables.sh.
Add Shebang: #!/bin/bash.
Declare Variables:
name="RHEL User"age=28
Use Variables:
echo"Hello, my name is $name and I am $age years old."
Save & Exit: Ctrl+X, Y, Enter.
Make Executable: chmod +x variables.sh.
Run Script: ./variables.sh.
Output: Hello, my name is RHEL User and I am 28 years old.
Create Script: nano if_basic.sh.
Shebang: #!/bin/bash.
Prompt & Read Input:
echo"Enter a number:"readnum
Condition:
if [ $num-gt10 ];thenecho"Number is greater than 10"fi
Make Executable: chmod +x if_basic.sh.
Run: ./if_basic.sh.
if-else Statement
Create Script: nano teststatements.sh.
Prompt & Read Input:
read-p"Enter your number: "num
Condition:
if [ $num-lt10 ];thenecho"$num is less than 10"elseecho"$num is greater than 10"fi
Make Executable: chmod +x teststatements.sh.
Run: ./teststatements.sh.
if-elif-else Ladder
Create Script: nano if_elif.sh.
Calculate Disk Usage:
usage=$(df/|tail-1|awk'{print $5}'|sed's/%//')
Conditions:
if [ $usage-lt50 ];thenecho"Disk usage is under control."elif [ $usage-lt80 ];thenecho"Disk usage is moderate."elseecho"Disk usage is critical! Take action."fi
Ensure scripts remain robust in production environments.
Scalability & Flexibility
Design for multiple machines and environments.
Support:
Environment variables
Configuration files
Command-line flags
Adapt to changing configurations without code changes.
Goal
Build scripts that are:
Reliable
Safe
Repeatable
Turn scripts into lightweight utilities for enterprise use.
✅ Key RHCSA Takeaway: Advanced scripting is about strategy, not memorizing commands. It’s about creating scripts that anticipate future needs, handle errors gracefully, and adapt to different environments.
Script Overview
Goal: Log messages and command outputs with timestamps into a file.
Purpose: Move beyond basic shell scripting (loops, if-else) to create smart, flexible, and reliable scripts.
Goal: Scripts should behave like real programs—structured, readable, and adaptable.
Core Principles
Planning Before Coding:
Identify decisions the script must make.
Define expected inputs and future flexibility.
Think Like a Developer:
Design reusable scripts for others, not just one-off tasks.
Logic & Flow Control
Logic is Key:
Anticipate errors and changing conditions.
Use decision trees for multiple outcomes (e.g., start/stop actions).
Environment Awareness:
Check disk space, memory, network before proceeding.
Fail-safes:
Recover gracefully from errors; provide helpful feedback.
Structure & Organization
Complex Flow Handling:
Use loops with break and continue.
Nest conditionals and loops for advanced logic.
Flags & Switches:
Allow user customization via command-line options.
Readability:
Keep logic organized for long-term maintainability.
User Communication
Interactive & Informative:
Scripts should inform users, request input, and explain errors clearly.
Logging:
Essential for automated scripts; logs show what happened and when.
Error Handling
Anticipate Failures:
Missing files, invalid input, command failures.
Graceful Recovery:
Provide clear messages or backup plans.
Scalability & Flexibility
Design for Multiple Environments:
Handle varying inputs and system configurations.
Adaptability:
Use environment variables, config files, and flags for easy adjustments.
Key Takeaways
Advanced scripting = strategy, not memorization.
Focus on:
Error handling
Adaptability
User communication
Reusability
Aim for scripts that are reliable, safe, and repeatable in production.
Creating a Script with Logging and Timestamps
Objective: To demonstrate the creation of a Bash script that performs system tasks and logs its actions with timestamps. This combines fundamental shell scripting skills with practical system administration.
Key Skills Demonstrated:
Creating and executing a Bash script.
Using variables and functions.
Implementing timestamp-based logging.
Using conditional execution (&&).
Managing file permissions (chmod).
Demo Walkthrough & Expanded Explanation
Step 1: Creating the Script File
The script is a plain text file containing commands for the shell to execute.
Command:bashnano script.sh
Explanation: This opens the nano text editor to create a new file named script.sh. The .sh extension is a convention indicating a shell script.
**Step 2: Script Contents and Breakdown
Let’s analyze the script line by line to understand its components.