CUPS 的 lp 返回什么?
我正在编写一个通过调用进行打印的 ruby 脚本:
`/usr/bin/lp -d PrinterQueue -U #{user} #{fileToBePrinted}`
我想优雅地处理打印错误,但无法确定执行它时 lp 返回什么。通常它是这样的字符串:
请求 ID 为 PrinterQueue-68(1 个文件)
是否有任何地方描述了在奇怪情况下 lp
应该返回什么?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请允许我详细说明一下我的评论。
您应该忘记为此使用反引号并直接转到
Open3
。特别是Open3.capture3< /code>
:
那么
out
将是一个包含lp
的标准输出的字符串,err
将是一个包含标准输出的字符串错误,以及status
将是Process::Status
实例。您检查
status.success?
以查看lp
命令是否有效,并查看err
(或显示err
以用户)如果它不起作用。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
:Then
out
will be a string containing the standard output fromlp
,err
will be a string containing the standard error, andstatus
will be aProcess::Status
instance. You checkstatus.success?
to see if thelp
command worked and look aterr
(or showerr
to the user) if it didn't work.字符串
request id is PrinterQueue-68 (1 file(s))
是lp
命令打印的内容,而不是返回的内容。如果 lp 命令失败,它将返回非零退出状态。 (它还应该打印一条错误消息,但这些消息不一定有记录,并且可能会从一个版本更改为下一个版本。)
据我了解,您可以在调用后查询
$?
的值使用反引号的命令。如果命令成功,$?
应该为 0。如果失败,它将有一些非零值。在评论中,@muistooshort 建议使用
open3
;这可能比使用反引号更强大、更灵活。The string
request id is PrinterQueue-68 (1 file(s))
is what thelp
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.