Real World Scripting
Last updated
sudo apt-get install jqbrew install jqcurl https://arrivelah2.busrouter.sg/?id=18331#!/bin/bash
# Replace this with your actual curl command that fetches the JSON response
response=$(curl -s "https://arrivelah2.busrouter.sg/?id=18331")
# Extract the 'time' field of the next bus using jq
next_bus_time=$(echo "$response" | jq -r '.services[0].next.time')
# Convert current time and the bus time to epoch for comparison
# TODO: Implement this (hint: figure out how to use date +%s)
current_time=
bus_arrival_time=
# Calculate time difference in seconds
time_diff=
# Convert time difference to minutes and seconds
minutes=$((time_diff / 60))
seconds=$((time_diff % 60))
# Display the result
if [ "$time_diff" -gt 0 ]; then
echo "Bus number 95 will arrive in $minutes minutes and $seconds seconds."
else
echo "Bus number 95 has already arrived or will arrive shortly."
fi#!/bin/bash
# Replace this with your actual curl command that fetches the JSON response
response=$(curl -s "https://arrivelah2.busrouter.sg/?id=18331")
# Extract the 'time' field of the next bus using jq
next_bus_time=$(echo "$response" | jq -r '.services[0].next.time')
# Convert current time and the bus time to epoch for comparison
current_time=$(date +%s)
bus_arrival_time=$(date -d "$next_bus_time" +%s)
# Calculate time difference in seconds
time_diff=$((bus_arrival_time - current_time))
# Convert time difference to minutes and seconds
minutes=$((time_diff / 60))
seconds=$((time_diff % 60))
# Display the result
if [ "$time_diff" -gt 0 ]; then
echo "Bus number 95 will arrive in $minutes minutes and $seconds seconds."
else
echo "Bus number 95 has already arrived or will arrive shortly."
fi
echo $PATHcp bus.sh /usr/local/bin/buscurl https://api.data.gov.sg/v1/environment/24-hour-weather-forecast | jq#!/bin/bash
# Replace these with your actual Telegram bot token and chat ID
TELEGRAM_BOT_TOKEN="YOUR_BOT_TOKEN"
CHAT_ID="YOUR_CHAT_ID"
TELEGRAM_API_URL="https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage"
# Replace this with your actual curl command that fetches the JSON weather data
response=$(curl -s "https://api.data.gov.sg/v1/environment/24-hour-weather-forecast
")
# Define an array of rain-related keywords
rain_keywords=("Rain" "Showers" "Thundery Showers" "Heavy Thundery Showers")
# Extract the general weather forecast using jq
general_forecast=$(echo "$response" | jq -r '.items[0].general.forecast')
# Initialize a flag to check if rain is found
rain_found=0
# Check for rain-related keywords in the general forecast
# TODO: Try implementing this! Your code should follow this general idea:
# for each keyword in rain_keywords check if the general forecast matches it. If
# so, set rain_found to 1
# If rain is detected, send an alert
if [ "$rain_found" -eq 1 ]; then
message="Weather Alert: Rain expected! General forecast is '$general_forecast'. Stay prepared!"
# Send the message to the Telegram bot
curl -s -X POST $TELEGRAM_API_URL \
-d chat_id=$CHAT_ID \
-d text="$message" > /dev/null
echo "Alert sent: $message"
else
echo "No rain expected. General forecast is '$general_forecast'."
fi#!/bin/bash
# Replace these with your actual Telegram bot token and chat ID
TELEGRAM_BOT_TOKEN="YOUR_BOT_TOKEN"
CHAT_ID="YOUR_CHAT_ID"
TELEGRAM_API_URL="https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendMessage"
# Replace this with your actual curl command that fetches the JSON weather data
response=$(curl -s "https://api.data.gov.sg/v1/environment/24-hour-weather-forecast
")
# Define an array of rain-related keywords
rain_keywords=("Rain" "Showers" "Thundery Showers" "Heavy Thundery Showers")
# Extract the general weather forecast using jq
general_forecast=$(echo "$response" | jq -r '.items[0].general.forecast')
# Initialize a flag to check if rain is found
rain_found=0
# Check for rain-related keywords in the general forecast
for keyword in "${rain_keywords[@]}"; do
if [[ "$general_forecast" == *"$keyword"* ]]; then
rain_found=1
break
fi
done
# If rain is detected, send an alert
if [ "$rain_found" -eq 1 ]; then
message="Weather Alert: Rain expected! General forecast is '$general_forecast'. Stay prepared!"
# Send the message to the Telegram bot
curl -s -X POST $TELEGRAM_API_URL \
-d chat_id=$CHAT_ID \
-d text="$message" > /dev/null
echo "Alert sent: $message"
else
echo "No rain expected. General forecast is '$general_forecast'."
fi<magic string> <command>0 9 * * * /path/to/script/weather.sh