Home Explore Blog CI



man-pages

20th chunk of `less.man`
cd6d9c0f2fac75d27ae9cc23e29ccc51d4276c88f97e87e30000000100000faa
 postprocessor, which may perform any desired clean‐up action (such as deleting the replacement file  created  by  LESSOPEN).
       This program receives two command line arguments, the original filename as entered by the user, and the name of the replacement file.  To set up an input postprocessor, set the LESSCLOSE environment variable to a com‐
       mand  line which will invoke your input postprocessor.  It may include two occurrences of the string "%s"; the first is replaced with the original name of the file and the second with the name of the replacement file,
       which was output by LESSOPEN.

       For example, on many Unix systems, these two scripts will allow you to keep files in compressed format, but still let less view them directly:

       lessopen.sh:
            #! /bin/sh
            case "$1" in
            *.Z) TEMPFILE=$(mktemp)
                 uncompress -c $1  >$TEMPFILE  2>/dev/null
                 if [ -s $TEMPFILE ]; then
                      echo $TEMPFILE
                 else
                      rm -f $TEMPFILE
                 fi
                 ;;
            esac

       lessclose.sh:
            #! /bin/sh
            rm $2

       To use these scripts, put them both where they can be executed and set LESSOPEN="lessopen.sh %s", and LESSCLOSE="lessclose.sh %s %s".  More complex LESSOPEN and LESSCLOSE scripts may be written to accept  other  types
       of compressed files, and so on.

       It  is also possible to set up an input preprocessor to pipe the file data directly to less, rather than putting the data into a replacement file.  This avoids the need to decompress the entire file before starting to
       view it.  An input preprocessor that works this way is called an input pipe.  An input pipe, instead of writing the name of a replacement file on its standard output, writes the entire contents of the replacement file
       on its standard output.  If the input pipe does not write any characters on its standard output, then there is no replacement file and less uses the original file, as normal.  To use an  input  pipe,  make  the  first
       character  in  the LESSOPEN environment variable a vertical bar (|) to signify that the input preprocessor is an input pipe.  As with non‐pipe input preprocessors, the command string must contain one occurrence of %s,
       which is replaced with the filename of the input file.

       For example, on many Unix systems, this script will work like the previous example scripts:

       lesspipe.sh:
            #! /bin/sh
            case "$1" in
            *.Z) uncompress -c $1  2>/dev/null
                 ;;
            *)   exit 1
                 ;;
            esac
            exit $?

       To use this script, put it where it can be executed and set LESSOPEN="|lesspipe.sh %s".

       Note that a preprocessor cannot output an empty file, since that is interpreted as meaning there is no replacement, and the original file is used.  To avoid this, if LESSOPEN starts with two vertical  bars,  the  exit
       status  of  the script becomes meaningful.  If the exit status is zero, the output is considered to be replacement text, even if it is empty.  If the exit status is nonzero, any output is ignored and the original file
       is used.  For compatibility with previous versions of less, if LESSOPEN starts with only one vertical bar, the exit status of the preprocessor is ignored.

       When an input pipe is used, a LESSCLOSE postprocessor can be used, but it is usually not necessary since there is no replacement file to clean up.  In this case, the replacement file name passed to the LESSCLOSE post‐
       processor is "-".

       For compatibility with previous versions of less, the input preprocessor or pipe is not used if less is viewing standard input.  However, if the first character of LESSOPEN is a dash (-),  the  input  preprocessor  is
       used  on  standard  input as well as other files.  In this case, the dash is

Title: Less: Input Preprocessor Examples and Input Pipes
Summary
This section provides example scripts for `lessopen.sh` and `lessclose.sh` that allow viewing compressed files directly in `less`. It explains how to use these scripts by setting the `LESSOPEN` and `LESSCLOSE` environment variables. The section then introduces the concept of an "input pipe", where the preprocessor sends the file content directly to `less` instead of creating a replacement file. This is enabled by starting the `LESSOPEN` variable with a vertical bar (|). The `lesspipe.sh` script is presented as an example. Finally, it describes the behavior when `LESSOPEN` starts with two vertical bars (||), making the exit status of the script meaningful even for empty outputs. It also explains how `LESSCLOSE` behaves with input pipes and the usage of `LESSOPEN` with a leading dash (-) to process standard input.