Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Friday, December 27, 2019

lp - the Linux printing command



https://www.networkworld.com/article/3373502/printing-from-the-linux-command-line.html
The lpstat -p command displays the status of a printer while lpstat -p -d also lists available printers.
With the -n option, the lp command allows you to specify the number of copies of a printout you  want.
$ lp -n 11 agenda
To cancel a print job, you can use the cancel or lprm command. If you don't act quickly, you might see this:
$ cancel 229
cancel: cancel-job failed: Job #229 is already completed - can't cancel.
To print in landscape mode, you would use the landscape option with the lp command.
$ lp -o landscape penguin.jpg


https://www.cups.org/doc/man-lp.html
-o media=size
Sets the page size to size. Most printers support at least the size names "a4", "letter", and "legal".
-o number-up={2|4|6|9|16}
Prints 2, 4, 6, 9, or 16 document (input) pages on each output page.
-o orientation-requested=4
Prints the job in landscape (rotated 90 degrees counter-clockwise).
-o orientation-requested=5
Prints the job in landscape (rotated 90 degrees clockwise).
-o orientation-requested=6
Prints the job in reverse portrait (rotated 180 degrees).
-o print-quality=3
-o print-quality=4
-o print-quality=5
Specifies the output quality - draft (3), normal (4), or best (5).
-o sides=one-sided
Prints on one side of the paper.
-o sides=two-sided-long-edge
Prints on both sides of the paper for portrait output.
-o sides=two-sided-short-edge
Prints on both sides of the paper for landscape output.




-t "name"
Sets the job name.
-H hh:mm
-H hold
-H immediate
-H restart
-H resume
Specifies when the job should be printed. A value of immediate will print the file immediately, a value of hold will hold the job indefinitely, and a UTC time value (HH:MM) will hold the job until the specified UTC (not local) time. Use a value of resume with the -i option to resume a held job. Use a value of restart with the -i option to restart a completed job.

To print from command line we need, first of all, to know which printers are available for our system and how are named. To do that, we use following command:
$ lpstat -a


https://www.computerhope.com/unix/ulpstat.htm
-a [printer(s)]Shows the accepting state of printer queues. If no printers are specified then all printers are listed.
-lShows a long listing of printers, classes, or jobs.


lp -d <printername> -o ColorModel=CMYK <filename.extension>
Set your printer default to color
sudo lpoptions -p the_printer -o <name=value>

Example: To make banana-color print in color by default on your computer:

sudo lpoptions -p the_printer -o ColorModel=CMYK

To change back to black and white:

sudo lpoptions -p the_printer -o ColorModel=Gray

Note Option commands vary by printer and model. To see available options for a specific printer:

lpoptions -p printername -l

https://unix.stackexchange.com/questions/68830/how-to-force-lp-from-cups-to-print-in-grayscale/68844#68844
Maybe you are looking for lpoptions ?
Use lpoptions -l in order to know what's the name for your printer.
You can play it like thi (example with an HP Photosmart) :
lp -o scaling=//100// -oColorModel=KGray image.jpg

lpoptions -d the_printer -l | grep -i color

https://www.math.wisc.edu/wiki/index.php/Examples
        lp -d P file.pdf  (send file to printer P - e.g. P=5)
        lp -o sides=one-sided file.pdf  (to each page on one side only)
        lp -o ColorModel=color file.pdf    (print in color)
        lp -o sides=two-sided-short-edge  file.pdf  (duplex along short edge instead of long)
        lp -o page-ranges=1  (print just page 1)
        lp -o page-ranges=1-4,7,9-12

You can combine these commands, for example:
        lp -o sides=two-sided-short-edge -o ColorModel=color file.pdf  (useful for printing tri-fold brochures)
lpoptions
Use the lpoptions command to save these options for later use. For example, you might want to do color printing to the printer on another floor, reserving the printer on your own floor for the normal grayscale prints. This command will ensure that anything you send to the 5th floor printer will be printed in color:
        lpoptions -d 5  ColorModel=color
your lpoptions commands are saved in the file ~/.cups/lpoptions. Typing lpoptions by itself will show you what your current printer setup is.
Checking the queue and cancelling jobs
Use the lpstat command to check the status of a queue and cancel to cancel jobs
       lpstat -d5 -t   (checks jobs on queue 5)
       cancel  5-241   (deletes job 5-241 from queue 5)
