-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurr.sh
More file actions
executable file
·61 lines (48 loc) · 1.57 KB
/
curr.sh
File metadata and controls
executable file
·61 lines (48 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#! /bin/sh
show_help() {
script_name=$(basename "$0")
echo "Usage: $script_name [amount] [from_currency] [to_currency]"
echo "Example: $script_name 20 usd inr"
exit 1
}
check_internet() {
# Check for internet connectivity by pinging a reliable server
if ! ping -c 1 google.com > /dev/null 2>&1; then
echo "No internet connection."
exit 1
fi
}
get_exchange_rate() {
from_currency="$1"
to_currency="$2"
check_internet
response=$(curl -s -w "%{http_code}" -o /tmp/exchange_rate_response.json "https://api.exchangerate-api.com/v4/latest/$from_currency")
if [ "$response" -ne 200 ]; then
echo "Failed to fetch exchange rate. API might be down or invalid currency code."
exit 1
fi
rate=$(jq -r ".rates.$to_currency" /tmp/exchange_rate_response.json)
echo "$rate"
}
convert_currency() {
amount="$1"
from_currency="$2"
to_currency="$3"
rate=$(get_exchange_rate "$from_currency" "$to_currency")
converted_amount=$(echo "$amount * $rate" | bc -l)
echo "$converted_amount"
}
# Main script
if [ "$#" -ne 3 ]; then
show_help
fi
amount="$1"
from_currency=$(echo "$2" | tr '[:lower:]' '[:upper:]')
to_currency=$(echo "$3" | tr '[:lower:]' '[:upper:]')
rate=$(get_exchange_rate "$from_currency" "$to_currency")
if [ "$rate" = "null" ]; then
echo "Invalid currency code(s)."
exit 1
fi
converted_amount=$(convert_currency "$amount" "$from_currency" "$to_currency")
echo "$amount $from_currency is equivalent to $converted_amount $to_currency at an exchange rate of $rate"