0

I have successfully made a bash script that needs root, permission, which I can run as root! But, the script only runs when I am using the terminal as root and if I type exit, it runs. When I type :

bash lights_infinity.sh

The terminal does nothing. Why is this so and is it normal? The code is

#!/bin/bash

sudo su
while true
do
    sudo -k setleds -D +caps < /dev/console
    sleep 0.1
    sudo -k setleds -D +caps < /dev/console
    sleep 0.1
    sudo -k setleds -D -caps < /dev/console
    sleep 0.1
done

Do I need to remove the sudo commands?

  • 3
    yes, just sudo /path/to/script . Put a shebang at the top #!/bin/bash – Panther Mar 7 '16 at 18:28
  • 2
    sudo su is opening a new shell – Andrea Corbellini Mar 7 '16 at 18:34
  • @AndreaCorbellini , Surprise, surprise, when I run it without the use of sudo su, it asks for my password, and opens nothing new – Ubuntulover Mar 7 '16 at 18:56
  • question: when is this loop suppose to end? Oh: and the one I posted below seems to run with a single "sudo" ;) – Rinzwind Mar 7 '16 at 18:59
5

I would

#!/bin/bash
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi
while true
do
    setleds -D +caps < /dev/console
    sleep 0.1
    setleds -D +caps < /dev/console
    sleep 0.1
    setleds -D -caps < /dev/console
    sleep 0.1
done

and do this:

sudo lights_infinity.sh

(add the -k if you want to kill the 15 minute time-frame for sudo but should not be needed)


The reason you need the "exit" is due to the "sudo su" you used. That opens an extra shell you need to "exit".


  • extra: check for "root" at the beginning of the file. If this runs for 15 minutes it might error out(not sure); if so add "sudo" to the "setleds" and it will stop and ask the password.
  • extra2: 3 times "setleds -D +(/-)caps < /dev/console sleep 0.1" looks better when you use a function. And the while loop will look better if it has a nicer exit than control-x.
  • root@ILOVEROBLOX123:/home/meshak/Desktop# sudo sh lights_infinity.sh lights_infinity.sh: 2: lights_infinity.sh: [[: not found And I have my keyboard light doing a light show! Thank you! – Ubuntulover Mar 7 '16 at 19:19
  • it works for me??? (~/leds$ ./lights_infinity.sh shows This script must be run as root) – Rinzwind Mar 7 '16 at 19:20
  • @Rinzwind you need to run it with sudo at the start. So $ sudo ./lights_infinity.sh – Someone Somewhere Mar 7 '16 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.