π Part 2: Making Scripts Interactive
- Using the
readcommand 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!"
π¬ Output:
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..."
π¬ Output:
Enter your environment (dev/stage/prod): devDeploying 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)"
π¬ Output:
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"
π¬ Output:
Enter server name within 5 seconds: middlewarebox01Server 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..."
π¬ Output:
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"
π¬ Output:
Enter username and group: devops adminsCreating 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
π¬ Output:
Do you want to restart Tomcat? (yes/no): yesStopping 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
π¬ Output:
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
π¬ Output:
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
π¬ Output:
Enter the log file path: /var/log/nginx/error.logAnalyzing 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!"
π¬ Output:
Enter Git branch to deploy: mainEnter 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
π¬ Output:
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.
No comments:
Post a Comment