Skip to content

Instantly share code, notes, and snippets.

@samukasmk
Last active November 28, 2024 20:56
Show Gist options
  • Save samukasmk/2316299ac56efac6759cae8d8d8972dd to your computer and use it in GitHub Desktop.
Save samukasmk/2316299ac56efac6759cae8d8d8972dd to your computer and use it in GitHub Desktop.
Sample of bashscript receiving and requiring command line argument
#!/bin/bash
# Variables to store argument values
ARG1=""
ARG2=""
# Function to display the error message and exit the script
function usage_error {
echo "Usage: $0 --argument_1=\"<text>\" --argument_2=\"<text>\""
echo "Error: $1"
exit 1
}
# Process the arguments
while [[ $# -gt 0 ]]; do
case "$1" in
--argument_1=*)
ARG1="${1#*=}"
shift
;;
--argument_2=*)
ARG2="${1#*=}"
shift
;;
*)
usage_error "Unknown argument: $1"
;;
esac
done
# Check if required arguments were provided
if [[ -z "$ARG1" ]]; then
usage_error "--argument_1 is required"
fi
if [[ -z "$ARG2" ]]; then
usage_error "--argument_2 is required"
fi
# Output the received arguments
echo "argument_1: $ARG1"
echo "argument_2: $ARG2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment