#!/bin/bash Location="Poznan" Dynamic=false Fahrenheit=false Api_key="99aab0a02cc2431b910194023170912" lb="e[94m"; lc="e[96m"; lw="e[97m"; ly="e[33m"; default="e[39m"; while getopts "dl:f" opt; do case $opt in d) Dynamic=true ;; l) Location="$OPTARG" ;; f) Fahrenheit=true ;; ?) exit 1 ;; :) exit 1 ;; esac done function download_data { local File="/tmp/$Location" local CurrentTime=$(date +%s) local FileCreationTime=0 if [ -e "$File" ] ; then FileCreationTime=$(stat -c "%Y" $File) fi if (( ( $CurrentTime - $FileCreationTime ) >= 300 )) ; then Data=$(curl -s "http://api.apixu.com/v1/current.json?key=$Api_key&q=$Location") if $(echo $Data | jq 'has("error")') ; then echo "Nie udało się znaleźć takiego miasta. Możesz je wybudować!:)" exit 1 fi echo "$Data" > $File else Data=$(cat $File) fi } function print_data { echo -e "$lbu27F0 $lw Miasto $lbu27F0 $lc $(echo $Data | jq -r '.location.name')" if [ "$Fahrenheit" == true ] ; then echo -e "$lbu2600 $lw Temperatura $lbu2600 $lc $(echo $Data | jq -r '.current.temp_f') F" else echo -e "$lbu2600 $lw Temperatura $lbu2600 $lc $(echo $Data | jq -r '.current.temp_c') C" fi echo -e "$lbu2B83 $lw Ciśnienie $lbu2B83 $lc $(echo $Data | jq -r '.current.pressure_mb')" echo -e "$lbu0f04 $lw Wiatr $lbu0f04 $lc $(echo $Data | jq -r '.current.wind_kph')" echo -e "$lbu2614 $lw Wilgotność $lbu2614 $lc $(echo $Data | jq -r '.current.humidity')" echo -e "$lbu2601 $lw Zachmurzenie $lbu2601 $lc $(echo $Data | jq -r '.current.cloud')" } if [ "$Dynamic" == true ] ; then while [ true ]; do echo -e "$ly===================================================" download_data print_data echo -e "$ly===================================================" sleep 300; done else download_data print_data exit 0 fi