Kritim Yantra
May 04, 2025
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.
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.
#!/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.
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.
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.
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."
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.
#!/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
#!/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.
echo
often to understand what your script is doing.bash -x script.sh
.~/scripts
folder and add it to your PATH.select
, case
, and functions for better control.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.
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google