diff --git a/ShellScripts/zTests/Mmt.sh b/ShellScripts/zTests/Mmt.sh index 64bc203..e68228e 100755 --- a/ShellScripts/zTests/Mmt.sh +++ b/ShellScripts/zTests/Mmt.sh @@ -20,6 +20,11 @@ function zjsonconfig { ~/bin/zTests/zJsonConfig.sh } +function zjqjsonutil { + clear + ~/bin/zTests/zJQJsonUtil.sh +} + function zpassfromshortcuts { clear ~/bin/zTests/zPassFromShortcuts.sh @@ -42,9 +47,10 @@ function menu { echo -e "\t1. zDialogBox" echo -e "\t2. zJQJsonConfig" echo -e "\t3. zJsonConfig" - echo -e "\t4. zPassFromShortcuts" - echo -e "\t5. zUserinput" - echo -e "\t6. zDaysOfMonth" + echo -e "\t4. zJQJsonUtil" + echo -e "\t5. zPassFromShortcuts" + echo -e "\t6. zUserinput" + echo -e "\t7. zDaysOfMonth" echo -e "\t0. Exit Menu\n\n" echo -en "\t\tEnter an Option: " read -k 1 option @@ -71,12 +77,15 @@ do zjsonconfig;; 4) - zpassfromshortcuts;; + zjqjsonutil;; 5) - zuserinput;; + zpassfromshortcuts;; 6) + zuserinput;; + + 7) zdaysofmonth;; # Return / Enter Key Pressed diff --git a/ShellScripts/zTests/zJQJsonUtil.sh b/ShellScripts/zTests/zJQJsonUtil.sh new file mode 100755 index 0000000..adcc736 --- /dev/null +++ b/ShellScripts/zTests/zJQJsonUtil.sh @@ -0,0 +1,66 @@ +#!/bin/zsh + +# JSON File Utility + +clear +echo "JSON File Utility..." +echo + +# Prompt for file name +read filename\?"Enter the JSON file name: " + +# Function to sanitize a filename, global variable, or directory path +sanitize_input() { +# Replace characters that could be interpreted as special characters +# Preserve global variables and directory paths + sanitized_input=$(echo "$1" | sed 's/[^A-Za-z0-9._/ ~-]//g') + echo "$sanitized_input" +} + +# Example usage: +input="$filename" +sanitized=$(sanitize_input "$input") +echo "Sanitized input: $sanitized" + +# Using "eval" command to take in filename and executes it as if it were typed directly +# on the command line. +# +# BE ADVISED! USING eval MAY POSE A SECURITY RISK! +# Keep that in mind if you opt for this solution. +# USE THIS CODE AT YOUR OWN PERIL!!! +# Suggest that you only use this solution if script execution is limited +# to TRUSTED INDIVIDUALS and filename is properly sanitized to avoid security vulnerabilities. +sanitizedfilename="$(eval echo $sanitized)" + +# Check if file exists +if [ -f "$sanitizedfilename" ]; then + # Check if file is JSON + if [[ "$sanitizedfilename" == *.json ]]; then + echo + echo "$sanitizedfilename exists and is a JSON file." + echo +# Pretty Print + echo "Pretty Print..." + jq -M '.' "$sanitizedfilename" + echo +# Alphabetic List + echo "Alphabetic List..." + jq -S '.' "$sanitizedfilename" + echo +# Reverse Alphabetic List + echo "Reverse Alphabetic List" + jq -S '.' "$sanitizedfilename" | jq 'to_entries | reverse | from_entries' + echo + else + echo + echo "Error: $sanitizedfilename exists but is not a JSON file." + echo + exit 1 + fi +else + echo + echo "Error: $sanitizedfilename does not exist." + echo + exit 1 +fi +