Lesson 11 — Linux Shell Scripting
Lesson 11 — Linux Shell Scripting
Section titled “Lesson 11 — Linux Shell Scripting”Lesson Overview
Section titled “Lesson Overview”Imagine you’re responsible for managing 500 Linux servers.
Every morning you need to:
- Check disk usage
- Verify system uptime
- Restart failed services
- Create user accounts
- Backup important files
- Generate health reports
Doing this manually would take hours.
Instead, Linux administrators automate these tasks using Shell Scripts.
Shell scripting is one of the most valuable skills for Linux Administrators, Cloud Engineers, DevOps Engineers, Site Reliability Engineers (SREs), and Cybersecurity Professionals.
Whether you’re deploying cloud infrastructure, configuring Kubernetes clusters, automating backups, or responding to security incidents, shell scripting saves time and reduces human error.
Learning Objectives
Section titled “Learning Objectives”After completing this lesson, you will be able to:
- Understand what shell scripting is.
- Write and execute Bash scripts.
- Use variables.
- Accept user input.
- Work with conditional statements.
- Use loops and functions.
- Automate common Linux administration tasks.
- Apply shell scripting best practices.
What is Shell Scripting?
Section titled “What is Shell Scripting?”A Shell Script is a text file containing a sequence of Linux commands that are executed automatically.
Instead of typing commands individually:
pwd
ls
date
whoamiYou can place them inside a script:
#!/bin/bash
pwdlsdatewhoamiRun once.
Everything executes automatically.
Why Shell Scripting Matters
Section titled “Why Shell Scripting Matters”Shell scripting helps administrators:
- Automate repetitive tasks
- Reduce manual work
- Improve consistency
- Save time
- Reduce configuration errors
- Increase productivity
Automation is one of the core principles of modern IT operations.
What is Bash?
Section titled “What is Bash?”Bash (Bourne Again SHell) is the default command-line shell for most Linux distributions.
It provides:
- Command execution
- Variables
- Loops
- Functions
- Conditional statements
- Input and output redirection
Most Linux automation starts with Bash.
Creating Your First Script
Section titled “Creating Your First Script”Create a file.
nano hello.shAdd the following:
#!/bin/bash
echo "Welcome to GoHackersCloud Academy!"Save the file.
Making a Script Executable
Section titled “Making a Script Executable”Grant execute permission.
chmod +x hello.shRunning a Script
Section titled “Running a Script”Execute the script.
./hello.shOutput:
Welcome to GoHackersCloud Academy!The Shebang
Section titled “The Shebang”Every Bash script begins with:
#!/bin/bashThis tells Linux which interpreter should execute the script.
Comments
Section titled “Comments”Comments explain your code.
Example:
# Display system informationLinux ignores comments during execution.
Variables
Section titled “Variables”Variables store information.
Example:
#!/bin/bash
name="Student"
echo $nameOutput:
StudentVariables make scripts reusable and dynamic.
User Input
Section titled “User Input”Accept input from the keyboard.
#!/bin/bash
echo "Enter your name:"
read name
echo "Welcome $name"Output:
Enter your name:
Rohit
Welcome RohitEnvironment Variables
Section titled “Environment Variables”Linux provides built-in variables.
Examples:
echo $HOME
echo $USER
echo $PATH
echo $HOSTNAMEThese variables provide information about the current environment.
Conditional Statements
Section titled “Conditional Statements”Use if statements to make decisions.
Example:
#!/bin/bash
if [ -f test.txt ]
then
echo "File Exists"
fiThe script executes only if the condition is true.
If-Else Example
Section titled “If-Else Example”#!/bin/bash
read age
if [ $age -ge 18 ]
then
echo "Eligible"
else
echo "Not Eligible"
fiComparison Operators
Section titled “Comparison Operators”| Operator | Meaning |
|---|---|
| -eq | Equal |
| -ne | Not Equal |
| -gt | Greater Than |
| -lt | Less Than |
| -ge | Greater Than or Equal |
| -le | Less Than or Equal |
These operators are commonly used in conditions.
For Loops
Section titled “For Loops”Repeat a task multiple times.
Example:
for i in {1..5}
do
echo $i
doneOutput:
1
2
3
4
5While Loops
Section titled “While Loops”Execute while a condition remains true.
count=1
while [ $count -le 5 ]
do
echo $count
count=$((count+1))
doneFunctions
Section titled “Functions”Functions organize reusable code.
#!/bin/bash
greet()
{
echo "Welcome!"
}
greetFunctions make scripts easier to maintain.
Command-Line Arguments
Section titled “Command-Line Arguments”Scripts can accept parameters.
Example:
#!/bin/bash
echo "Hello $1"Run:
./hello.sh RohitOutput:
Hello RohitExit Codes
Section titled “Exit Codes”Every Linux command returns an exit status.
View the previous command’s exit code.
echo $?Common values:
| Code | Meaning |
|---|---|
| 0 | Success |
| Non-Zero | Error |
Exit codes are important when automating workflows.
Redirecting Output
Section titled “Redirecting Output”Save command output.
ls > files.txtAppend output.
date >> logfile.txtRedirect errors.
command 2> error.logScheduling Scripts
Section titled “Scheduling Scripts”Linux uses cron to execute scripts automatically.
Example:
Every day
↓
2:00 AM
↓
Backup ScriptCron automation is widely used in enterprise environments.
Practical Example 1 — System Information
Section titled “Practical Example 1 — System Information”#!/bin/bash
echo "Hostname:"
hostname
echo "Current User:"
whoami
echo "Kernel:"
uname -r
echo "Disk Usage:"
df -hPractical Example 2 — Backup Script
Section titled “Practical Example 2 — Backup Script”#!/bin/bash
cp -r /home/student/Documents /backupAutomates file backups.
Practical Example 3 — Health Check
Section titled “Practical Example 3 — Health Check”#!/bin/bash
echo "CPU"
top -bn1 | head
echo "Memory"
free -h
echo "Disk"
df -hUseful for server monitoring.
Shell Scripting in Cloud Computing
Section titled “Shell Scripting in Cloud Computing”Cloud Engineers automate:
- EC2 provisioning
- Software installation
- User creation
- Server configuration
- Application deployment
- Monitoring
- Backups
Cloud-init scripts also use Bash during server startup.
Shell Scripting in DevOps
Section titled “Shell Scripting in DevOps”DevOps Engineers use shell scripts for:
- CI/CD Pipelines
- Docker Builds
- Kubernetes Deployments
- Git Automation
- Infrastructure Provisioning
- Server Configuration
Bash remains a fundamental automation language.
Shell Scripting in Cybersecurity
Section titled “Shell Scripting in Cybersecurity”Security professionals automate:
- Log Analysis
- File Integrity Checks
- User Auditing
- Vulnerability Scanning
- Malware Detection
- Incident Response
Automation significantly improves investigation speed.
Common Commands
Section titled “Common Commands”Create script:
nano script.shMake executable:
chmod +x script.shRun script:
./script.shCheck syntax:
bash -n script.shDisplay exit status:
echo $?Real-World Example
Section titled “Real-World Example”A Linux administrator manages 200 cloud servers.
Instead of manually checking each server, a Bash script:
Connect to Server
↓
Check Disk Usage
↓
Verify Running Services
↓
Collect Logs
↓
Generate Report
↓
Email ResultsThis automation reduces hours of manual work to just a few minutes.
Best Practices
Section titled “Best Practices”As a Linux administrator:
- Write clear and readable scripts.
- Use meaningful variable names.
- Add comments to explain complex logic.
- Test scripts in a lab before production.
- Handle errors using exit codes.
- Avoid hardcoding passwords or secrets.
- Store scripts in version control systems like Git.
- Follow consistent coding standards.
Well-written scripts improve reliability, maintainability, and security.
Key Takeaways
Section titled “Key Takeaways”After completing this lesson, you should understand:
- What shell scripting is.
- How to create and execute Bash scripts.
- Variables and user input.
- Conditional statements.
- Loops and functions.
- Command-line arguments.
- Exit codes and output redirection.
- Enterprise scripting best practices.
Summary
Section titled “Summary”Shell scripting is one of the most powerful skills in Linux administration.
By automating repetitive tasks, Bash scripts improve efficiency, consistency, and reliability across cloud, DevOps, cybersecurity, and enterprise environments.
Throughout your career, you’ll use shell scripting to deploy infrastructure, configure servers, automate monitoring, manage users, perform backups, and respond to incidents.
Mastering Bash scripting is a major step toward becoming an efficient Linux Administrator, Cloud Engineer, DevOps Engineer, or Cybersecurity Professional.
Next Lesson
Section titled “Next Lesson”➡️ Lesson 12 — Linux Security Best Practices
In the next lesson, you’ll learn how to secure Linux systems by implementing user hardening, SSH security, firewall configuration, log monitoring, software updates, auditing, and enterprise security best practices.