forked from ChrisTitusTech/mybash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
numlock.sh
executable file
·62 lines (51 loc) · 1.33 KB
/
numlock.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/sh -e
# Define color variables
RC='\033[0m' # Reset
GREEN='\033[32m' # Green
YELLOW='\033[33m' # Yellow
BLUE='\033[34m' # Blue
# Create a script to toggle numlock
create_file() {
echo "${BLUE}Creating script...${RC}"
sudo tee "/usr/local/bin/numlock" >/dev/null <<'EOF'
#!/bin/bash
for tty in /dev/tty{1..6}
do
/usr/bin/setleds -D +num < "$tty";
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 "${BLUE}Creating service...${RC}"
sudo tee "/etc/systemd/system/numlock.service" >/dev/null <<'EOF'
[Unit]
Description=numlock
[Service]
ExecStart=/usr/local/bin/numlock
StandardInput=tty
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
EOF
echo "${GREEN}Service file created.${RC}"
}
main() {
# 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 "${GREEN}Numlock service will be enabled on boot.${RC}"
}
main