0

I've got a script that loops:

#!/bin/sh
while [ true ]
do
  mpc current > current_song.txt
  mpc idle player
done

However sometimes it fails to get the song details and creates a blank file. FFMpeg is reading this file and it crashes if its blank. Is there any way to fail safe the script so if the file is blank it adds a certain text?

Would the best way be to create a script that tries to read the file and if it turns out blank to insert some text and then sleep for a period of time or is there a more elegant way to do it?

Orophix
  • 33
  • 1
  • 7
  • Could you add "if [ ! -s ]; then echo Not found > current_song.txt; fi" after the "mpc current" line? This just writes "Not found" to the file if it is empty. By the way, your first line should be "while true" (you don't need the square brackets). – simon3270 Nov 20 '18 at 11:10
  • Slight error in previous line - missed out he filename in the test! It should be "if [ ! -s current_song.txt ]" – simon3270 Nov 20 '18 at 12:20
  • Thanks a lot! Got it working with this :) – Orophix Nov 21 '18 at 12:38

1 Answers1

0

If the file is truly empty ("ls -l" shows length 0) you can put some text into the file with the following:

#!/bin/sh
while true
do
  mpc current > current_song.txt
  if [ ! -s current_song.txt ]; then
    echo SongNotFound.mp3 > current_song.txt
  fi
  mpc idle player
done
simon3270
  • 722
  • 1
  • 5
  • 8