Arrays and arguments give your scripts flexibility — perfect for automating Middleware & DevOps tasks like restarting Tomcat across servers, cleaning WebSphere logs, or passing dynamic inputs to Jenkins jobs.
π Table of Contents
1️⃣ Arrays in Shell
π― Example 1 – Basic Array
#!/bin/bash
servers=("dev" "stage" "prod")
echo "First server: ${servers[0]}"
echo "All servers: ${servers[@]}"
π¬ Output:
First server: devAll servers: dev stage prod
π Example 2 – Loop through Array
#!/bin/bash
apps=("Tomcat" "WebSphere" "JBoss")
for app in "${apps[@]}"; do
echo "Managing $app service..."
done
π¬ Output:
Managing Tomcat service...Managing WebSphere service...
Managing JBoss service...
π§° Example 3 – Restart Tomcat on Multiple Servers
#!/bin/bash
servers=("app1" "app2" "app3")
for s in "${servers[@]}"; do
echo "Restarting Tomcat on $s..."
ssh $s "systemctl restart tomcat" || echo "⚠️ Restart failed on $s"
done
echo "✅ Restart loop completed."
π¬ Output:
Restarting Tomcat on app1...Restarting Tomcat on app2...
⚠️ Restart failed on app3
✅ Restart loop completed.
π§Ή Example 4 – Cleanup WebSphere Logs
#!/bin/bash
logs=(SystemOut.log SystemErr.log activity.log)
for file in "${logs[@]}"; do
echo "Deleting $file..."
rm -f /opt/IBM/WebSphere/AppServer/logs/$file || echo "⚠️ Could not remove $file"
done
echo "π§Ή Old WebSphere logs removed."
π¬ Output:
Deleting SystemOut.log...Deleting SystemErr.log...
Deleting activity.log...
π§Ή Old WebSphere logs removed.
2️⃣ Command-line Arguments
π¦ Example 5 – Access Arguments
#!/bin/bash
echo "Environment: $1"
echo "Version: $2"
π¬ Output:
$ ./deploy.sh prod v2.0Environment: prod
Version: v2.0
π Example 6 – Trigger Jenkins Job
#!/bin/bash
job_name=$1
if [ -z "$job_name" ]; then
echo "❌ Usage: $0 "
exit 1
fi
curl -s -X POST "http://localhost:8080/job/$job_name/build?token=deploy123" || echo "⚠️ Failed to trigger job."
π¬ Output:
$ ./jenkins_trigger.sh BuildApp✅ Triggered Jenkins job BuildApp
π§ Example 7 – Argument Validation
#!/bin/bash
if [ $# -lt 2 ]; then
echo "Usage: $0 "
exit 1
fi
echo "Deploying $1 version $2..."
π¬ Output:
$ ./deploy.sh myapp v1.5Deploying myapp version v1.5...
3️⃣ Combining Arrays & Arguments
π Example 8 – Check NGINX on Multiple Hosts
#!/bin/bash
hosts=("$@")
for h in "${hosts[@]}"; do
echo "Checking NGINX on $h..."
ssh $h "systemctl is-active nginx" >/dev/null 2>&1 && echo "✅ $h OK" || echo "❌ $h DOWN"
done
π¬ Output:
$ ./check_nginx.sh host1 host2 host3✅ host1 OK
✅ host2 OK
❌ host3 DOWN
πΎ Example 9 – Query Multiple Databases
#!/bin/bash
dbs=("salesdb" "hrdb" "logdb")
for db in "${dbs[@]}"; do
echo "Checking $db connection..."
mysql -h dbserver -u admin -psecret -e "use $db;" >/dev/null 2>&1 && echo "✅ $db reachable" || echo "⚠️ $db not reachable"
done
π¬ Output:
Checking salesdb connection... ✅ salesdb reachableChecking hrdb connection... ⚠️ hrdb not reachable
Checking logdb connection... ✅ logdb reachable
π§© Example 10 – CI/CD Pipeline Trigger
#!/bin/bash
branches=("main" "dev" "hotfix")
for b in "${branches[@]}"; do
echo "Triggering pipeline for $b..."
curl -s -X POST "https://ci.middlewarebox.com/job/build-$b/build" || echo "⚠️ Failed for $b"
done
π¬ Output:
Triggering pipeline for main... ✅ DoneTriggering pipeline for dev... ✅ Done
Triggering pipeline for hotfix... ⚠️ Failed for hotfix
4️⃣ Error Handling & Debugging
- Use
set -xto trace executed commands. - Check argument count with
$#. - Use
"$@"to safely pass all args to loops. - Handle SSH/curl/mysql failures with
||and echo messages. - Log outputs →
script.sh >> /var/log/script.log 2>&1
π Summary
- Arrays – store lists of servers, apps, or files.
- Arguments – pass values into scripts for dynamic execution.
- Combine both for multi-server automation.
- Always include error handling to avoid silent failures.
No comments:
Post a Comment