Skip to content

Instantly share code, notes, and snippets.

@himynameisdave
Created June 25, 2020 22:16
Show Gist options
  • Save himynameisdave/5c02fcac75c18dd3ebc1d58cb67825a8 to your computer and use it in GitHub Desktop.
Save himynameisdave/5c02fcac75c18dd3ebc1d58cb67825a8 to your computer and use it in GitHub Desktop.
Bash script to scrape Google for the current USD-to-CAD exchange rate
#!/bin/bash
# Variables (feel free to edit these)
FROM_CURRENCY='usd'
TO_CURRENCY='cad'
# Constants
USER_AGENT="User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36"
SEARCH_URL="https://www.google.com/search?hl=en&q=$FROM_CURRENCY%20to%20$TO_CURRENCY"
# Command
OUTPUT=$(curl -s -H "$USER_AGENT" "$SEARCH_URL" | grep -Eo 'data-exchange-rate="[^\"]+"' | grep -Eo '"([^"\\]|\\.)*"')
echo $OUTPUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment