Skip to content

Instantly share code, notes, and snippets.

@eugrus
Created August 23, 2023 22:58
Show Gist options
  • Save eugrus/1406fcc8ba86482e6329e90e2304dd98 to your computer and use it in GitHub Desktop.
Save eugrus/1406fcc8ba86482e6329e90e2304dd98 to your computer and use it in GitHub Desktop.
Bash/CGI parsing of URI args separated by whitespaces
#!/bin/bash
echo "Content-Type: text/plain; charset=utf-8" && echo
IFS='?' read -ra url_parts <<< "$REQUEST_URI"
# Extract the query string from the URL
query_string="${url_parts[1]}"
# URL-decode the query string
decoded_query_string=$(printf '%b' "${query_string//%/\\x}")
# Split the decoded query string into an array using space as delimiter
IFS=' ' read -ra query_params <<< "$decoded_query_string"
# Debug:
#echo "First element: ${query_params[0]}"
#echo "Second element: ${query_params[1]}"
#echo "Third element: ${query_params[2]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment