Skip to content

Instantly share code, notes, and snippets.

@borestad
Created September 20, 2023 08:32
Show Gist options
  • Save borestad/3d8b68b02efc625e695b08e240232ac2 to your computer and use it in GitHub Desktop.
Save borestad/3d8b68b02efc625e695b08e240232ac2 to your computer and use it in GitHub Desktop.
This code snippet processes command line arguments and assigns values to variables based on the provided options. It supports options like -o (for output), -ttl (for time to live), and their corresponding long forms (--output, --ttl). It also handles

bash argument parsing

Preview:
URL=$1
shift 1

while [ "$#" -gt 0 ]; do
  case "$1" in
    -o) OUTPUT="$2"; shift 2;;
    -ttl) TTL="$2"; shift 2;;

    --output=*) OUTPUT="${1#*=}"; shift 1;;
    --ttl=*) TTL="${1#*=}"; shift 1;;
    --output|--ttl) echo "$1 requires an argument" >&2; exit 1;;

    -*) echo "unknown option: $1" >&2; exit 1;;
    # *) echo "$1"; shift 1;;
    *) shift 1;;
  esac
done
Associated Context
Type Code Snippet ( .dart )
Associated Tags Option flag (-ttl) Duration parameter Script execution Option flag (--output) Command line arguments System management URL parsing Command-line interface Unknown option handling Output variable bash Shift command Error handling Option flag (-o) linux Option flag (--ttl) URL command Output file unix Argument validation Shell scripting TTL configuration
💡 Smart Description This code checks if a given URL is in the format "URL=$1" and sets TTL to $2, then prints an error message. If it does not exist, it exits with no output or invalid argument.
This code snippet processes command line arguments and assigns values to variables based on the provided options. It supports options like -o (for output), -ttl (for time to live), and their corresponding long forms (--output, --ttl). It also handles
🔎 Suggested Searches How to use case-case in command line?
How to set the TTL of a URL using shift 1 ?
What is the syntax for setting up an argument programming language?
how to add optional options and ttl ?
Bash script for parsing command line arguments with options and arguments
How to handle command line options and arguments in Bash
Bash script for parsing command line flags and values
Code snippet for processing command line arguments in Bash
Bash script for handling command line options and parameters
Related Links https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html
https://www.shellscript.sh/variables1.html
https://www.hackerearth.com/practice/algorithms/string-algorithm/basics-of-string-manipulation/tutorial/
https://www.geeksforgeeks.org/data-structures/linked-list/
https://www.geeksforgeeks.org/
https://www.tutorialspoint.com/unix_commands/shift.htm
https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
https://www.shellscript.sh/loops.html
https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html
https://www.geeksforgeeks.org/what-is-linked-list/
https://www.shellscript.sh/test.html
Related People No Related People
Sensitive Information No Sensitive Information Detected
Shareable Link https://user-e6450573-071d-4aab-aa99-af4c81da7d7d-jtn3vuzmna-ue.a.run.app/?p=31d64988af
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment