2

I'm making a function that can read the metadata of the current song playing in spotify. This is being programmed in lua since it is an implementation for awesome wm. I got the following line to get all the metadata that I can later use.

handle = io.popen('qdbus org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Metadata | awk -F: \'{$1=\"\";$2=\"\";print substr($0,4)}\'') 

However when Spotify is not running I don't get the expected information and qdbus writes an error to the stderr stream. I wanted to use the fact that qdbus writes to the error stream to determine a fault and stop the program there. (This should also catch any other errors not related to wheter spotify is running or not)

My understanding is that lua popen uses popen3 that can subdivide between stdout and stderr. but all my efforts so far are fruitless and my error stream is always empty. Is it possible to check for a non nil value in the stderr in order to determine a faulty call to qdbus (or awk)?

thanks!

Hoogendijk
  • 100
  • 1
  • 10

3 Answers3

5

I think you can redirect stderr to stdout in the call to popen like this:

handle = io.popen("somecommand 2>&1")
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    not what I meant, I actually want the io.popen to write to 2 different variables depending wheter they come from stderr or stdout. This will simply redirect the stderr to stdout. Sorry if my question wasn't clearer – Hoogendijk Jun 05 '13 at 11:58
  • @Hoogendijk In that case, I don't thing `popen` can do the job. Since it is implemented with `fork` and `pipe` in C (I haven't confirmed it.) – Yu Hao Jun 05 '13 at 12:25
1

If you want to differentiate stderr and stdout, you cannot do it with the io library but you can with luaposix. See this answer for instance.

Community
  • 1
  • 1
catwell
  • 6,770
  • 1
  • 23
  • 21
0

You can checkout juci.exec which I wrote for JUCI webgui. I struggled with the same problem and I ended up using luaposix for this kind of thing when I really need two separate streams. My implementation also gives you the program exit code which is good for testing for errors: https://github.com/mkschreder/juci/blob/master/juci/lua/core.lua

user2826084
  • 517
  • 5
  • 11