You can also cancel jobs using the printer icon which will appear in the top right hand corner of your screen when you start your job. To do this, open the printer icon, right click on your job and select cancel (not delete).

lpoptions -d the_printer -o ColorModel=color

https://stackoverflow.com/questions/33977512/how-to-send-send-cups-a-print-only-in-black-ink-command
- not work
The setting to print color as grey (from the ppd) is valid an is processed.
HPPJLColorAsGray/Print Color as Gray: False HighQuality * BlackInkOnly 
But the settings from the actual printout "overwrite" the default values (what makes sense to me) and in case color is in an document an this is printed with default values -- the colour ink is used. This behaviour can be controlled in the print dialogue generally. E.g. in libreoffice you can use an option "print text in black ink" this would result in real black text (not mixed as black from colors).
In LaTeX you can use \usepackage[monochrome]{xcolor} to force all text to black an so the use of the black ink.
Im my case I wanted to directly print to the print queue even though the default was set in the ppd to use BlackInkOnly it was not the case. After I added this option additionally as parameter to my print command it worked as I want it to be: color is printed in gray and only the black ink is used.
lpr  -P<printqueue> -o HPPJLColorAsGray=BlackInkOnly ...


https://docs.oracle.com/cd/E19082-01/819-7761/gfjex/index.html
  1. Set the system's default printer.

    # lpadmin -d [printer-name]
    -d printer-name specifies the name of the printer you are assigning as the system's default printer. If you don't specify printer-name, the system is set up with no default printer.
  2. Check the system's default printer.

    # lpstat -d
https://docs.oracle.com/cd/E19455-01/805-7229/printadmin-55845/index.html
# lpadmin -d [printer-name]

-d printer-nameName of the printer you are assigning as the system's default printer. If you don't specify printer-name, the system is set up with no default printer.

https://alvinalexander.com/unix/edu/examples/lp.shtml
This command prints the ".profile" file to the printer named "Sales". The -d option specifies the destination.
lp -dSales file1 file2 file3
This command prints the three files "file1", "file2", and "file3" to the printer named "Sales".
lp -i Sales-101 -H hold
This command places the print request Sales-101 on hold.
lp -i Sales-101 -H resume
This command resumes the print request Sales-101. The print request starts printing from page one unless you instruct it otherwise.
lp -i Sales-101 -H resume -P 4-
This command resumes the print request Sales-101, starting with page 4 of the print job. Note that the final hyphen (following the "4") is required.
The "lp" command can also be used as part of a pipeline. For instance, the following command will print the output of the "ps -ef" command to the default printer:
ps -ef | lp
https://www.jdyoung.com/2018/01/12/youre-going-print-print-efficiently/
Save Ink– Use grayscale as the default print setting and only switch to full color on the final version or for client-facing documents.
Fonts Matter– In 2014, a teenager discovered the government could save more than $200 million a year by using Garamond instead of Times New Roman when printing documents. You probably won’t save that much, but using thinner and lighter fonts like Garamond or Evergreen can save on ink.
Start Selectively Printing

Pull, not push
Many enterprise printing systems now offer a 'pull-printing' option. Instead of sitting at your desk and sending documents to a remote printer, pull-printing requires the user to manually enter a security code on the printer itself. This provides greater security, reduces uncollected paper left in the printer - and might even make people think twice before pushing the 'Print' button.



Wednesday, August 28, 2019

Autocomplete in bash



https://www.tldp.org/LDP/abs/html/tabexpansion.html
 and compgen builtins make it possible for tab completion to recognize partial parameters and options to commands. In a very simple case, we can use complete from the command-line to specify a short list of acceptable parameters.
bash$ touch sample_command
bash$ touch file1.txt file2.txt file2.doc file30.txt file4.zzz
bash$ chmod +x sample_command
bash$ complete -f -X '!*.txt' sample_command


bash$ ./sample[Tab][Tab]
sample_command
file1.txt   file2.txt   file30.txt
  
