Skip to content

Commit

Permalink
Merge pull request #283 from iAGorynT/iAGorynT-Code-Snippet-Changes
Browse files Browse the repository at this point in the history
Added JSON Utility Test Script
  • Loading branch information
iAGorynT authored Apr 3, 2024
2 parents ca72b01 + 970c478 commit 10a3d89
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 5 deletions.
19 changes: 14 additions & 5 deletions ShellScripts/zTests/Mmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ function zjsonconfig {
~/bin/zTests/zJsonConfig.sh
}

function zjqjsonutil {
clear
~/bin/zTests/zJQJsonUtil.sh
}

function zpassfromshortcuts {
clear
~/bin/zTests/zPassFromShortcuts.sh
Expand All @@ -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
Expand All @@ -71,12 +77,15 @@ do
zjsonconfig;;

4)
zpassfromshortcuts;;
zjqjsonutil;;

5)
zuserinput;;
zpassfromshortcuts;;

6)
zuserinput;;

7)
zdaysofmonth;;

# Return / Enter Key Pressed
Expand Down
66 changes: 66 additions & 0 deletions ShellScripts/zTests/zJQJsonUtil.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 10a3d89

Please sign in to comment.