如何列出可以与 lpr 一起使用的打印机名称?

Ian*_*non 76 linux bash printing lpr ubuntu

lpr男子说:页目标打印机可以与指定-P标志。

-P destination[/instance]
    Prints files to the named printer.
Run Code Online (Sandbox Code Playgroud)

我使用 Ubuntu/Gnome 中的 GUI 在本地 Samba 共享上“添加”了各种打印机。如何以-P标志期望的格式(最好从 bash shell)获取这些可用打印机的列表?

Kev*_*nko 116

$ lpstat -p -d
Run Code Online (Sandbox Code Playgroud)

来自CUPS 手册

-p选项指定您要查看打印机列表,该-d选项报告当前的默认打印机或类别。

  • 每当我寻找这个时,我也在寻找如何选择默认打印机:`lpoptions -d printername` (8认同)
  • 谢谢!我刚刚发现 `lpq` 也有效。 (4认同)

Gus*_*ves 17

要获取列表,您可以使用:

lpstat -a
Run Code Online (Sandbox Code Playgroud)

或者

cat /etc/printcap
Run Code Online (Sandbox Code Playgroud)

仅打印打印机名称:

lpstat + 读取 + 数组:

$ while read l; do l=($l); echo "${l[0]}"; done <<< "$(lpstat -a)"
Run Code Online (Sandbox Code Playgroud)

lpstat + awk:

$ lpstat -a | awk '{print $1}'
Run Code Online (Sandbox Code Playgroud)

lpstat + 剪切:

$ lpstat -a | cut -f1 -d ' '
Run Code Online (Sandbox Code Playgroud)

cat + grep + 切入/etc/printcap

$ cat /etc/printcap | cut -f1 -d'|' | grep '^#' -v
Run Code Online (Sandbox Code Playgroud)

这是显示的内容,每行一个:

HP_LaserJet_P1606dn
HP_Deskjet_2540_series
HP_LaserJet_M1212nf
GCP-Save_to_Google_Docs
Run Code Online (Sandbox Code Playgroud)

我觉得这些lpstat解决方案更加优雅和可靠。主要是因为/etc/printcap在我测试的某些系统上找不到。

关于使用awkor cut,取决于您已安装和喜欢的内容。bash read + bash array 选项应该可以在任何 bash shell 上运行,而无需外部组件。

编辑:我说标记的解决方案在 Amazon Linux 上对我不起作用。但是我想如果您只想从输出的其余部分中间复制打印机名称,它会起作用。与仅使用lpstat -a.

$ lpstat -p -d
printer HP_Deskjet_2540_series is idle. enabled since Tue 22 Dec 2015 01:12:10 PM BRST
. . .
printer GCP-Save_to_Google_Docs is idle. enabled since Tue 15 Dec 2015 02:13:33 AM BRST
system default destination: HP_LaserJet_P1606dn
Run Code Online (Sandbox Code Playgroud)