π Mastering Advanced Shell Scripting: Loops, User Management & File Automation(Day 5)
Table of contents
π Advanced Shell Scripting Made Simple: Loops, User Management & File Automation
Shell scripting is like giving instructions to a super-efficient assistant (your computer) to handle tasks automatically. If you've ever wished that your computer could do things on its own, like renaming files, managing users, or cleaning up old documents, then shell scripting is for you! π
Letβs break it down step by step with real-world examples, so even non-IT folks can understand and apply these concepts!
π Understanding Loops in Shell Scripting
Loops are like daily routinesβthey allow your computer to repeat tasks without you having to do it manually!
π For Loop: Doing a Task Multiple Times
Example: Imagine you're a teacher and you need to write "Good Job!" on the notebooks of 3 students: Alice, Bob, and Charlie. Instead of doing it manually, a for loop can do it for you!
π» In Shell Scripting:
for student in Alice Bob Charlie; do
echo "Good Job, $student!"
done
β What happens?
The script will print:
Good Job, Alice! Good Job, Bob! Good Job, Charlie!
π While Loop: Keep Doing a Task Until a Condition is Met
Example: You are filling bottles at a juice stall. You keep filling until you reach 5 bottles.
π» In Shell Scripting:
count=1
while [ $count -le 5 ]; do
echo "Filling bottle #$count"
((count++))
done
β What happens?
It will print:
Filling bottle #1 Filling bottle #2 Filling bottle #3 Filling bottle #4 Filling bottle #5
π Until Loop: Waiting for Something to Happen
Example: Youβre waiting for your bus to arrive. Every minute, you check if the bus is there. Once it arrives, you stop waiting.
π» In Shell Scripting:
bus_arrived="no"
until [ $bus_arrived == "yes" ]; do
echo "Waiting for the bus..."
sleep 2
done
echo "The bus has arrived!"
β What happens?
- The script keeps printing
"Waiting for the bus..."
until you tell it the bus has arrived.
π€ User Management: Handling Multiple Users in Linux
In Linux, users are like team members in a company. You can add new members, remove old ones, or switch roles easily.
β Creating a New User
Example: Your company just hired a new employee, and you need to create an account for them.
π» In Shell Scripting:
sudo useradd -m newemployee
sudo passwd newemployee
β What happens?
A new user account named "newemployee" is created.
A password is set for security.
β Removing a User
Example: An employee leaves the company, so you remove their access.
π» In Shell Scripting:
sudo userdel -r oldemployee
β What happens?
- The user "oldemployee" is deleted, along with their files.
π Switching Between Users
Example: Just like switching between Netflix profiles, you can switch between users in Linux.
π» In Shell Scripting:
su - newemployee
β What happens?
- You log in as "newemployee" and see their personal files.
π File Automation: Let Linux Do the Work for You!
Have you ever had to rename, move, or delete multiple files manually? Letβs see how Linux can do it automatically!
ποΈ Renaming Multiple Files at Once
Example: You have 100 pictures named like pic1.jpg
, pic2.jpg
, pic3.jpg
β¦ and you want to rename them all to vacation1.jpg
, vacation2.jpg
, etc.
π» In Shell Scripting:
for file in pic*.jpg; do
mv "$file" "vacation_$file"
done
β What happens?
pic1.jpg
βvacation_pic1.jpg
pic2.jpg
βvacation_pic2.jpg
pic3.jpg
βvacation_pic3.jpg
ποΈ Deleting Old Files Automatically
Example: Your phone deletes WhatsApp messages older than 30 days. Similarly, you can make Linux delete old files to save space!
π» In Shell Scripting:
find /home/user/documents -type f -mtime +30 -exec rm {} \;
β What happens?
- This deletes all files older than 30 days inside
/home/user/documents
.
β° Scheduling Automatic Backups (Cron Jobs)
Example: Your Google Drive automatically backs up your photos every night at 2 AM. You can do the same with Linux!
π» In Shell Scripting:
crontab -e
0 2 * * * /home/user/backup.sh
β What happens?
- Every night at 2 AM, your script
backup.sh
runs and saves important files.
π― Final Thoughts: Why Should You Learn Shell Scripting?
Imagine being able to:
β
Automatically rename, move, or delete files
β
Manage users in a shared computer system
β
Schedule daily backups or cleanups
β
Run repetitive tasks without lifting a finger
Shell scripting is like hiring a personal assistant for your computerβit does the boring stuff while you focus on important work! π
π‘ Challenge for You:
Try writing a script that renames all .txt
files in a folder and moves them to an "Archived" folder!
π Next up: Day 6 β Understanding Linux Networking! π Stay tuned!