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 ;)