XenoTheStrange

Set all logitech device LED colors via ratbag [Bash]

Nov 25th, 2024
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.64 KB | None | 0 0
  1. #!/bin/bash
  2. #Written by chatgpt with my instruction
  3. #My use for this is to reset the colors on my equipment after it goes back to default from power loss
  4.  
  5. # Default color is 500000 (dark red)
  6. default_color="500000"
  7.  
  8. # Check if a color was passed as the first argument
  9. if [[ $1 =~ ^[0-9a-fA-F]{6}$ ]]; then
  10.     color="$1"
  11.     echo "Using custom color: $color"
  12. else
  13.     color="$default_color"
  14.     echo "No valid color provided, using default: $color"
  15. fi
  16.  
  17. # Get the list of devices
  18. device_list=$(ratbagctl list)
  19.  
  20. # Parse the device names (those containing a colon followed by a name)
  21. for device in $(echo "$device_list" | grep -oP '^\S+(?=:)' | uniq); do
  22.     echo "Processing device: $device"
  23.  
  24.     # Get the list of LEDs for this device
  25.     led_info=$(ratbagctl "$device" led get)
  26.  
  27.     # Extract LED numbers and current colors
  28.     led_list=$(echo "$led_info" | grep -oP '(?<=LED: )\d+')
  29.     led_colors=$(echo "$led_info" | grep -oP '(?<=color: )\S+')
  30.  
  31.     # Loop through each LED and check the current color
  32.     i=1  # Index to keep track of current LED in the led_colors list
  33.     for led in $led_list; do
  34.         # Get the current color of the LED (nth entry from led_colors)
  35.         current_color=$(echo "$led_colors" | sed -n "${i}p")
  36.  
  37.         # Compare current color with the target color
  38.         if [[ "$current_color" == "$color" ]]; then
  39.             echo "LED $led on $device is already set to $color. Skipping..."
  40.         else
  41.             echo "Setting LED $led on $device to color $color"
  42.             ratbagctl "$device" led "$led" set color "$color"
  43.         fi
  44.  
  45.         ((i++))  # Move to the next LED color
  46.     done
  47. done
  48.  
Advertisement
Add Comment
Please, Sign In to add comment