1

I have a script that prints the volume of my laptop, which is really convoluted and slow:

/bin/sh -c \"amixer get Master "
                                       "| tail -n1 "
                                       "| grep -Po '\\[\\K[^%]*' "
                                       "| head -n1\""

In Unix, so I assume Linux as well, everything is a file. So where is the file holding the volume, so I can just cat that? Like

/sys/class/backlight/acpi_video0/brightness
5
  • Please provide an OS/release detail; SE Unix & Linux covers Linux (Unix too) so why are you asking here? Do note: how apps see the root file-system varies; so product & release details matters with Ubuntu due to confinement & other differences on some products.
    – guiverc
    May 24, 2023 at 7:35
  • Why the need for a file? A function is all you need and a lot more stable.
    – Rinzwind
    May 24, 2023 at 8:05
  • @Rinzwind is it though? You can read the file in any Linux distribution (I am including future versions of Ubuntu in this), as long as future kernels don't handle it differently. The script I use or you provided will break whenever amixer or awk breaks or is not installed. Also, performance. I am querying this 10 times per second on old hardware. May 24, 2023 at 13:28
  • @sadkfdsfjklsdfjklds see my edit. Looks like the volume is retrieved from the device using ioctl. How's your C?
    – Rinzwind
    May 24, 2023 at 15:29
  • @Rinzwind Thank you! I use C for pretty much everything, so should be able to figure it out. May 24, 2023 at 16:04

1 Answer 1

1

There is already a tool for that and it is available on all Ubuntu. Your command can be used like this:

amixer sget Master | awk -F"[][]" '/Left:/ { print $2 }'

for a system that has stereo to probe the LEFT speaker. Other options are "Right" and "Mono" and it will show the volume as a percentage (100%). Just put that result in a variable.


To fix guiverc's comment:

amixer get Master | egrep -o '[0-9]{1,3}%'

will show all percentages for all lines (if 1 percentage it is Mono, otherwise Left, Right in that order)


To address the "file" method:

From what I see in the source code /dev/dsp is probed using ioctl.

#include <sys/ioctl.h>

 int ioctl(int fd, unsigned long request, ...);

The ioctl() system call manipulates the underlying device parameters of special files. In particular, many operating characteristics of character special files (e.g., terminals) may be controlled with ioctl() requests. The argument fd must be an open file descriptor.

The second argument is a device-dependent request code. The third argument is an untyped pointer to memory. It's traditionally char *argp (from the days before void * was valid C), and will be so named for this discussion.

An ioctl() request has encoded in it whether the argument is an in parameter or out parameter, and the size of the argument argp in bytes. Macros and defines used in specifying an ioctl() request are located in the file <sys/ioctl.h>. See NOTES.

But my C knowledge is very limited ;)

1
  • @guiverc is this edit better? :)
    – Rinzwind
    May 24, 2023 at 15:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .