4
\$\begingroup\$

I'm writing a bash script for my tablet laptop to flip the screen depending on a file in Manjaro Linux. My current script is the following:

while true
do
 state=$(cat /sys/devices/platform/thinkpad_acpi/hotkey_tablet_mode) #This file indicates if it's in tablet mode or not
 if [[ $state == 0 && $oldState == 1 ]]
 then
 xrandr --output LVDS1 --rotate normal
 xsetwacom --set "$a" Rotate none
 xsetwacom --set "$b" Rotate none
 xsetwacom --set "$c" Rotate none
 oldState=0
 elif [[ $state == 1 && $oldState == 0 ]]
 then
 xrandr --output LVDS1 --rotate inverted
 xsetwacom --set "$a" Rotate half
 xsetwacom --set "$b" Rotate half
 xsetwacom --set "$c" Rotate half
 oldState=1
 /home/eto/scripts/backlight 0
 fi
 sleep 1s
done

The code in the if statements don't matter, but what I'm worried about is the entire logic of how it checks the tablet mode. Is there a better way to check the tablet mode indicator file rather than a while loop? I feel like this is a very inefficient way of doing so as it reads the disk quite frequently, but I don't see how else I could approach this. Any input is appreciated as I'm very much an amateur with coding.

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Oct 13, 2019 at 16:05
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Good

I like most of what you've done so far as is:

  • Using [[ (double square brackets) for conditionals is a good practice.
  • Using $() for command substitution instead of the classic backticks is also a good modern shell practice.
  • Most of your variable substitutions are quoted. This is a good habit in case the variable contains spaces it won't get broken up by shell parsing.

Could be better

There are some minor things I'd improve:

  • include the #! line at the top
  • typically you see folks add their thens to the end of the previous line like if [[ cond ]]; then. Your outer loop could also be done as while true; do.
  • the comment gets sort of lost out on the right. Why not put it on the line above?

Think about

The script is getting invoked every second and chewing up some amount of CPU. There's probably not much CPU being burned in this case, but one thing to consider is whether this sort of polling could be avoided. Linux includes inotify as a function to let processes known when a file has changed so they don't have to constantly check the file themselves. This is available in the shell through inotifywait. If the file you're checking works with inotifywait you could avoid the sleep.

A bonus you get beyond conserving CPU from skipping sleep is that the script should run with lower latency. Instead of needing to wait for up to a second to detect the change your script would be invoked within a small fraction of a second.

answered Oct 13, 2019 at 22:18
\$\endgroup\$
1
  • \$\begingroup\$ I was really only looking for you to recommend inotifywait, but thanks for the other suggestions! \$\endgroup\$ Commented Oct 13, 2019 at 23:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.