Bash Programming

bash script: Dynamic Wallpaper Changer Script


#!/bin/bash

# Function to change wallpaper based on time of day
change_wallpaper_time() {
current_hour=$(date +"%H")

if [ $current_hour -ge 0 ] && [ $current_hour -lt 12 ]; then
# Morning wallpaper
gsettings set org.gnome.desktop.background picture-uri "file:///path/to/morning-wallpaper.jpg"
elif [ $current_hour -ge 12 ] && [ $current_hour -lt 18 ]; then
# Afternoon wallpaper
gsettings set org.gnome.desktop.background picture-uri "file:///path/to/afternoon-wallpaper.jpg"
else
# Evening wallpaper
gsettings set org.gnome.desktop.background picture-uri "file:///path/to/evening-wallpaper.jpg"
fi
}

# Function to change wallpaper based on weather conditions
change_wallpaper_weather() {
current_weather=$() # Replace with actual command

if [ "$current_weather" = "sunny" ]; then
gsettings set org.gnome.desktop.background picture-uri "file:///path/to/sunny-wallpaper.jpg"
elif [ "$current_weather" = "rainy" ]; then
gsettings set org.gnome.desktop.background picture-uri "file:///path/to/rainy-wallpaper.jpg"
else
gsettings set org.gnome.desktop.background picture-uri "file:///path/to/default-wallpaper.jpg"
fi
}

# Main script logic
# Check if weather condition-based wallpaper is enabled
if [ "$1" = "--weather" ]; then
change_wallpaper_weather
else
change_wallpaper_time
fi

This bash script is a dynamic wallpaper changer that changes the desktop wallpaper based on either the time of day or weather conditions. It defines two functions: `change_wallpaper_time` and `change_wallpaper_weather`. The `change_wallpaper_time` function calculates the current hour and sets the wallpaper based on whether it is morning, afternoon, or evening. The `change_wallpaper_weather` function retrieves the current weather (using a placeholder command) and sets the wallpaper based on sunny, rainy, or default conditions.

The main script logic checks if the `–weather` flag is provided as a command-line argument. If the flag is present, it calls the `change_wallpaper_weather` function to set the wallpaper based on weather conditions. Otherwise, it defaults to calling the `change_wallpaper_time` function to set the wallpaper based on the time of day.

This script leverages `gsettings` to change the desktop wallpaper for a GNOME desktop environment. Make sure to replace the placeholder paths with actual paths to your wallpaper images and the command to get the current weather information.