This commit is contained in:
Sebastian
2024-03-18 08:25:28 +01:00
parent 0dbf236167
commit d67a169626
2 changed files with 31 additions and 16 deletions

View File

@@ -1,4 +1,8 @@
{ {
"8453": ["https://base.drpc.org"], "base": {
"1": ["https://eth.drpc.org"] "id": 8453,
"urls": ["https://base.drpc.org"]}
"eth": {
"id": 1,
"urls": ["https://eth.drpc.org"]}
} }

View File

@@ -1,16 +1,14 @@
#!/bin/bash #!/bin/bash
# Check if the script is provided with the correct number of arguments # Check if the script is provided with the correct number of arguments
if [ $# -lt 1 ] || [ $# -gt 2 ]; then if [ $# -ne 2 ]; then
echo "Usage: $0 <key> [<index>]" echo "Usage: $0 <id> <index>"
exit 1 exit 1
fi fi
# Key provided as the first argument # ID and index provided as arguments
key="$1" id="$1"
index="$2"
# Set index to 0 if not provided as the second argument
index="${2:-0}"
# Check if the JSON file exists # Check if the JSON file exists
json_file="reference-rpc-endpoint.json" json_file="reference-rpc-endpoint.json"
@@ -19,14 +17,27 @@ if [ ! -f "$json_file" ]; then
exit 1 exit 1
fi fi
# Use jq to extract the element of the array corresponding to the key and index # Use jq to find the object with the provided id
element=$(jq -r ".$key[$index]" "$json_file") object=$(jq --arg id "$id" '.[] | select(.id == ($id | tonumber))' "$json_file")
# Check if the key exists in the JSON file # Check if the object exists
if [ "$element" = "null" ]; then if [ -z "$object" ]; then
echo "Error: Key '$key' not found in the JSON file or index '$index' out of range." echo "Error: Object with ID '$id' not found."
exit 1 exit 1
fi fi
# Print the element # Extract the URLs array from the object
echo "$element" urls=$(echo "$object" | jq -r '.urls')
# Check if the index is out of range
num_urls=$(echo "$urls" | jq -r 'length')
if [ "$index" -ge "$num_urls" ]; then
echo "Error: Index '$index' is out of range for ID '$id'."
exit 1
fi
# Extract the URL at the specified index
url=$(echo "$urls" | jq -r ".[$index]")
# Print the URL
echo "URL at index '$index' for ID '$id': $url"