I am writing a bash script to set up the all the software I need after installing Ubuntu. If the computer is a laptop, I want to install tlp for power management. If it is a desktop, tlp is not necessary.
Is there a command with which I can figure out whether the computer is a desktop or a laptop/tablet?
Perhaps, detecting the presence of a battery is the signature I should look for.
I don't think checking the battery is a good solution: A desktop may have UPS that shows up as battery.
You can use the command:
The output will be
See
man hostnamectl
.Another option would be to use:
Note that in this case, the output may be something like "Laptop", "Notebook", "Portable", "Hand held" or "Sub Notebook" depending on the manufacturer's designation.
There is also a script named:
You can inspect its (
/bin/laptop-detect
) contents for a more detailed approach.Checking on whether or not the system has a battery is not reliable - a UPS connected to the system may show up as a battery (which showed up when I checked).
So one way is to use
dmidecode
to get the chassis type of the system:But this command requires
sudo
. To avoid usingsudo
, you can print the contents of/sys/class/dmi/id/chassis_type
, which will return the decimal value of the chassis type.But when I checked, both of the commands gave output where my desktop was considered as "Rack Mount Chassis" (decimal value 23) and my laptop as "Notebook" (decimal value 10). So it might vary according to the system that we are handling.
That's when I used the
hostnamectl
command and got better values from the Chassis and Icon name:Since my desktop machine didn't have any Chassis option, I used the Icon name value:
So on my laptop the result was "computer-laptop", on desktop it's just "computer" and on my server, it is "computer-vm". You can use these values to install the tlp on the system.