🚀 Part 2: Making Scripts Interactive
- Using the
read command for user input
- Creating scripts that listen to user responses
- Essential for real-world scripting and automation
💡 What is the read Command?
The read command accepts user input. Think of it as your script saying —
"Hey human, tell me something so I can automate it for you." 😎
read variable_name
Whatever the user types will be stored inside $variable_name.
🎯 Example 1: Basic User Input
#!/bin/bash
echo "What is your name?"
read name
echo "Hello $name, welcome to MiddlewareBox!"
What is your name?
John
Hello John, welcome to MiddlewareBox!
⚙️ Example 2: Read with Prompt in Same Line
#!/bin/bash
read -p "Enter your environment (dev/stage/prod): " env
echo "Deploying application to $env environment..."
Enter your environment (dev/stage/prod): dev
Deploying application to dev environment...
🔐 Example 3: Hidden Input (Password)
#!/bin/bash
read -sp "Enter your Jenkins admin password: " password
echo
echo "Password received! (Not displayed for security)"
Enter your Jenkins admin password: ********
Password received! (Not displayed for security)
⏰ Example 4: Set Timeout for Input
#!/bin/bash
read -t 5 -p "Enter server name within 5 seconds: " server
echo "Server chosen: $server"
Enter server name within 5 seconds: middlewarebox01
Server chosen: middlewarebox01
📦 Example 5: Default Value if User Skips Input
#!/bin/bash
read -p "Enter build version (default: 1.0): " version
version=${version:-1.0}
echo "Deploying build version $version..."
Enter build version (default: 1.0):
Deploying build version 1.0...
💾 Example 6: Take Multiple Inputs Together
#!/bin/bash
read -p "Enter username and group: " user group
echo "Creating user: $user in group: $group"
Enter username and group: devops admins
Creating user: devops in group: admins
🧰 Example 7: Restart Tomcat with User Confirmation
#!/bin/bash
read -p "Do you want to restart Tomcat? (yes/no): " choice
if [[ "$choice" == "yes" ]]; then
echo "Stopping Tomcat..."
sh /opt/tomcat/bin/shutdown.sh
sleep 3
echo "Starting Tomcat..."
sh /opt/tomcat/bin/startup.sh
echo "✅ Tomcat restarted successfully!"
else
echo "❌ Operation cancelled."
fi
Do you want to restart Tomcat? (yes/no): yes
Stopping Tomcat...
Starting Tomcat...
✅ Tomcat restarted successfully!
🧠 Example 8: Jenkins Backup Trigger
#!/bin/bash
read -p "Do you want to back up Jenkins data now? (y/n): " answer
if [[ "$answer" == "y" ]]; then
backup_dir="/var/jenkins_home/backup"
mkdir -p $backup_dir
tar -czf $backup_dir/jenkins_backup_$(date +%F).tar.gz /var/jenkins_home
echo "✅ Jenkins backup created in $backup_dir"
else
echo "🕒 Backup skipped by user."
fi
Do you want to back up Jenkins data now? (y/n): y
✅ Jenkins backup created in /var/jenkins_home/backup
🌍 Example 9: Deployment Environment Selector
#!/bin/bash
read -p "Enter deployment environment (dev/test/prod): " env
case $env in
dev)
echo "🚀 Deploying to Development servers..."
;;
test)
echo "🧪 Deploying to Testing servers..."
;;
prod)
echo "⚠️ Deploying to Production environment..."
;;
*)
echo "❌ Invalid environment!"
;;
esac
Enter deployment environment (dev/test/prod): prod
⚠️ Deploying to Production environment...
📁 Example 10: Check and Read File Path Before Processing
#!/bin/bash
read -p "Enter the log file path: " logfile
if [[ -f "$logfile" ]]; then
echo "Analyzing log file..."
grep -i "error" $logfile | head -5
else
echo "❌ File not found: $logfile"
fi
Enter the log file path: /var/log/nginx/error.log
Analyzing log file...
[error] 1054#1054: *1 connect() failed (111: Connection refused)
[error] 1055#1055: *2 open() "/var/www/html/404.html" failed (2: No such file)
🧩 Example 11: Take User Input in a CI/CD Script
#!/bin/bash
read -p "Enter Git branch to deploy: " branch
read -p "Enter version tag: " version
echo "Cloning branch '$branch'..."
git clone -b $branch https://github.com/middlewarebox/myapp.git
echo "Deploying version $version..."
echo "✅ Deployment completed!"
Enter Git branch to deploy: main
Enter version tag: v2.0
Cloning branch 'main'...
✅ Deployment completed!
🧠 Example 12: User Confirmation for Rollback
#!/bin/bash
read -p "Rollback to previous version? (yes/no): " rollback
if [[ "$rollback" == "yes" ]]; then
echo "🔁 Rolling back..."
echo "✅ Rollback completed successfully!"
else
echo "👍 Staying on current version."
fi
Rollback to previous version? (yes/no): yes
🔁 Rolling back...
✅ Rollback completed successfully!
🏁 Conclusion
In DevOps and Middleware environments, interactive scripts save time and prevent mistakes.