si3t.ch> cd /


Obtenir le status de mpd avec awk

2021-12-06T13:27:53Z

Pour afficher quelle est la chanson jouée avec mpd dans une barre de status, j'utilisais ce script:

#!/bin/sh

MPC="$(mpc)"
if [ -n "$(printf "%s" "$MPC" |grep playing)" ]; then
	info="$(printf "%s" "$MPC" |head -n1 | cut -b -42)"
	L="$(printf "%s" "$MPC" |grep -Eo '[0-9]+:[0-9]+/[0-9]+:[0-9]+')"
	printf "%s\n" "♪ $info - $L"
fi


exit 0

On voit qu'il appelle mpc, head, cut, grep, et printf, soit en tout 5 commandes.

Voici comment je l'ai simplifié avec awk :

#!/bin/sh

mpc | awk '
(NR == 1) { status = $0; next }
(NR == 2) && /playing/ {
    isplaying = 1
    progress = $3
    next
}
END { if (isplaying) { printf "♪ %s - %s\n", status, progress } }
'

J'aime beaucoup awk :)

Une réaction?

Envoyez votre commentaire par mail.

Mode d'emploi de la liste de diffusion pour recevoir les réponses.