Untitled
unknown
plain_text
2 years ago
791 B
2
Indexable
#!/bin/bash # Check if a file path is provided as an argument if [ $# -ne 1 ]; then echo "Please provide the JSON file path as an argument." exit 1 fi # Check if the file exists if [ ! -f "$1" ]; then echo "The file does not exist." exit 1 fi # Read the JSON file json=$(cat "$1") # Specify the key to retrieve its value json_key="your_key_name" # Retrieve the value of the specified key json_value=$(echo "$json" | awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/\042'$json_key'\042/){print $(i+1)}}}' | tr -d '"' | sed -e 's/^[[:space:]]*//') # Check if the key exists in the JSON file if [ -z "$json_value" ]; then echo "The key '$json_key' does not exist in the JSON file." exit 1 fi # Output the value of the key echo "The value of '$json_key' is: $json_value"
Editor is loading...