docker run -it ubuntu bash ( running bash terminal using docker )
How to save changes by creating an image for the bash terminal that you have already used.
Use Docker Commit to Create a New Image
If you've made changes (like installing software or creating files) inside a running container and want to save
the final state as a new, reusable image, you can use the docker commit command [1, 3].
Steps:
1. Run your container normally:
bash
docker run -it --name my_bash_session ubuntu bash
2. Do your work inside the container.
3. Exit the container. It will stop, but its state is saved until you explicitly remove it.
4. Commit the changes to a new image:
bash
docker commit my_bash_session new_image_name
5. You can now run a new container from new_image_name, and your saved files will be there:
bash
docker run -it new_image_name bash
This phase answers one core question:
How does Linux execute a shell script from top to bottom?
1️⃣ What EXACTLY is a Shell Script?
A shell script is:
A plain text file
Contains Linux commands
Executed by a shell interpreter (bash)
Example:
ls
pwd
date
When saved in a file → Linux runs these commands line by line.
2️.Script vs Command (Critical Difference)
Command Script
One instruction Multiple instructions
Manual execution Automated execution
Temporary Reusable
Human-driven System-driven
DevOps lives on scripts, not commands.
3️⃣ Script Anatomy (VERY IMPORTANT)
Every professional script has 3 mandatory layers:
#!/bin/bash ← Interpreter
# comments ← Documentation
commands ← Logic
4️⃣ The Shebang (#!) — Deep Explanation
#!/bin/bash
What it means:
#! → kernel instruction
/bin/bash → path of shell
Linux kernel says:
“Run this file using bash”
Without shebang:
Script may fail
Wrong shell may execute
DevOps pipelines break
5️⃣ Find Your Bash Path (Practice)
Inside Docker container:
which bash
Output:
/bin/bash
So we use:
#!/bin/bash
6️⃣ Creating Your First Script (Hands-On)
Step 1: Create File
nano [Link]
Step 2: Write Script
#!/bin/bash
echo "Hello Shell Scripting"
date
pwd
Step 3: Save
CTRL + O
Enter
CTRL + X
7️⃣ Script Permissions (MOST CONFUSING FOR BEGINNERS)
Linux protects files using permissions.
Check permissions
ls -l [Link]
Example output:
-rw-r--r-- 1 root root 45 [Link]
Permission Meaning
-rw-r--r--
|| |
| | └── Others
| └──── Group
└────── Owner
Symbol Meaning
r read
w write
x execute
8️⃣ Make Script Executable
chmod +x [Link]
Verify:
ls -l [Link]
Now you’ll see:
-rwxr-xr-x
9️⃣ Running the Script (3 Methods)
✅ Method 1 (BEST)
./[Link]
Method 2
bash [Link]
❌ Method 3 (WRONG)
[Link]
Linux does NOT search current directory by default.
10️⃣ Why ./ is REQUIRED
. → current directory
/ → separator
So:
./[Link]
Means:
Run [Link] from current directory
11️⃣ Script Execution Flow (How Linux Thinks)
Steps:
1. You run ./[Link]
2. Kernel checks execute permission
3. Reads shebang
4. Loads /bin/bash
5. Bash executes line by line
12️⃣ Comments (Professional Rule)
# This is a comment
Used for:
Documentation
Debugging
Security warnings
Example:
#!/bin/bash
# Author: Manju
# Purpose: Learning shell scripting
echo "Learning Shell"
13️⃣ Exit Codes (VERY IMPORTANT IN DEVOPS)
Every script returns a number.
Code Meaning
0 Success
1+ Failure
Check last exit status:
echo $?
Example
ls /wrongpath
echo $?
Output:
DevOps tools rely on this.
14️⃣ Safe Script Template (Use This Always)
#!/bin/bash
set -e
set -u
echo "Script started"
Command Meaning
set -e Exit on error
set -u Error on unset variables
15️⃣ Practice Tasks (DO THIS)
Task 1
Create script that prints:
Date
Current user
Current directory
Task 2
Create script:
Create folder logs
Create file [Link]
Write date into file
Task 3
Break the script intentionally → observe $?
16️ Common Beginner Mistakes (Avoid These)
❌ Missing shebang
❌ Forgetting chmod +x
❌ Using Windows editors (CRLF issue)
❌ Hardcoding paths
❌ Not checking exit codes
To "edit" your script without an editor, use a Heredoc. This allows you to write multiple lines
directly into the file from the command line:
1. Re-write the script correctly
Run this command exactly. It will overwrite first_ss.sh with the correct multi-line format:
bash
cat << 'EOF' > first_ss.sh
#!/usr/bin/bash
date
pwd
EOF
Use code with caution.
What this does: cat << 'EOF' > first_ss.sh tells Bash to take everything you type until it sees
the word EOF and save it into the file.
Why the previous error happened: Your earlier command put #!/usr/bin/bash date pwd all
on one line. The date command interpreted pwd as a date string it didn't recognize, hence
the "invalid date 'pwd'" error.
2. Verify and Run
Since you already granted execute permissions earlier, you can just run it:
bash
cat first_ss.sh # Confirm it shows 3 separate lines
./first_ss.sh # Execute the script
Use code with caution.
3. Alternative: Installing an Editor
If you prefer a real editing experience, you can install a lightweight editor like nano or vim-
tiny if your container has internet access and uses apt (Debian/Ubuntu):
bash
apt update && apt install -y nano
nano first_ss.sh
Use code with caution.
4. Why set -o vi didn't work
set -o vi only changes the command-line editing mode (how you edit the text you are
currently typing at the prompt). It does not provide a text editor for existing files; it just lets
you use Vim shortcuts to navigate your command history.
Good.
We now move to PHASE 4 — Variables & Input, taught slow, deep, practical, exactly how a
senior DevSecOps engineer explains to beginners.
🔢 PHASE 4 — Variables & User Input (DEEP + PRACTICAL)
This phase answers three core questions:
1. How does Bash store data?
2. How does a script take input from users?
3. How does data flow inside scripts?
1️⃣ What is a Variable in Shell?
A variable is a named memory location that stores data temporarily while a script runs.
In shell:
Everything is treated as text unless told otherwise.
2️⃣ Variable Rules (VERY IMPORTANT)
Correct
name="Manju"
❌ Wrong
name = "Manju" # spaces break shell
Rules:
✔ No spaces around =
✔ Case-sensitive (Name ≠ name)
✔ Use meaningful names
✔ Always quote strings
3️⃣ Accessing Variables
name="Manju"
echo $name
$ means:
“Fetch the value stored inside variable”
Without $ → you only get the variable name.
4️⃣ Variable Lifecycle (How Bash Thinks)
1. Script starts
2. Variable created
3. Value used
4. Script ends
5. Variable destroyed
👉 No permanent storage unless written to file.
5️⃣ Types of Variables in Shell
🔹 1. User-defined Variables
city="Hyderabad"
🔹 2. System Variables
Already created by Linux.
echo $HOME
echo $USER
echo $SHELL
echo $PATH
🔹 3. Environment Variables
Shared with child processes.
export APP_ENV="production"
Check:
env
6️⃣ Quoting Variables (CRITICAL)
Double Quotes ✅ (Preferred)
echo "$name"
✔ Expands variables
✔ Allows spaces
Single Quotes ❌
echo '$name'
Output:
$name
Used only for literal strings.
7️⃣ Command Substitution (Very Powerful)
Store output of a command into a variable.
Old style
today=`date`
Modern style (BEST)
today=$(date)
Example:
echo "Today is $today"
8️⃣ Taking User Input — read
Basic Input
read name
echo "Hello $name"
Professional Input Prompt
read -p "Enter your name: " name
echo "Welcome $name"
Silent Input (Passwords)
read -s -p "Enter password: " pass
echo
9️⃣ Multiple Inputs
read -p "Enter username and age: " user age
echo "User: $user, Age: $age"
🔟 Default Values (Defensive Coding)
read -p "Enter country: " country
country=${country:-India}
If user presses Enter → value becomes India.
1️⃣1️⃣ Special Variables (DevOps GOLD)
Variable Meaning
$0 Script name
$1 First argument
$2 Second argument
$# Number of arguments
$@ All arguments
$? Last exit code
$$ Process ID
Example (Arguments)
#!/bin/bash
echo "Script: $0"
echo "First arg: $1"
echo "Second arg: $2"
Run:
./[Link] AWS Docker
1️⃣2️⃣ Arithmetic Operations (IMPORTANT LIMITATION)
Shell does NOT do math directly.
Correct way:
a=10
b=5
sum=$((a + b))
echo $sum
Using expr
expr 10 + 5
1️⃣3️⃣ Readonly Variables (Security)
readonly PI=3.14
PI=4 # error
Used in secure scripts.
1️⃣4️⃣ Unset Variables
unset name
Good for:
Memory cleanup
Security
Avoid leaks
1️3 Common Beginner Mistakes (IMPORTANT)
❌ Spaces around =
❌ Using single quotes for variables
❌ Forgetting $
❌ No input validation
❌ Assuming numeric behavior
1️⃣6️⃣ Professional Script Example
#!/bin/bash
set -e
set -u
read -p "Enter service name: " service
read -p "Enter environment: " env
echo "Deploying $service to $env environment"
🧠 PHASE 4 — YOU NOW UNDERSTAND
✔ Variable creation & access
✔ User input handling
✔ Environment vs system variables
✔ Command substitution
✔ Script arguments
✔ Defensive scripting
🔥 PRACTICE TASKS (DO THESE)
Task 1
Ask user:
Name
City
Print greeting sentence
Task 2
Pass two numbers as arguments → print sum
Task 3
Store output of df -h into variable and display it
Task 4
Create script that accepts password silently
Good.
We now enter PHASE 5 — CONDITIONS (if, elif, else, case), taught deeply, practically, and
visually, exactly how a senior DevSecOps engineer trains beginners.
🧠 PHASE 5 — Decision Making in Shell Scripting (DEEP)
This phase answers one critical question:
How does a shell script THINK and DECIDE what to do next?
1️⃣ Why Conditions Exist (Real-World Meaning)
Without conditions, a script is blind.
With conditions, a script can:
Check disk space
Verify files
Validate users
Control deployments
Stop failures automatically
👉 DevOps automation = Conditions + Scripts
2️⃣ The if Statement (Foundation)
Basic Syntax
if [ condition ]
then
command
fi
Mental Model
“IF this condition is true → DO something”
3️⃣ Your First if Script (Hands-On)
#!/bin/bash
read -p "Enter a number: " num
if [ $num -gt 10 ]
then
echo "Number is greater than 10"
fi
If condition fails → script silently skips block.
4️⃣ if – else (Two-Way Decision)
if [ $num -gt 10 ]
then
echo "Greater than 10"
else
echo "10 or less"
fi
Only ONE block executes.
5️⃣ if – elif – else (Multiple Decisions)
read -p "Enter marks: " marks
if [ $marks -ge 90 ]
then
echo "Grade A"
elif [ $marks -ge 75 ]
then
echo "Grade B"
else
echo "Grade C"
fi
Execution:
Top to bottom
First TRUE wins
Others ignored
6️⃣ The [ condition ] Brackets (VERY IMPORTANT)
This is NOT decoration.
[ condition ]
Internally:
[ is a command
Needs spaces
Needs closing ]
❌ Wrong:
if [$num -gt 10]
✅ Correct:
if [ $num -gt 10 ]
7️⃣ Numeric Comparison Operators
Operator Meaning
-eq equal
-ne not equal
-gt greater
-lt less
-ge greater or equal
-le less or equal
Example:
if [ $a -eq $b ]
8️⃣ String Comparisons (Common Mistake Area)
name="admin"
if [ "$name" = "admin" ]
then
echo "Admin user"
fi
Operator Meaning
= equal
!= not equal
-z string is empty
-n string not empty
⚠ Always quote strings:
"$var"
9️⃣ File & Directory Conditions (DevOps GOLD)
file="[Link]"
if [ -f "$file" ]
then
echo "File exists"
fi
Test Meaning
-f file exists
-d directory exists
-e exists
-r readable
-w writable
-x executable
🔟 Logical Operators (AND / OR)
AND (&&)
if [ $age -ge 18 ] && [ $age -le 60 ]
OR (||)
if [ "$user" = "root" ] || [ "$user" = "admin" ]
1️⃣1️⃣ Exit Status Based Conditions (PRO LEVEL)
Every command returns exit code.
if ls /tmp >/dev/null 2>&1
then
echo "Command succeeded"
else
echo "Command failed"
fi
This is how DevOps scripts detect failures.
1️⃣2️⃣ case Statement (Cleaner Than if)
Used when:
Many options
Menu-driven scripts
User choices
Syntax
case $variable in
pattern1)
command ;;
pattern2)
command ;;
*)
default ;;
esac
1️⃣3️⃣ case Example (Realistic)
read -p "Enter option (start|stop|restart): " action
case $action in
start)
echo "Starting service" ;;
stop)
echo "Stopping service" ;;
restart)
echo "Restarting service" ;;
*)
echo "Invalid option" ;;
esac
Why case?
✔ Cleaner
✔ Faster
✔ Safer
1️⃣4️⃣ Nested Conditions (Avoid When Possible)
if [ $a -gt 0 ]
then
if [ $b -gt 0 ]
then
echo "Both positive"
fi
fi
Use only when necessary.
1️⃣5️⃣ Defensive Condition Writing (PRO RULES)
✔ Quote variables
✔ Validate user input
✔ Use set -e
✔ Handle else case
✔ Never assume success
1️⃣6️⃣ Professional DevOps Example
#!/bin/bash
set -e
THRESHOLD=80
USAGE=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
if [ "$USAGE" -gt "$THRESHOLD" ]
then
echo "Disk usage critical: $USAGE%"
else
echo "Disk usage normal"
fi
This is real production logic.
🧠 PHASE 5 — YOU NOW MASTER
✔ if / else / elif
✔ Numeric, string, file checks
✔ Logical operators
✔ Exit code based logic
✔ case statements
✔ DevOps-grade decision making
🔥 PRACTICE TASKS (MANDATORY)
Task 1
Ask user age → check eligibility to vote.
Task 2
Check if file exists → create if missing.
Task 3
Menu-based script using case.
Task 4
Check service name input → allow only predefined values.
Perfect.
Now we go PHASE 6 — LOOPS, taught slow, deep, visual, and practical, exactly how a senior
DevSecOps engineer trains beginners.
🔁 PHASE 6 — Loops in Shell Scripting (DEEP + PRACTICAL)
This phase answers one core question:
How does a shell script repeat work automatically without writing the same code again and again?
1️⃣ What is a Loop? (Human Explanation)
A loop means:
“Do this task again and again until a condition changes.”
Real-life analogy:
Washing dishes → one plate at a time
Attendance → one student at a time
Server checks → one server at a time
👉 Automation = loops
2️⃣ Why Loops Are CRITICAL in DevOps
Loops are used to:
Iterate servers
Process files
Monitor resources
Automate deployments
Run health checks
Without loops → no automation
🔹 TYPES OF LOOPS IN SHELL
Loop Use Case
for Known number of iterations
while Unknown / condition-based
until Run until condition becomes true
🔁 3️⃣ for Loop (MOST USED)
Basic Syntax
for variable in list
do
commands
done
Mental Model:
“For EACH item → DO something”
Example 1 — Simple Numbers
for i in 1 2 3 4 5
do
echo "Number: $i"
done
Example 2 — Using Range
for i in {1..5}
do
echo "Count: $i"
done
Example 3 — Files in Directory (DevOps GOLD)
for file in *.log
do
echo "Processing $file"
done
Example 4 — Command Output Loop
for user in $(cat [Link])
do
echo "User: $user"
done
⚠ Use carefully (word-splitting risk).
Example 5 — C-Style for Loop
for ((i=1; i<=5; i++))
do
echo $i
done
Most familiar for programmers.
🔄 4️⃣ while Loop (CONDITION-BASED)
Syntax
while [ condition ]
do
commands
done
Mental Model:
“WHILE this is true → keep running”
Example 1 — Counter
count=1
while [ $count -le 5 ]
do
echo "Count: $count"
((count++))
done
Example 2 — Infinite Loop (MONITORING)
while true
do
echo "Monitoring..."
sleep 5
done
Used in:
Health checks
Daemons
Watch scripts
Example 3 — Read File Line by Line (VERY IMPORTANT)
while read line
do
echo "$line"
done < [Link]
✔ Safe
✔ Recommended
✔ Production-grade
⏳ 5️⃣ until Loop (OPPOSITE OF WHILE)
Syntax
until [ condition ]
do
commands
done
Mental Model:
“Run UNTIL this becomes true”
Example
count=1
until [ $count -gt 5 ]
do
echo "Count: $count"
((count++))
done
Less common, but useful.
🛑 6️⃣ Loop Control Statements
break — Stop Loop
for i in {1..10}
do
if [ $i -eq 5 ]
then
break
fi
echo $i
done
continue — Skip Current Iteration
for i in {1..5}
do
if [ $i -eq 3 ]
then
continue
fi
echo $i
done
🔍 7️⃣ Nested Loops (Use Carefully)
for i in 1 2
do
for j in a b
do
echo "$i $j"
done
done
Avoid deep nesting → hard to debug.
⚠️8️⃣ Common Beginner Mistakes
❌ Forgetting do / done
❌ Infinite loops unintentionally
❌ Not updating loop variable
❌ Using for where while is safer
❌ Reading files using for line in $(cat file)
🧪 9️⃣ Real DevOps Examples
Example 1 — Server Health Check
for server in server1 server2 server3
do
ping -c 1 $server >/dev/null || echo "$server down"
done
Example 2 — Log Rotation
for file in *.log
do
mv "$file" "$[Link]"
done
Example 3 — Wait for Service
until systemctl is-active nginx >/dev/null
do
echo "Waiting for nginx..."
sleep 2
done
🧠 PHASE 6 — YOU NOW UNDERSTAND
✔ for, while, until
✔ When to use each
✔ Loop control (break, continue)
✔ File & command iteration
✔ Real DevOps automation use cases
🔥 PRACTICE TASKS (MANDATORY)
Task 1
Print numbers 1–10 using:
for
while
until
Task 2
Read a file line by line and print it.
Task 3
Create infinite loop that prints time every 5 seconds.
Task 4
Loop through directory → count files.
Perfect.
Now we move to PHASE 7 — FUNCTIONS, taught deep, slow, practical, and production-grade,
exactly how a senior DevSecOps engineer trains beginners.
🧩 PHASE 7 — Functions in Shell Scripting (DEEP + PRACTICAL)
This phase answers one powerful question:
How do we write clean, reusable, professional shell scripts instead of messy command files?
1️⃣ What is a Function? (Human Explanation)
A function is:
A named block of code that performs one specific task and can be reused many times.
Real-life analogy:
Mobile phone → Contacts, Camera, Messages (functions)
Laptop → Copy, Paste, Shutdown (functions)
👉 Functions bring structure and discipline to scripts.
2️⃣ Why Functions Are CRITICAL in DevOps
Without functions:
Huge scripts
Repeated code
Hard to debug
Easy to break production
With functions:
✔ Clean code
✔ Reusability
✔ Faster debugging
✔ Team-friendly scripts
👉 All production DevOps scripts use functions
3️⃣ Function Syntax (MUST MEMORIZE)
Standard Syntax
function_name() {
commands
OR
function function_name {
commands
}
✅ First style is preferred.
4️⃣ Your First Function (Hands-On)
#!/bin/bash
greet() {
echo "Hello from function"
greet
What happens:
1. Function is defined
2. Function is called
3. Code executes
⚠ Defining ≠ Executing
You must call the function.
5️⃣ Function Execution Flow (IMPORTANT)
Flow:
1. Script starts
2. Function definitions loaded into memory
3. Execution reaches function call
4. Function code runs
5. Control returns back
6️⃣ Functions with Arguments (VERY IMPORTANT)
Functions accept arguments just like scripts.
welcome() {
echo "Welcome $1"
welcome Manju
Symbol Meaning
$1 First argument
$2 Second argument
$@ All arguments
$# Argument count
7️⃣ Realistic Function Example
create_user() {
echo "Creating user: $1"
create_user devops
create_user tester
👉 Same function, different inputs.
8️⃣ Returning Values from Functions (LIMITATION)
Shell functions cannot return values directly like other languages.
❌ Wrong thinking
return name
✅ Correct ways
Method 1 — Echo Output (MOST COMMON)
get_date() {
echo "$(date)"
today=$(get_date)
echo "Today: $today"
Method 2 — Use Global Variable
result=""
calculate() {
result=$((2 + 3))
calculate
echo $result
Method 3 — Exit Codes (Boolean Logic)
check_file() {
[ -f "$1" ]
check_file [Link]
echo $?
Used heavily in DevOps pipelines.
9️⃣ Local vs Global Variables (CRITICAL)
By default → all variables are global.
Local Variable
my_func() {
local temp=10
echo $temp
}
✔ Prevents data corruption
✔ Mandatory in professional scripts
🔟 Nested Functions (Allowed, Rare)
outer() {
inner() {
echo "Inside inner"
inner
outer
Avoid unless required.
1️⃣1️⃣ Function Libraries (PRO PRACTICE)
Functions can be reused across scripts.
File: [Link]
log() {
echo "[INFO] $1"
Main Script
#!/bin/bash
source [Link]
log "Service started"
✔ Modular
✔ Scalable
✔ Industry standard
1️⃣2️⃣ Error Handling with Functions (PRO LEVEL)
check_root() {
if [ "$EUID" -ne 0 ]
then
echo "Run as root"
exit 1
fi
check_root
Used in:
Installers
System scripts
Security checks
1️⃣3️⃣ DevOps-Grade Example Script
#!/bin/bash
set -e
log() {
echo "[INFO] $1"
check_disk() {
usage=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
if [ "$usage" -gt 80 ]
then
log "Disk usage critical: $usage%"
else
log "Disk usage normal"
fi
log "Starting system check"
check_disk
This is real production quality.
1️⃣4️⃣ Common Beginner Mistakes
❌ Forgetting to call function
❌ Using global variables everywhere
❌ Returning values incorrectly
❌ Writing huge functions
❌ No error handling
🧠 PHASE 7 — YOU NOW MASTER
✔ Function syntax
✔ Arguments & return patterns
✔ Local vs global variables
✔ Reusable function libraries
✔ DevOps-grade structure
🔥 PRACTICE TASKS (MANDATORY)
Task 1
Create function to:
Accept name
Print greeting
Task 2
Function to:
Accept file name
Check if exists
Task 3
Create logging function → reuse in script
Task 4
Create function that checks:
Root user
Disk space
Internet connectivity
Understood.
Now we go PHASE 8 — FILE HANDLING, taught deeply, practically, and production-grade, exactly
how a senior DevSecOps engineer trains beginners.
📂 PHASE 8 — File Handling in Shell Scripting (DEEP + PRACTICAL)
This phase answers one fundamental DevOps question:
How does a shell script create, read, write, append, and safely manipulate files?
If you master this phase, you can:
Build log systems
Create backup scripts
Automate reports
Handle configs
Write production-grade automation
1️⃣ How Shell Sees Files (Mental Model)
In Linux:
Everything is a file
Logs, configs, devices, sockets → files
A shell script:
Reads files
Writes files
Redirects output
Processes content line-by-line
2️⃣ File Creation (Basics)
Create Empty File
touch [Link]
Create File with Content
echo "Application started" > [Link]
⚠ > overwrites existing content.
3️⃣ Writing vs Appending (VERY IMPORTANT)
Overwrite (>)
echo "New run" > [Link]
Append (>>)
echo "Another entry" >> [Link]
📌 90% of production scripts use >>
4️⃣ Reading Files (Multiple Ways)
Read Entire File
cat [Link]
Read First Lines
head [Link]
head -n 5 [Link]
Read Last Lines (Logs!)
tail [Link]
tail -f [Link]
5️⃣ Reading File Line-by-Line (PRO LEVEL)
✅ Correct Way
while read line
do
echo "$line"
done < [Link]
Why this is best:
Handles spaces
Handles large files
No word splitting
Production-safe
❌ Wrong Way (Common Beginner Mistake)
for line in $(cat [Link])
do
echo "$line"
done
Breaks on spaces → never use in production
6️⃣ File Existence & Type Checks (MANDATORY)
file="[Link]"
if [ -f "$file" ]
then
echo "File exists"
else
echo "File missing"
fi
File Test Operators
Test Meaning
-f regular file
-d directory
-e exists
-r readable
-w writable
-x executable
7️⃣ Creating Directories Safely
mkdir logs
Safe Creation
mkdir -p logs/archive
Does not fail if directory already exists.
8️⃣ Copy, Move, Delete Files (SCRIPT SAFE)
cp [Link] [Link]
mv [Link] logs/
rm [Link]
Safer Delete (Check First)
if [ -f [Link] ]
then
rm [Link]
fi
9️⃣ File Permissions (DevOps-Critical)
ls -l [Link]
Change permissions:
chmod 644 [Link]
chmod +x [Link]
Number Meaning
7 rwx
6 rw-
5 r-x
4 r--
🔟 Ownership (Admin Scripts)
chown root:root [Link]
Used in:
Install scripts
Security hardening
CI/CD runners
1️⃣1️⃣ File Descriptors (ADVANCED BUT IMPORTANT)
FD Meaning
0 stdin
1 stdout
2 stderr
Redirect Output
command > [Link]
Redirect Errors
command 2> [Link]
Redirect Both
command > [Link] 2>&1
1️⃣2️⃣ Logging in Scripts (PRO PRACTICE)
LOG_FILE="/var/log/[Link]"
echo "$(date) App started" >> "$LOG_FILE"
With Function
log() {
echo "$(date) $1" >> [Link]
1️⃣3️⃣ Here Documents (HEREDOC)
Used to write multi-line content.
cat <<EOF > [Link]
APP_ENV=prod
PORT=8080
DEBUG=false
EOF
Used in:
Config generation
Docker entrypoints
Cloud-init scripts
1️⃣4️⃣ Temporary Files (SECURITY PRACTICE)
temp=$(mktemp)
echo "temp data" > "$temp"
rm "$temp"
Avoid predictable filenames.
1️⃣5️⃣ Find & Process Files (DevOps GOLD)
find /var/log -name "*.log"
With loop:
for file in $(find . -name "*.log")
do
echo "$file"
done
(Advanced handling comes later.)
1️⃣6️⃣ Real DevOps File Handling Script
Log Cleanup Script
#!/bin/bash
set -e
LOG_DIR="/var/log/myapp"
if [ -d "$LOG_DIR" ]
then
find "$LOG_DIR" -name "*.log" -mtime +7 -delete
echo "Old logs cleaned"
else
echo "Log directory not found"
fi
This is real production automation.
1️⃣7️⃣ Common Beginner Mistakes
❌ Overwriting files accidentally
❌ Not quoting file names
❌ Deleting without checks
❌ Using unsafe loops
❌ Ignoring permissions
🧠 PHASE 8 — YOU NOW UNDERSTAND
✔ Create, read, write, append files
✔ Safe file checks
✔ Directory handling
✔ Permissions & ownership
✔ Redirection & logging
✔ Production-grade file automation
🔥 PRACTICE TASKS (MANDATORY)
Task 1
Create script that:
Creates logs/
Writes date into logs/[Link]
Task 2
Read a file line-by-line and count lines.
Task 3
Check if config file exists → create default if missing.
Task 4
Create cleanup script:
Delete .log files older than 5 days.
Excellent.
Now we move to PHASE 9 — PROCESS & SYSTEM HANDLING, taught like a senior DevSecOps
engineer, with clear mental models, real commands, and production use cases.
This phase is CRITICAL because this is where shell scripting becomes real DevOps automation.
⚙️PHASE 9 — Process & System Handling (DEEP + PRACTICAL)
This phase answers one core question:
How does Linux run programs, track them, monitor system resources, and how can a shell script
control all of that?
1️⃣ What is a Process? (Human Explanation)
A process is:
A running instance of a program.
Example:
ls → command
When executed → becomes a process
After completion → process dies
Every process has:
PID (Process ID)
Owner
State
Resource usage (CPU, Memory)
2️⃣ Process Lifecycle (Mental Model)
States:
Created
Running
Sleeping
Stopped
Zombie
Terminated
Shell scripts often:
Check if process is running
Kill stuck processes
Restart failed services
3️⃣ Viewing Processes (ps)
Basic
ps
All Processes (MOST USED)
ps aux
Meaning:
a → all users
u → user format
x → background processes
Filter Processes
ps aux | grep nginx
⚠ grep itself appears — normal behavior.
4️⃣ Real-Time Monitoring (top, htop)
top
Shows:
CPU usage
Memory usage
Running processes
Load average
DevOps uses this to:
Detect CPU spikes
Find memory hogs
5️⃣ Process IDs (PID) — VERY IMPORTANT
echo $$
→ PID of current shell
Find PID of process:
pidof nginx
or
ps aux | grep nginx
6️⃣ Killing Processes (CONTROL)
Graceful Kill
kill PID
Force Kill (Last Option)
kill -9 PID
Signal meanings:
Signal Meaning
15 SIGTERM (polite)
9 SIGKILL (force)
👉 Always try SIGTERM first
7️⃣ Background & Foreground Processes
Run in Background
sleep 100 &
List Background Jobs
jobs
Bring to Foreground
fg %1
8️⃣ Waiting for Processes (wait)
Shell scripts often start background jobs.
sleep 5 &
pid=$!
wait $pid
echo "Process completed"
Variable Meaning
$! PID of last background process
9️⃣ Checking if Process is Running (SCRIPT LOGIC)
if pgrep nginx >/dev/null
then
echo "nginx is running"
else
echo "nginx is NOT running"
fi
This is production-grade logic.
🔟 System Resource Monitoring
CPU Load
uptime
Memory Usage
free -m
Disk Usage
df -h
Disk I/O
iostat
(Advanced systems)
1️⃣1️⃣ Automating Resource Checks (REAL DEVOPS)
Disk Usage Alert Script
#!/bin/bash
THRESHOLD=80
USAGE=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
if [ "$USAGE" -gt "$THRESHOLD" ]
then
echo "Disk usage critical: $USAGE%"
else
echo "Disk usage OK: $USAGE%"
fi
This script is interview-grade.
1️⃣2️⃣ Services & Daemons (SYSTEM LEVEL)
Check service status:
systemctl status ssh
Start service:
systemctl start ssh
Stop service:
systemctl stop ssh
Restart:
systemctl restart ssh
Scripts often wrap these commands.
1️⃣3️⃣ Process Exit Codes (AUTOMATION LOGIC)
Every command returns exit code.
command
echo $?
Example:
ping -c 1 [Link] >/dev/null
if [ $? -eq 0 ]
then
echo "Internet OK"
else
echo "Internet DOWN"
fi
CI/CD systems rely on this.
1️⃣4️⃣ Signals & Traps (ADVANCED BUT IMPORTANT)
trap 'echo "Script interrupted"; exit' SIGINT SIGTERM
Used to:
Cleanup temp files
Handle CTRL+C
Graceful shutdown
1️⃣5️⃣ Real DevOps Monitoring Script
#!/bin/bash
set -e
check_service() {
if pgrep "$1" >/dev/null
then
echo "$1 is running"
else
echo "$1 is DOWN"
fi
check_service nginx
check_service docker
This is real-world production logic.
1️⃣6️⃣ Common Beginner Mistakes
❌ Killing wrong PID
❌ Using kill -9 always
❌ Ignoring exit codes
❌ No monitoring loops
❌ No logging
🧠 PHASE 9 — YOU NOW UNDERSTAND
✔ What processes are
✔ How to view & control them
✔ CPU, memory, disk monitoring
✔ Background & foreground jobs
✔ Exit codes & signals
✔ Real DevOps automation logic
🔥 PRACTICE TASKS (MANDATORY)
Task 1
Create script that:
Checks if a process is running
Prints PID
Task 2
Write script:
Monitor disk usage
Alert if >75%
Task 3
Start background job → wait → print completion
Task 4
Write service checker for:
docker
ssh
Perfect.
Now we enter the FINAL CORE PHASE of this journey.
You are no longer a “beginner”.
From here on, you think like a DevOps / DevSecOps engineer.
🚀 PHASE 10 — DevOps Automation Scripts (PRODUCTION-GRADE)
This phase answers the most important question:
How do real DevOps engineers use shell scripting to automate systems, pipelines, servers, and
cloud workflows?
Everything here is real-world, interview-relevant, and job-ready.
1️⃣ What “Automation” Really Means in DevOps
Automation means:
No manual intervention
Repeatable execution
Predictable outcomes
Safe failure handling
In DevOps, shell scripts automate:
Server setup
Health checks
CI/CD steps
Docker workflows
Backups
Monitoring
Cleanup tasks
👉 Shell scripting is the backbone of DevOps automation
2️⃣ Core Principles of DevOps Automation Scripts
Before code, understand principles:
✔ Idempotent (safe to run multiple times)
✔ Fail fast (stop on error)
✔ Logged (every action recorded)
✔ Parameterized (no hardcoding)
✔ Environment-aware (dev / test / prod)
3️⃣ Standard DevOps Script Template (MEMORIZE THIS)
#!/bin/bash
set -e
set -u
LOG_FILE="/var/log/[Link]"
log() {
echo "$(date '+%F %T') | $1" | tee -a "$LOG_FILE"
log "Script started"
This template:
Stops on error (set -e)
Prevents undefined variables (set -u)
Logs everything
4️⃣ Automation Script #1 — System Health Check
Goal
Check:
CPU load
Memory usage
Disk usage
Script
#!/bin/bash
set -e
THRESHOLD=80
log() {
echo "$(date) | $1"
CPU_LOAD=$(uptime | awk '{print $(NF-2)}' | tr -d ',')
MEMORY=$(free | awk '/Mem/ {printf "%.0f", $3/$2 * 100}')
DISK=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
log "CPU Load: $CPU_LOAD"
log "Memory Usage: $MEMORY%"
log "Disk Usage: $DISK%"
if [ "$DISK" -gt "$THRESHOLD" ]; then
log "WARNING: Disk usage critical"
fi
This is production-level monitoring logic.
5️⃣ Automation Script #2 — Docker Cleanup (VERY COMMON)
Problem
Docker fills disk with:
Old images
Stopped containers
Unused volumes
Script
#!/bin/bash
set -e
echo "Cleaning unused Docker resources..."
docker system prune -af
docker volume prune -f
echo "Docker cleanup completed"
Used in:
Servers
CI runners
Cloud VMs
6️⃣ Automation Script #3 — Backup Automation
Goal
Compress directory
Timestamp backup
Store safely
#!/bin/bash
set -e
SOURCE="/data"
BACKUP_DIR="/backups"
DATE=$(date +%F)
mkdir -p "$BACKUP_DIR"
tar -czf "$BACKUP_DIR/data-backup-$[Link]" "$SOURCE"
echo "Backup completed for $DATE"
Real DevOps scripts look exactly like this.
7️⃣ Automation Script #4 — Service Auto-Restart
Goal
Detect crashed service
Restart automatically
#!/bin/bash
set -e
SERVICE="nginx"
if systemctl is-active --quiet "$SERVICE"; then
echo "$SERVICE is running"
else
echo "$SERVICE is down, restarting..."
systemctl restart "$SERVICE"
fi
This logic powers self-healing systems.
8️⃣ Automation Script #5 — Environment Setup Script
Goal
Install packages
Prepare system
Run once or many times safely
#!/bin/bash
set -e
packages=(git curl [Link])
for pkg in "${packages[@]}"
do
if ! dpkg -s "$pkg" >/dev/null 2>&1; then
echo "Installing $pkg"
apt install -y "$pkg"
else
echo "$pkg already installed"
fi
done
This is idempotent automation.
9️⃣ Automation Script #6 — CI/CD Pipeline Step
#!/bin/bash
set -e
echo "Running tests..."
./[Link]
echo "Building application..."
docker build -t myapp:latest .
echo "Deployment successful"
This exact style runs inside:
Jenkins
GitHub Actions
GitLab CI
🔟 DevSecOps Add-Ons (SECURITY THINKING)
Always add:
✔ Input validation
✔ Permission checks
✔ Root user check
✔ No hardcoded secrets
✔ Minimal privileges
Example:
if [ "$EUID" -ne 0 ]; then
echo "Run as root"
exit 1
fi
1️⃣1️⃣ Scheduling Automation — cron
Run scripts automatically.
crontab -e
Example:
0 2 * * * /scripts/[Link]
Runs daily at 2 AM.
1️⃣2️⃣ Common Interview-Level Automation Questions
✔ Difference between manual vs automated ops
✔ Why set -e is important
✔ How to make script idempotent
✔ How scripts integrate with CI/CD
✔ How you monitor failures
You now can answer all of them.
🧠 PHASE 10 — YOU ARE NOW HERE
At this point, you can:
✔ Write real DevOps automation scripts
✔ Automate servers & Docker
✔ Build CI/CD steps
✔ Monitor & self-heal systems
✔ Think like a DevOps engineer
This is job-ready shell scripting.