Skip to content

Commit

Permalink
Update numlock.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmistry2000 authored Sep 11, 2024
1 parent de255d4 commit 9b3a396
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions numlock.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#!/bin/sh -e

# setleds can be used in all distros
# This method works by calling a script using systemd service
# Define color variables
RC='\033[0m' # Reset
RED='\033[31m' # Red
GREEN='\033[32m' # Green
YELLOW='\033[33m' # Yellow
BLUE='\033[34m' # Blue

# Create a script to toggle numlock
create_file() {
echo "Creating script..."
echo "${BLUE}Creating script...${RC}"
sudo tee "/usr/local/bin/numlock" >/dev/null <<'EOF'
#!/bin/bash
Expand All @@ -16,11 +20,12 @@ done
EOF

sudo chmod +x /usr/local/bin/numlock
echo "${GREEN}Script created and permissions set.${RC}"
}

# Create a systemd service to run the script on boot
create_service() {
echo "Creating service..."
echo "${BLUE}Creating service...${RC}"
sudo tee "/etc/systemd/system/numlock.service" >/dev/null <<'EOF'
[Unit]
Description=numlock
Expand All @@ -33,21 +38,26 @@ RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
echo "${GREEN}Service file created.${RC}"
}

main() {
# Check if the script and service files exists
# Check if the script and service files exist
if [ ! -f "/usr/local/bin/numlock" ]; then
create_file
else
echo "${YELLOW}Script already exists.${RC}"
fi

if [ ! -f "/etc/systemd/system/numlock.service" ]; then
create_service
else
echo "${YELLOW}Service file already exists.${RC}"
fi

# Always enable the numlock service
sudo systemctl enable numlock.service --quiet
echo "Numlock will be enabled on boot"
echo "${GREEN}Numlock service will be enabled on boot.${RC}"
}

main

0 comments on commit 9b3a396

Please sign in to comment.