Fandom

Music Player Daemon Community Wiki

Hack:mpd rmprev.sh

525pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

  • Description: Small bash script which removes the previously played song from the playlist when a new song is played.
  • Other info: Requested by Kr1st1an in #mpd at 19:54, 2005-01-20.
  • Author: Bumby
  • License: Oh, come on. It's a 13 line bash script.
  • Code
#!/bin/bash

cur=`mpc|head -n 2|tail -n 1|sed -e 's/.*#\([0-9]*\).*/\1/'`
prev=$cur

while [ 1 ];do
        sleep 1;
        cur=`mpc|head -n 2|tail -n 1|sed -e 's/.*#\([0-9]*\).*/\1/'`
        if [ $cur != $prev ];then
                mpc del $prev
                let "prev=$cur-1"
        fi
done


  • Please give more information about where to put the script, when to execute it... should it keep running in the background? --ep
  • The script above should be running in the background -- the looping construct (see "while" above) checks once per second to see if the currently playing song has changed. I put the script into /usr/sbin and call it on startup. --Wjs

Here is a modified version of Bumby's script. By no means am I an expert programmer...  :) --Wjs

Changes:

  • Move the check for currently playing song to a function
  • Change regex to match one or more digits (old was '*' = match 0 or more)
  • Check every three seconds instead of every one second
  • Generic check for numeric response from head/tail/sed line -- return zero if numeric response was not detected
  • only delete from playlist if cur > prev (unless cur is 0, which means user hit stop or end of playlist was reached). Granted, this should not happen normally, but one instance would be if you add a bunch of songs, select a high song on the list (like 7) and then do a shuffle. In this case cur becomes 1 and prev would be 7, but 7 is an unknown song, so we don't want to delete it.
#!/bin/bash
cur=0
prev=0
getcur() {
   cur=`mpc | head -n2 | tail -n1 | sed -e 's/.*#\([0-9]\+\).*/\1/'`
   temp=`echo $cur | grep -e '^[0-9]\+'`  #make sure we have a number
   #if $temp is empty, set cur equal to zero
   if [ -z "$temp" ]; then
      cur=0
   fi
}
while [ 1 ] ; do
   #continuously check for a song
   while [ $cur -eq 0 ]; do
      sleep 3;
      getcur
      prev=$cur
   done
   getcur
   sleep 3;
   if [ $cur != $prev ] ; then
      if [ $cur -gt $prev ]; then
         mpc del "$prev"
         if [ $cur -gt 1 ]; then
            let prev=$cur-1
         else
            let prev=$cur
         fi
      else
         #for some reason cur < prev (shuffle?) do nothing unless
         #cur is now zero, in that case the user hit stop or we are
         #finished with the playlist.
         if [ $cur -eq 0 ]; then
            mpc del "$prev"
         fi
         let prev=$cur
      fi
   fi
done

Also on Fandom

Random Wiki