2

When you type cd /sys/class/drm and then use:

$ ll */status
-rw-r--r-- 1 root root 4096 Dec 23 08:43 card1-DP-1/status
-rw-r--r-- 1 root root 4096 Dec 25 15:22 card1-DP-2/status
-rw-r--r-- 1 root root 4096 Dec 25 15:22 card1-eDP-1/status
-rw-r--r-- 1 root root 4096 Dec 25 15:22 card1-HDMI-A-1/status
-rw-r--r-- 1 root root 4096 Dec 25 15:22 card1-HDMI-A-2/status

followed by:

$ cat */status
connected
disconnected
connected
disconnected
disconnected

You get some useful information about xrandr monitors and which names are connected and which are not. In my case I have to "connect the dots" to know:

  • card1-DP-1/status = connected
  • card1-eDP-1/status = connected

To prevent having to connect the dots, is there a way of matching up virtual file system filenames to their contents?

WinEunuuchs2Unix
  • 99,709
  • 34
  • 237
  • 401

3 Answers3

2

Here's the first thing that came to mind:

$ cd /sys/class/drm
$ grep . */status
card0-DVI-I-1/status:disconnected
card0-DVI-I-2/status:connected
$ 

Note if you have just one interface grep won't list the file name:

$ grep . */status
connected
$

but presumably if you only have one you won't be confused as to which was meant. If needed, use the -H flag:

$ grep -H . */status
card0-VGA-1/status:connected
$

What we used to to do before the -H flag was

$ grep . */status /dev/null
card0-VGA-1/status:connected
$ 
rfm
  • 865
  • 8
  • 15
1

In my ~/.bashrc terminal start up file (where the shell prompt is set) I have this function defined:

dircat () 
{ 
    if [[ $# -eq 0 ]]; then
        echo Directory tree required, eg. 'dircat /sys/class/drm/*/status'.;
    else
        paste <(ls "$@") <(cat "$@") | column -s '  ' -t;
    fi
}

Using same directory in question use the following:

$ cd /sys/calss/drm

/sys/calss/drm$ dircat */status
card1-DP-1/status      connected
card1-DP-2/status      disconnected
card1-eDP-1/status     connected
card1-HDMI-A-1/status  disconnected
card1-HDMI-A-2/status  disconnected

Here is another example using the dircat function:

$ dircat /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq
/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq  800044
/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq  800023
/sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq  800043
/sys/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq  800107
/sys/devices/system/cpu/cpu4/cpufreq/scaling_cur_freq  800030
/sys/devices/system/cpu/cpu5/cpufreq/scaling_cur_freq  800047
/sys/devices/system/cpu/cpu6/cpufreq/scaling_cur_freq  800079
/sys/devices/system/cpu/cpu7/cpufreq/scaling_cur_freq  800106

Finally, one last example:

$ cd /sys/bus/usb/devices/

/sys/bus/usb/devices$ dircat usb*/power/wakeup
usb1/power/wakeup  disabled
usb2/power/wakeup  disabled
usb3/power/wakeup  disabled
usb4/power/wakeup  disabled
WinEunuuchs2Unix
  • 99,709
  • 34
  • 237
  • 401
1

I know that you use bash most of the time in scripting solutions, hence why not take advantage of bash's features, specifically the associative arrays:

$ cd /sys/class/drm
$ declare -A monitor_status
$ for i in * ; do  [ -f "$i"/status ] && monitor_status["$i"]=$(cat "$i"/status ) ; done

Here we load everything into associative array monitor_status where names of the monitors are the keys. Accessing keys in the array later can be done easily via ${!array_name[@]} and corresponding key can be used to access each item in the array itself:

# Note I'm using virtual machine, so only one monitor here
$ for monitor in "${!monitor_status[@]}" ; do printf "%s:%s\n" "$monitor" "${monitor_status[$monitor]}"; done
card0-VGA-1:connected

The advantage here is that

  1. we have one single data structure to hold two related pieces of information, and it can be further used as variables in the script.

  2. no need for extra parsing or complex functions

  3. Native bash tools ( aside from cat, though that can be substituted with just $(< "$i"/status ) ).

  4. short and clear

See also BASH associative array printing

Sergiy Kolodyazhnyy
  • 103,293
  • 19
  • 273
  • 492
  • +1 but you really shouldn't worry about bashing my feelings :) Besides I've taken your year old+ advise and am sloowwwllllyyyy learning python. I met a great girl and human development is slowing down computer development though.... – WinEunuuchs2Unix Dec 26 '18 at 00:47
  • @WinEunuuchs2Unix Combine both: start teaching her Python. Joking of course. Congrats ! As for files, if you're going to go the Python route, that's even better - you can use a dictionary there to do exactly the same thing as associative arrays. – Sergiy Kolodyazhnyy Dec 26 '18 at 00:53
  • As you can imagine when I try to talk about life in **Ask Ubuntu** she rolls her eyes and I shut it. My current focus with Python is morphing SQLite3 and Tkinter together :) – WinEunuuchs2Unix Dec 26 '18 at 01:03
  • @WinEunuuchs2Unix That a good direction. I eventually want to do the same, move some of my older indicators to use SQLite3 instead of json files to store configuration and various other info. As for AskUbuntu and life outside of it, maybe it's for the best to keep them separated, or at least something to think about – Sergiy Kolodyazhnyy Dec 26 '18 at 01:12