You are not logged in.

#1 2015-09-08 18:49:49

Jack450
Member
From: Los Angeles
Registered: 2010-07-11
Posts: 58

[Solved]Scripting for mpc

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

#2 2015-09-08 19:21:18

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: [Solved]Scripting for mpc

Take a look at getopt(s)


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#3 2015-09-08 20:23:49

teckk
Member
Registered: 2013-02-21
Posts: 519

Re: [Solved]Scripting for mpc

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

#4 2015-09-09 06:14:59

Jack450
Member
From: Los Angeles
Registered: 2010-07-11
Posts: 58

Re: [Solved]Scripting for mpc

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

#5 2015-09-09 07:04:16

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: [Solved]Scripting for mpc

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
...

No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#6 2015-09-09 08:08:06

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [Solved]Scripting for mpc

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.

Offline

#7 2015-09-09 14:20:08

Jack450
Member
From: Los Angeles
Registered: 2010-07-11
Posts: 58

Re: [Solved]Scripting for mpc

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

#8 2015-09-09 15:03:56

Jack450
Member
From: Los Angeles
Registered: 2010-07-11
Posts: 58

Re: [Solved]Scripting for mpc

Raynman wrote:
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

#9 2015-09-09 18:04:23

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [Solved]Scripting for mpc

Jack450 wrote:

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

#10 2015-09-10 12:55:44

chittu
Member
Registered: 2014-01-26
Posts: 19

Re: [Solved]Scripting for mpc

noob here! This is interesting for me as I regularly use mpc & ncmpcpp but I don't know how to run this script sad

I saved it as test.sh in my current working directory and ran like

./test.sh p

but I have no effect sad

Offline

#11 2015-09-10 18:36:52

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: [Solved]Scripting for mpc

chittu wrote:

I saved it as test.sh in my current working directory and ran like

./test.sh p

but I have no effect sad

./test.sh -p

No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#12 2015-09-11 03:09:24

chittu
Member
Registered: 2014-01-26
Posts: 19

Re: [Solved]Scripting for mpc

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

Board footer

Powered by FluxBB