dotfiles/bash/gcommands
Errol Sancaktar 66445d30e0 update
2025-04-02 09:31:39 -06:00

19 lines
560 B
Plaintext

check_envs(var) {
local command="$2"
if [[ -z "${!env_var}" ]]; then
read -rp "Enter value for $env_var: " "${env_var}"
export "$env_var=${!env_var}" #Export the variable to be accessible by child processes
fi
if [[ -n "${!env_var}" ]]; then
eval "$command"
else
echo "Environment variable $env_var is not set. Command not executed."
fi
}
# Example usage:
# run_with_env MY_API_KEY "curl -H \"Authorization: Bearer $MY_API_KEY\" https://api.example.com"
# run_with_env DATABASE_URL "psql $DATABASE_URL -c 'SELECT * FROM users;'"