1

I'm trying to make script that monitor laptops (T480) batteries. There are 2 batteries, internal and external. Externals information seems to disappear capacity while it's charging.

/sys/class/power_supply/BAT0/capacity

Problem

I made check for the charging, if the file exists, problem is that it pass the -f on missing capacity, it's ASCII text so it shouldn't be a problem.

Script

I've tried -e -d -c too, but no success.

#!/bin/bash
...

[[ -f /sys/class/power_supply/BAT0/capacity ]] && {
    EXTERNAL="$(cat /sys/class/power_supply/BAT0/capacity)" 
   ...some logic
}
..
Filip Seman
  • 1,252
  • 2
  • 15
  • 22
  • Maybe you can try making a second check to see if the file is not empty, like this: ```[[ $(du your_file | cut -f 1) > 0 ]]``` or more easily, with ```[[ -s your_file ]] ``` – Miguel May 10 '21 at 09:53
  • `-s` check If file exists and its size is greater than zero, it's ok if it's empty, my goal is to avoid logic on non existing file after `-f` check. – Filip Seman May 10 '21 at 11:39
  • It's really difficult to reproduce this. Are you sure that the file actually disappears? Make sure that you are not creating the file again in a previous command in your script – Miguel May 10 '21 at 11:45
  • `/sys/class/` contains every device class registered with the kernel, I don't write to any of these files, I just read the output. – Filip Seman May 10 '21 at 11:53
  • 1
    `/sys/class/power_supply/BAT0` is a syslink. Maybe consider checking if [the syslink is there](https://stackoverflow.com/questions/5767062/how-to-check-if-a-symlink-exists) before checking the file inside it. – 0stone0 May 10 '21 at 12:06
  • 1
    thank you very much `-L` check works! – Filip Seman May 10 '21 at 12:17

1 Answers1

1

Converting my comment to an answer


/sys/class/power_supply/BAT0 is a symlink, as shown by the output of ls -lta:

root@deb:~# cd /sys/class/power_supply/
root@deb:/sys/class/power_supply# ls -tla
total 0
lrwxrwxrwx  1 root root 0 May 21 14:02 BAT0 -> ../../devices/LNXSYSTM:00/LNXSYBUS:00/PNP0C0A:00/power_supply/BAT0
lrwxrwxrwx  1 root root 0 May 21 14:02 ADP0 -> ../../devices/LNXSYSTM:00/LNXSYBUS:00/ACPI0003:00/power_supply/ADP0
drwxr-xr-x 48 root root 0 May 21 14:02 ..
drwxr-xr-x  2 root root 0 May 21 14:02 .
root@deb:/sys/class/power_supply#

Obnoxious screen shot

Instead of using the -f flag which:

True if file exists and is a regular file.

Since you're dealing with a symlink, I'd recommend -L flag which

True if file exists and is a symbolic link.

0stone0
  • 34,288
  • 4
  • 39
  • 64
  • 1
    Please [don’t post images of code, error messages, or other textual data.](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) – tripleee May 21 '21 at 08:09