1

I've found similar problems on stackexchange, but never close enough to my case to help.

I'm trying to create a script and bind it to a key, so that when pressed, it displays "title by artist" OR, if there is no artist/title, it displays "filename".

Cmus (my music player of choice) has cmus-remote paired with the -C (raw) option which let me query cmus player for anything and get a newline-separated response like:

cmus-remote -C 'format_print %a %t %F'
Beach House
Space Song
/home/user/Music/Beach House - Space Song.mp3

The thing is, I don't want to run cmus-remote for each of the queries and instead would prefer

IFS= read -r artist title file << EOF
$(cmus-remote -C 'format_print %a %t %F')
EOF

However that only provides artist and title and file are empty Note: removing the -r option doesn't help in this case, nor does modifying IFS.

To elaborate: my Music folder isn't fully tagged, i.e. some songs don't have an artist or title, so the idea of this script is on exec to send a notification 'playing title by artist', if there's no title or artist 'playing filename' so I kindof need to have the three variables to then evaluate wich notification to send.

terdon
  • 242,166
  • while yes, I do indeed use bash, i would prefer to have something more portable, though I will use bash if that's the more practical thing to do – Dr. Coomer May 15 '23 at 12:18
  • oops I completely misunderstood the task – steeldriver May 15 '23 at 13:14
  • Oh, oh, oh, sorry! Was that an answer? I thought you were just clarifying what you need. Answering your own question is not frowned upon at all. It is actually actively encouraged. This still doesn't make the variables survive after the while finishes, is that really all you need? If so, please let me know and I will udnelete your answer with my apologies. – terdon May 15 '23 at 13:44
  • @terdon I believe that was an answer. I have undeleted. – Kusalananda May 15 '23 at 13:46
  • Fair enough. Sorry, @Dr.Coomer, my bad! I now rolled back my edit since it was I, not you, who was being dumb! – terdon May 15 '23 at 13:47
  • Thanks folks! while I suppose having variables survive outside the loop might be nice, I don't exactly need it, a simple if a and t else f inside the loop is just fine for what I'm trying to do – Dr. Coomer May 15 '23 at 14:37

1 Answers1

0

I just need a read for every newline, i.e.

cmus-remote -C 'format_print %a %t %F' | 
while IFS= read -r artist; IFS= read -r title; IFS= read -r file
do
  echo "a:$artist"
  echo "t:$title"
  echo "f:$file"
done

runs once and echoes the correct value, why IFS=$'\n' doesn't act like you'd think is beyond me, I'll accept three reads

Kusalananda
  • 333,661