4
\$\begingroup\$

I need to fix and maintain the master volume in PulseAudio (so that I don't blow up my speakers). E.g. call gfaaudio -v 25 to fix volume at 25%. Is there a cleaner way to get this done?

gfaaudio:

#!/bin/bash
usage="Usage: `basename 0ドル` [-v VOL] "
if [ "1ドル" = "-h" ] || [ "1ドル" = "--help" ]; then 
 echo $usage
 echo " -h, --help for help"
 exit
fi 
#############################
Dcard="alsa_card.usb-Musical_Fidelity_Musical_Fidelity_V90-DAC_24_96-00-M2496"
Dout="iec958-stereo"
Dname="alsa_output.usb-Musical_Fidelity_Musical_Fidelity_V90-DAC_24_96-00-M2496.iec958-stereo"
VOL=30
#############################
while [ $# -gt 0 ]; do
 case "1ドル" in
 -v)
 shift
 [ $# -gt 0 ] && VOL="1ドル";;
 -*)
 echo "Option not supported: 1ドル" >&2
 echo $usage >&2
 exit 1;;
 *) break;;
 esac
 shift
done
pacmd set-card-profile "$Dcard" output:"$Dout"
pacmd set-default-sink "$Dname"
[ -z "$(pidof -x gfaaudioloop)" ] || killall gfaaudioloop
gfaaudioloop "$Dname" "$VOL" &
exit

gfaaudioloop:

#!/bin/bash
if [[ $# != 2 ]]; then 
 echo "number of arguments not 2" >&2 
 exit 1
fi
while : ; do
 exits=$(pactl set-sink-volume "1ドル" "2ドル"%)
 exitc=$?
 [ ! -z "$exits" -o $exitc -ne 0 ] && break
 sleep 0.5
done
exit
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Aug 28, 2014 at 15:53
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Use $(...) style command substitution instead of the deprecated `...` style. And double-quote variables if they might contain spaces. So instead of this:

usage="Usage: `basename 0ドル` [-v VOL] "

Write like this:

usage="Usage: $(basename "0ドル") [-v VOL] "

Testing with [ ... ] is deprecated in favor of the more modern and powerful [[ ... ]]. So instead of this:

if [ "1ドル" = "-h" ] || [ "1ドル" = "--help" ]; then 

Write like this:

if [[ 1ドル = -h || 1ドル = --help ]]; then 

This is equivalent, but notice that it's shorter, and in this form there is no need for those double-quotes. (Strictly speaking, in [ ... ] there was no need to double-quote "-h" and "--help", only "1ドル", the variable).


But in any case, why not move the handling of -h and --help together with the other options of the script?

while [[ $# -gt 0 ]]; do
 case "1ドル" in
 -h|--help)
 echo $usage
 echo " -h, --help for help"
 exit;;
 -v)
 shift
 [[ $# -gt 0 ]] && VOL="1ドル";;
 -*)
 echo "Option not supported: 1ドル" >&2
 echo $usage >&2
 exit 1;;
 *) break;;
 esac
 shift
done

This way it's simpler, and now you can use -h and --help anywhere on the command line, not only as the first argument.


About this bit:

[ -z "$(pidof -x gfaaudioloop)" ] || killall gfaaudioloop

I don't have the pidof command so I can't really test it, but in general it doesn't sound good to put the output of a program into a string just to check with -z if it's empty. Usually there is a more direct way, using the exit code of the program. Here's an example that should come quite close to what you want:

ps xa | grep -q gfaaudioloop && killall gfaaudioloop

Having the exit at the end of a script makes is exit with success. Without it, the exit code of the script will be that of the last command executed. If you correctly check for and handle the exit code of all commands, then you don't need to explicitly exit. Let the script exit normally, using the exit code of the last command executed.

answered Aug 28, 2014 at 22:19
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.