BASH
Interactive Script with User Menu and Actions
Create an interactive Bash script with a dynamic menu, allowing users to select and execute various administrative or development tasks through a `case` statement, streamlining workflows.
#!/bin/bash
function display_menu {
echo "-----------------------------------------"
echo " Web Developer Utility Menu"
echo "-----------------------------------------"
echo "1. Deploy Application"
echo "2. Clear Cache"
echo "3. Restart Web Server (e.g., Nginx/Apache)"
echo "4. Check Disk Usage"
echo "0. Exit"
echo "-----------------------------------------"
}
function deploy_app {
echo "Deploying application..."
# Add your deployment commands here
# e.g., git pull origin main && composer install && npm install && npm run build
sleep 2
echo "Application deployed."
}
function clear_cache {
echo "Clearing application cache..."
# Add your cache clearing commands here
# e.g., php artisan cache:clear && php artisan view:clear
sleep 1
echo "Cache cleared."
}
function restart_web_server {
echo "Restarting web server..."
# Add your web server restart command here
# e.g., sudo systemctl restart nginx
sleep 2
echo "Web server restarted."
}
function check_disk_usage {
echo "Checking disk usage..."
df -h / # Check root partition disk usage
}
while true
do
display_menu
read -p "Enter your choice: " choice
case $choice in
1) deploy_app ;;
2) clear_cache ;;
3) restart_web_server ;;
4) check_disk_usage ;;
0) echo "Exiting..."; exit 0 ;;
*) echo "Invalid option. Please try again." ;;
esac
echo # Add a newline for better readability
sleep 1 # Pause briefly
done
How it works: This script provides an interactive menu-driven interface for common web developer tasks. It uses a `while` loop to repeatedly display options and a `case` statement to execute functions based on user input. This structure is highly useful for creating simple command-line tools for local development or server administration, streamlining repetitive actions into an easy-to-use menu.