The -f option to complete specifies filenames, and -X the filter pattern.
For anything more complex, we could write a script that specifies a list of acceptable command-line parameters. The compgen builtin expands a list of arguments to generate completion matches.
Let us take a modified version of the UseGetOpt.sh script as an example command. This script accepts a number of command-line parameters, preceded by either a single or double dash. And here is the corresponding completion script, by convention given a filename corresponding to its associated command.
Example J-1. Completion script for UseGetOpt.sh
# file: UseGetOpt-2
# UseGetOpt-2.sh parameter-completion

_UseGetOpt-2 ()   #  By convention, the function name
{                 #+ starts with an underscore.
  local cur
  # Pointer to current completion word.
  # By convention, it's named "cur" but this isn't strictly necessary.

  COMPREPLY=()   # Array variable storing the possible completions.
  cur=${COMP_WORDS[COMP_CWORD]}

  case "$cur" in
    -*)
    COMPREPLY=( $( compgen -W '-a -d -f -l -t -h --aoption --debug \
                               --file --log --test --help --' -- $cur ) );;
#   Generate the completion matches and load them into $COMPREPLY array.
#   xx) May add more cases here.
#   yy)
#   zz)
  esac

  return 0
}

complete -F _UseGetOpt-2 -o filenames ./UseGetOpt-2.sh
#        ^^ ^^^^^^^^^^^^  Invokes the function _UseGetOpt-2.
https://stuff-things.net/2016/05/11/bash-autocompletion/
The command that setups autocompletion is complete. When giving it a list, pass them in as an augument to the -Woption:
1
complete -W "$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e s/,.*//g | uniq | grep -v "\["`;)" wait-for-host
I’m capturing the output of the command with $() and wrapping it in double quotes. The last argument is the name of the command (which can be also be a function or alias) that will use this autocompletion. Now we get this:
1
2
3
~ wait-for-host www<TAB>
www.example.com            www1.example.com
~ wait-for-host www
That works fine, but it’s static and applies only to one command. Instead you can create a reusable function.
1
2
3
4
5
6
7
8
9
_known_hosts() {
    local know_hosts cur
    known_hosts=$(cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e s/,.*//g | uniq | grep -v "\[")

    cur="${COMP_WORDS[COMP_CWORD]}"

    COMPREPLY=( $(compgen -W "$known_hosts" -- ${cur}) )
    return 0
}
BASH auto completion functions are powerful things, but for today we’re keeping it simple. First we build a list of options as before and store it in $known_hosts. Second we get the current word, the command argument, which is be tab completed. Finally, we pass that list and that word into compgen which is BASH’s internal matcher. compgen has some powerful features, but in this case it’s just going to return a list of hosts in $known_hosts that start with the word we hit tab on.
Then we tell the completion that we are using a function by giving it the -f option, along with the function name, instead of -W:
1
complete -F _known_hosts wait-for-host
And you can use the _known_hosts function for other commands as well:
1
complete -F _known_hosts ssh
This is a good exercise in understanding autocompletion, but it’s pretty basic. Fortunately, people have already done all of the hard work in the bash-completion project. This ships by default with many Linux distros. On the Mac:
1
brew install bash-completion
The add:
1
2
3
if [ -f $(brew --prefix)/etc/bash_completion ]; then
    . $(brew --prefix)/etc/bash_completion
fi
https://sanctum.geek.nz/arabesque/bash-hostname-completion/
For example, given the following hosts(5) file in place at /etc/hosts:
127.0.0.1      localhost
192.0.2.1      web.example.com www
198.51.100.10  mail.example.com mx
203.0.113.52   radius.example.com rad
An appropriate call to compgen would yield this output:
$ compgen -A hostname

We could then use this to complete hostnames for network diagnostic tools like ping(8):
$ complete -A hostname ping
There’s a simple way to make host completion much more useful by defining the HOSTFILE variable in ~/.bashrc to point to any other file containing a list of hostnames. You could, for example, create a simple file ~/.hosts in your home directory, and then include this in your ~/.bashrc:
# Use a private mock hosts(5) file for completion
HOSTFILE=$HOME/.hosts
This setup allows you to set hostname completion as the default method for all sorts of network-poking tools, falling back on the usual filename completion if nothing matches with -o default:
$ complete -A hostname -o default curl dig host netcat ping telnet

https://iridakos.com/tutorials/2018/03/01/bash-programmable-completion-tutorial.html

Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts