Skip to content

Shell Example Code

MMDRZA edited this page Oct 18, 2024 · 1 revision
#!/bin/bash

# URL of the JSON data
raw="https://raw.githubusercontent.com/Crypto-Static/Rate/main/rateStatic.json"

# Fetch JSON data using curl
response=$(curl -s "$raw")

# Check if the request was successful
if [ $? -eq 0 ]; then
  # Parse JSON and extract the required fields using jq
  echo "$response" | jq -r '.[] | "\nSymbol: \(.symbol)\nLAST: \(.lastPrice)\nHIGH: \(.highPrice24h)\nLOW: \(.lowPrice24h)\nCHANGE: \(.changeRate)\nUPDATED: \(.lastUpdated)"'
else
  echo "Failed to fetch data. HTTP status: $?"
fi

Explanation:

  • curl: Used to make the HTTP request and fetch the JSON data.
  • jq: A lightweight command-line JSON processor that allows parsing and extracting specific fields from the JSON data.
  • if [ $? -eq 0 ]; then: This checks whether the curl command was successful (exit status 0).
  • jq -r: This flag is used to output the extracted fields as raw text, avoiding double quotes around strings.

How to Run:

  1. Install jq if it's not already installed:
sudo apt-get install jq  # Ubuntu/Debian
brew install jq          # macOS (Homebrew)

Run

chmod +x crypto_fetch.sh
./crypto_fetch.sh
Clone this wiki locally