Trouble with conditional execution in a shell script for local development setup
I'm having trouble with I've been struggling with this for a few days now and could really use some help. While developing a client application, I need to set up a local environment that involves conditionally executing certain commands based on the presence of specific configuration files. The goal is to streamline the development process by ensuring that the script only runs tasks relevant to the environment being utilized. I've tried using simple `if` statements to check for the existence of these files, but I'm not sure if I'm following the best practices. Hereโs what Iโve put together so far: ```bash #!/bin/bash if [ -f "config.env" ]; then echo "Loading environment variables from config.env" set -a source config.env set +a else echo "config.env not found, skipping..." fi if [ -d "src" ]; then echo "Starting development server..." npm start else echo "Source directory not found, please check your setup." fi ``` This script checks for `config.env` and the `src` directory before proceeding. However, I noticed that when `config.env` doesn't exist, the script just skips loading environment variables. I want it to read from a fallback configuration if present. Iโve considered adding another layer of conditionals, but I want to avoid creating overly complex scripts. Hereโs another approach I thought of: ```bash if [ -f "config.env" ]; then source config.env elif [ -f "config.default.env" ]; then echo "Using default config..." source config.default.env else echo "No valid config file found!" exit 1 fi ``` The challenge I'm facing now is ensuring that the fallback works seamlessly. Are there better ways to handle multiple configuration files in a shell script? What should I keep in mind regarding performance and maintainability? Would appreciate any insights or alternative methods to streamline this process effectively. My development environment is Windows. For context: I'm using Bash on Linux. Cheers for any assistance!