You are not logged in.
Pages: 1
I want to make a simple script for mpc which lowers vol, plays, stop, etc but I can't remember how you do that exactly. Is there anywhere I can look it up? What I'm trying to do is make a script with different options that are attach to different commands. If I "testscript -p" it will pause music but if I "testscript -s" it will stop it.
Last edited by Jack450 (2015-09-09 06:16:04)
Welcome to the Internet!
This isn't Daycare or the Justice System, here you're retarded until proven innocent.
Offline
Offline
http://linux.die.net/man/1/mpc
......
volume [+-]<num>
Sets the volume to <num> (0-100). If "+" or "-" is used, then it adjusts the volume relative to the current volume.
pause
Pauses playing.
play <position>
Starts playing the song-number specified. If none is specified, plays number 1.
.....
Depends on what you want your script to do and look like.
http://mywiki.wooledge.org/BashFAQ/
http://tldp.org/LDP/abs/html/
http://linuxconfig.org/bash-scripting-tutorial
Example:
#! /usr/bin/env bash
PS3="
Select an option. :"
while :; do
clear
options="vol+ vol- stop play quit"
select opt in $options; do
case $opt in
quit) clear; exit ;;
vol+) echo "Volume up"; sleep 1; break ;;
vol-) echo "Volume down"; sleep 1; break ;;
stop) echo "Stopping"; sleep 1; break ;;
play) echo "Playing"; sleep 1; break ;;
esac
done
done
Offline
Thanks for the help guys I made an mpc script and bound them to my special keys
This is the script :
#!/bin/bash
PLAY="mpc play"
STOP="mpc stop"
PAUSE="mpc pause"
VOLUP="mpc volume +1"
VOLDOWN="mpc volume -1"
NEXT="mpc next"
PREV="mpc prev"
MUTE="mpc volume 0"
UNMUTE="mpc volume +100"
while getopts "P p s u d n b m o" opt; do
case $opt in
P)
$PLAY
;;
p)
$PAUSE
;;
s)
$STOP
;;
u)
$VOLUP
;;
d)
$VOLDOWN
;;
n)
$NEXT
;;
b)
$PREV
;;
m)
$MUTE
;;
o)
$UNMUTE
;;
esac
Is this fine or is there a cleaner script?
Welcome to the Internet!
This isn't Daycare or the Justice System, here you're retarded until proven innocent.
Offline
Why the need for a script at all, why not just bind the keys directly to the function you wish to call?
Example from my herbstluftwm config...
...
hc keybind XF86AudioPlay spawn mpc toggle
hc keybind XF86AudioNext spawn mpc next
hc keybind XF86AudioPrev spawn mpc prev
hc keybind XF86AudioMute spawn pulseaudio-ctl mute
hc keybind XF86AudioRaiseVolume spawn pulseaudio-ctl up
hc keybind XF86AudioLowerVolume spawn pulseaudio-ctl down
...
Offline
Is this fine or is there a cleaner script?
It's fine, but see slithery's reply. I would say it's cleaner without the variables (each one is only used once anyway). And since all you're doing is mapping each option to an mpc command, you could make that mapping explicit with an associative array:
#!/bin/bash
declare -A opts2cmds=(
[P]="play"
[p]="pause"
[s]="stop"
[u]="volume +1"
[d]="volume -1"
[n]="next"
[b]="prev"
[m]="volume 0"
[o]="volume 100"
)
while getopts "${!opts2cmds[*]}" opt; do
mpc ${opts2cmds[$opt]}
done
Now, instead of a variable, a case branch and a letter in the getopts optstring (three places to edit), each option/command is defined on a single line.
Offline
No big reason, I just wanted to script it and get an idea how to do it.
Welcome to the Internet!
This isn't Daycare or the Justice System, here you're retarded until proven innocent.
Offline
Jack450 wrote:Is this fine or is there a cleaner script?
It's fine, but see slithery's reply. I would say it's cleaner without the variables (each one is only used once anyway). And since all you're doing is mapping each option to an mpc command, you could make that mapping explicit with an associative array:
#!/bin/bash declare -A opts2cmds=( [P]="play" [p]="pause" [s]="stop" [u]="volume +1" [d]="volume -1" [n]="next" [b]="prev" [m]="volume 0" [o]="volume 100" ) while getopts "${!opts2cmds[*]}" opt; do mpc ${opts2cmds[$opt]} done
Now, instead of a variable, a case branch and a letter in the getopts optstring (three places to edit), each option/command is defined on a single line.
Can two letters be added to an options?
ex:
[nx]="next"
Welcome to the Internet!
This isn't Daycare or the Justice System, here you're retarded until proven innocent.
Offline
Can two letters be added to an options?
ex:[nx]="next"
You can put it in the array, but it won't work with bash getopts. But I only used that because you did. And I left out a check for an invalid option, so getopts was only useful to strip the dash and support multiple commands/options in one invocation of the script. You can replace the while loop with something like this (with check for invalid options, but only the first option/action will be processed):
if [[ $1 != -* || -z ${opts2cmds[${1:1}]} ]]; then
echo -e >&2 "usage: $0 -OPTION\nwhere OPTION is one of ${!opts2cmds[*]}"
exit 1
fi
mpc ${opts2cmds[${1:1}]}
or slightly simpler, for `script nx` instead of `script -nx`:
if [[ -z $1 || -z ${opts2cmds[$1]} ]]; then
echo -e >&2 "usage: $0 OPTION\nwhere OPTION is one of ${!opts2cmds[*]}"
exit 1
fi
mpc ${opts2cmds[$1]}
If you want to continue this, have a look at some tutorials (see teckk's links for example).
Offline
noob here! This is interesting for me as I regularly use mpc & ncmpcpp but I don't know how to run this script
I saved it as test.sh in my current working directory and ran like
./test.sh p
but I have no effect
Offline
I saved it as test.sh in my current working directory and ran like
./test.sh p
but I have no effect
./test.sh -p
Offline
why do I get this error?
$ ./mpc.sh -p
./mpc.sh: line 11: syntax error near unexpected token `do'
./mpc.sh: line 11: ` [o]="volume 100" ) while getopts "${!opts2cmds[*]}" opt; do'
$ ./mpc.sh p
./mpc.sh: line 11: syntax error near unexpected token `do'
./mpc.sh: line 11: ` [o]="volume 100" ) while getopts "${!opts2cmds[*]}" opt; do'
$ cat mpc.sh
#!/bin/bash
declare -A opts2cmds=(t=`/bin/date` echo the time is $t
[P]="play"
[p]="pause"
[s]="stop"
[u]="volume +1"
[d]="volume -1"
[n]="next"
[b]="prev"
[m]="volume 0"
[o]="volume 100" ) while getopts "${!opts2cmds[*]}" opt; do
mpc ${opts2cmds[$opt]}
done
$ chmod +x mpc.sh
Offline
Pages: 1