1

I am writing a ruby script that prints by calling:

`/usr/bin/lp -d PrinterQueue -U #{user} #{fileToBePrinted}`

I would like to handle printing errors gracefully, but can't determine what lp returns when I execute it. Usually it is a string like this:

request id is PrinterQueue-68 (1 file(s))

Is there anywhere that describes what lp should return in strange cases?

Thanks!

Jay
  • 2,861
  • 3
  • 29
  • 51

2 Answers2

2

Allow me to elaborate on my comment a little.

You should forget about using backticks for this and go straight to Open3. In particular, Open3.capture3:

out, err, status = Open3.capture3("/usr/bin/lp -d PrinterQueue -U #{user} #{fileToBePrinted}")

Then out will be a string containing the standard output from lp, err will be a string containing the standard error, and status will be a Process::Status instance. You check status.success? to see if the lp command worked and look at err (or show err to the user) if it didn't work.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
0

The string request id is PrinterQueue-68 (1 file(s)) is what the lp command prints, not what it returns.

If the lp command fails, it will return a non-zero exit status. (It should also print an error message, but those messages aren't necessarily documented and might change from one version to the next.)

As I understand it, you can query the value of $? after invoking a command using backticks. If the command succeeds, $? should be 0. If it fails, it will have some non-zero value.

In a comment, @muistooshort suggests using open3; that's probably more robust and flexible than using backticks.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631