Shell Scripting for Beginners – Taking Input & Automating Real Tasks
Once you’re comfortable writing basic shell scripts, the next step is making them interactive and dynamic.
In this blog, we’ll learn how to:
✅ Take user input
✅ Handle arguments and parameters
✅ Automate common daily tasks like creating folders, renaming files, and monitoring memory
✅ Write cleaner, reusable shell code
Let’s get started with a very common requirement: asking the user for input.
🗣️ Taking Input from User
Let’s make a script that asks the user’s name and greets them.
#!/bin/bash
echo "What is your name?"
read username
echo "Hello, $username! Welcome to shell scripting."
Run it:
chmod +x welcome.sh
./welcome.sh
💡 read is used to capture what the user types in.
📦 Task 1: Create a Folder with User-Defined Name
#!/bin/bash
echo "Enter the folder name to create:"
read foldername
mkdir "$foldername"
echo "Folder '$foldername' created successfully!"
👉 You can now create folders without typing mkdir manually every time.
🛠️ Task 2: Rename Multiple Files Automatically
Let’s rename all .txt files to .bak format with user confirmation.
#!/bin/bash
for file in *.txt
do
echo "Rename $file to ${file%.txt}.bak? (y/n)"
read answer
if [[ $answer == "y" ]]; then
mv "$file" "${file%.txt}.bak"
echo "Renamed $file"
fi
done
🧠 This introduces you to loops plus conditional statements with user choices.
💡 Task 3: CPU & Memory Usage Checker
Let’s create a quick monitoring tool using shell:
#!/bin/bash
echo "Checking system performance..."
echo "CPU Load:"
uptime
echo "Memory Usage:"
free -h
echo "Top 5 memory-hogging processes:"
ps aux --sort=-%mem | head -n 6
🧠 This is how system admins use shell scripts to keep servers healthy.
📂 Task 4: Bulk File Creation
Need 10 log files for testing? Automate it!
#!/bin/bash
echo "Enter number of files to create:"
read count
for ((i = 1; i <= count; i++))
do
touch "logfile_$i.log"
done
echo "$count log files created successfully."
🧾 Understanding Script Parameters
You can pass values directly when running your script:
#!/bin/bash
echo "Hello $1! You are learning $2."
Save as intro.sh, and run:
./intro.sh Alice "Shell Scripting"
Output:
Hello Alice! You are learning Shell Scripting.
🧠 $1, $2, etc. are positional parameters.
⚙️ Task 5: Backup with Arguments
#!/bin/bash
src=$1
dest=$2
if [ -d "$src" ]; then
tar -czf "$dest/backup-$(date +%F).tar.gz" "$src"
echo "Backup completed!"
else
echo "Source directory does not exist."
fi
Usage:
./backup.sh /home/user/docs /home/user/backups
🧼 BONUS: Create a Cleanup Script with Confirmation
#!/bin/bash
echo "This will delete all .log files in the current directory."
read -p "Are you sure? (y/n): " confirm
if [[ $confirm == "y" ]]; then
rm *.log
echo "All .log files deleted."
else
echo "Aborted!"
fi
Safety first! Scripts that delete files should always ask for confirmation.
🧠 Final Tips for Beginner Scripters
- Use
echooften to understand what your script is doing. - Keep testing small blocks before combining them.
- Learn how to debug using
bash -x script.sh. - Save your scripts in a
~/scriptsfolder and add it to your PATH. - Explore
select,case, and functions for better control.
🧳 Summary
Today, you learned how to:
✅ Accept user input
✅ Handle command-line arguments
✅ Create folders, rename files, and monitor memory
✅ Use loops, conditionals, and parameters in real-world tasks
Shell scripting is not just about commands — it’s about automation, efficiency, and saving time. As you keep writing more scripts, you’ll discover just how powerful your terminal can be.
Projects to explore next
Source-code projects from the KritiMyantra catalogue. Check each project's details before buying.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure & Mi...
Flask Blog CMS with Admin Dashboard — Python Source Code
A Flask-based blogging website with a built-in admin dashboard. Manage articles, images, categories, tags and featured p...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes preconfigu...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.