1

I have the following command:

pactl list sinks | perl -000ne 'if(/#1/){/(Volume:.*)/; print "$1\n"}'

it returns the output as:

Volume: 0:  50% 1:  50%

it denote the pulseaudio volume for sink 1 is 50%.

So I want to check it in if condition the volume <= 50% or not

So how can i do this?

1

You can use the following command (enhanced version of terdon's proposal):

pactl list sinks | perl -000ne 'if(/#1/){/Volume:.*\s+(\d+)%.*\s+(\d+)%/; exit(1) if ($1>50 or $2>50)}'

It will check that both Left and Right channels are above 50%, and will exit with the value 1 (0 instead)

You can check the exit code with echo $?:

$ pactl list sinks | perl -000ne 'if(/#1/){/Volume:.*\s+(\d+)%.*\s+(\d+)%/; exit(1) if ($1<=50 or $2<=50)}'
$ echo $?
0

UPDATE:

To include the following command in a shell script, simply use the following snippet:

#!/bin/bash

pactl list sinks | perl -000ne 'if(/#1/){/Volume:.*\s+(\d+)%.*\s+(\d+)%/; exit(1) if ($1>50 and $2>50)}'
if [ $? -ne 0 ]; then
    echo "Volume > 50%"
else
    echo "Volume <= 50%"
fi
  • I want check the volume is <= 50, if it is <=50 then I want to execute some logic otherwise goes to the else block – Prakash V Holkar Apr 28 '14 at 10:56
  • I've updated my answer, please have a look to the shell sample code for a possible use. – Sylvain Pineau Apr 30 '14 at 0:03

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.