Created
February 15, 2025 17:30
-
-
Save surfaceflinger/0966e5c6d0ace4a357c92a65d592e9f9 to your computer and use it in GitHub Desktop.
simple crypto/currencies value checker/summer in bash because everything else sucks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ledger NOT 500 MANUAL | |
Revolut CHF 400 | |
Trezor BTC 0.009 comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NOT 0.002946 USD |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env nix-shell | |
#! nix-shell -i bash -p rates bc | |
# Force decimal point as separator regardless of locale | |
export LC_NUMERIC="C" | |
# Function to get manual rate for a currency | |
get_manual_rate() { | |
local currency="$1" | |
local target="$2" | |
local amount="$3" | |
# Read rates.txt and find the matching currency | |
while IFS= read -r line; do | |
# Split line into currency, value, and target currency | |
read -r curr value tcurr <<< "$line" | |
if [ "$curr" = "$currency" ]; then | |
if [ "$tcurr" = "$target" ]; then | |
# Calculate: amount * value | |
echo "$amount * $value" | bc | |
return 0 | |
else | |
# If we found the currency but with different target currency, | |
# first multiply by value to get amount in tcurr, then convert to target | |
local intermediate | |
intermediate=$(echo "$amount * $value" | bc) | |
if [ "$value" = "0" ]; then | |
echo "0" | |
return 0 | |
fi | |
result=$(rates "$intermediate" "$tcurr" "$target" | cut -d= -f2 | tr -d ' ' | tr ',' '.' | grep -o '^[0-9.]*') | |
echo "$result" | |
return 0 | |
fi | |
fi | |
done < "rates.txt" | |
# If we get here, currency not found in rates.txt | |
echo "0" | |
return 1 | |
} | |
# Function to convert amount from one currency to another | |
convert() { | |
local amount="$1" | |
local from_curr="$2" | |
local to_curr="$3" | |
local is_manual="$4" | |
# If source and target currencies are the same, return the original amount | |
if [ "$from_curr" = "$to_curr" ]; then | |
echo "$amount" | |
return | |
fi | |
# Handle manual rate if specified | |
if [ "$is_manual" = "true" ]; then | |
local manual_rate | |
manual_rate=$(get_manual_rate "$from_curr" "$to_curr" "$amount") | |
if [ -n "$manual_rate" ]; then | |
echo "$manual_rate" | |
return | |
fi | |
fi | |
# Only try rates command if manual rate wasn't found | |
result=$(rates "$amount" "$from_curr" "$to_curr" | cut -d= -f2 | tr -d ' ' | tr ',' '.' | grep -o '^[0-9.]*') | |
echo "${result:-0}" | |
} | |
# Initialize total sums | |
total_usd=0 | |
total_chf=0 | |
total_pln=0 | |
# Initialize associative arrays for account sums | |
declare -A account_usd | |
declare -A account_chf | |
declare -A account_pln | |
echo "Individual entries converted to USD/CHF/PLN:" | |
echo "----------------------------------------" | |
# Process each line from accounts.txt | |
while IFS= read -r line; do | |
# Skip empty lines | |
[ -z "$line" ] && continue | |
# Split line into fields | |
read -r account currency amount remainder <<< "$line" | |
# Skip entries with GECKO or UNISWAP in remainder | |
if [[ "$remainder" == *"GECKO"* ]] || [[ "$remainder" == *"UNISWAP"* ]]; then | |
continue | |
fi | |
# Check if this is a manual rate entry | |
is_manual="false" | |
if [[ "$remainder" == *"MANUAL"* ]]; then | |
is_manual="true" | |
fi | |
# Convert to each currency and ensure decimal point | |
amount_usd=$(convert "$amount" "$currency" "USD" "$is_manual" | tr ',' '.') | |
amount_chf=$(convert "$amount" "$currency" "CHF" "$is_manual" | tr ',' '.') | |
amount_pln=$(convert "$amount" "$currency" "PLN" "$is_manual" | tr ',' '.') | |
# Print individual entry conversions | |
printf "%-40s | USD: %'10.2f | CHF: %'10.2f | PLN: %'10.2f\n" \ | |
"$line" "${amount_usd:-0}" "${amount_chf:-0}" "${amount_pln:-0}" | |
# Add to account totals | |
account_usd[$account]=$(echo "${account_usd[$account]:-0} + ${amount_usd:-0}" | bc) | |
account_chf[$account]=$(echo "${account_chf[$account]:-0} + ${amount_chf:-0}" | bc) | |
account_pln[$account]=$(echo "${account_pln[$account]:-0} + ${amount_pln:-0}" | bc) | |
# Add to grand totals | |
total_usd=$(echo "$total_usd + ${amount_usd:-0}" | bc) | |
total_chf=$(echo "$total_chf + ${amount_chf:-0}" | bc) | |
total_pln=$(echo "$total_pln + ${amount_pln:-0}" | bc) | |
done < "accounts.txt" | |
echo | |
echo "Account summaries:" | |
echo "----------------------------------------" | |
# Sort accounts alphabetically and print their sums | |
for account in $(echo "${!account_usd[@]}" | tr ' ' '\n' | sort); do | |
printf "%-40s | USD: %'10.2f | CHF: %'10.2f | PLN: %'10.2f\n" \ | |
"$account" "${account_usd[$account]}" "${account_chf[$account]}" "${account_pln[$account]}" | |
done | |
echo | |
echo "Total sums:" | |
echo "----------------------------------------" | |
printf "TOTAL | USD: %'10.2f | CHF: %'10.2f | PLN: %'10.2f\n" \ | |
"$total_usd" "$total_chf" "$total_pln" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment