#!/bin/bash # Configuration / Default values OUTPUT_FILE="output.txt" VERBOSE=false ACTION="default" COUNT=1 # Function to display script usage usage() { echo "Usage: $0 [-f ] [-a ] [-c ] [-v] [-h]" echo " -f Specify an output file (default: $OUTPUT_FILE)" echo " -a Specify an action to perform (e.g., 'deploy', 'test', 'build')" echo " -c Specify a numerical count (default: $COUNT)" echo " -v Enable verbose mode" echo " -h Display this help message" exit 0 } # --- Main Script Logic --- # Use getopts for parsing short options # ':' after an option means it requires an argument # 'h' for help, 'v' for verbose # 'f:' for file, 'a:' for action, 'c:' for count while getopts "hvf:a:c:" opt; do case "${opt}" in h) usage ;; v) VERBOSE=true echo "Verbose mode enabled." ;; f) OUTPUT_FILE="${OPTARG}" ;; a) ACTION="${OPTARG}" ;; c) # Basic validation for count to be a number if [[ "$OPTARG" =~ ^[0-9]+$ ]]; then COUNT="${OPTARG}" else echo "Error: Count must be a number." >&2 usage fi ;; :) echo "Error: Option -${OPTARG} requires an argument." >&2 usage ;; ?) echo "Error: Invalid option -${OPTARG}." >&2 usage ;; esac done # Shift off the options and their arguments, so that "$@" refers to the remaining non-option arguments. shift $((OPTIND -1)) echo "--- Script Configuration ---" echo "Output File: $OUTPUT_FILE" echo "Action: $ACTION" echo "Count: $COUNT" echo "Verbose: $VERBOSE" if [ -n "$@" ]; then echo "Remaining arguments (non-options): $@" fi echo "--------------------------" # Example logic based on parsed arguments case "$ACTION" in deploy) echo "Initiating deployment for $COUNT times to $OUTPUT_FILE..." # Add deploy specific logic ;; test) echo "Running tests $COUNT times with output to $OUTPUT_FILE..." # Add test specific logic ;; build) echo "Building project $COUNT times..." # Add build specific logic ;; *) echo "Performing default action." for i in $(seq 1 $COUNT); do echo "Hello from default action ($i/$COUNT)!" >> "$OUTPUT_FILE" done echo "Default output written to $OUTPUT_FILE" ;; esac echo "Script finished."