2

In our university we have a script to setup the working environment under linux. Since I am lazy I decided to add this process to the shell initialization by adding source /setup/script to .cshrc. However since I have done so, I can't use less anymore. When I run less somefile, the output of said script is displayed by less instead of the contents of somefile.

I managed to work around the issue by using source /setup/script > /dev/null

So the questions:

  1. Is it wrong/is there a better way, then adding source initscript to .cshrc
  2. How can I continue using less (without the output redirect, the script echos some ussage information I find helpfull)?
  3. Why is this happening?

edit: OK since I feel uncomfortable adding the scripts, here is a minimal example that breaks less for me:

.cshrc:

source ~/test

~/test:

echo "test"

If I now do less test I get test instead of echo "test", by the way the output is the same for other files, I checked. The output is within the less environment (pressing q returns me to the console)

update: There is an additional annoying thing: less imaginaryPathToNonexistingFile- works, same output. less has some sort of memory: every time I change the file I open the display of test is one line down on the screen, until it is right over the filename(END)at the bottom of the console

update: I found that there is an environment variable called LESSOPEN set to |/usr/bin/lesspipe.sh %s (thx vonbrand)

#!/bin/sh -
#
# To use this filter with less, define LESSOPEN:
# export LESSOPEN="|/usr/bin/lesspipe.sh %s"

lesspipe() {
  case "$1" in
  *.[1-9n]|*.man|*.[1-9n].bz2|*.man.bz2|*.[1-9].gz|*.[1-9]x.gz|*.[1-9].man.gz)
    case "$1" in
        *.gz)   DECOMPRESSOR="gunzip -c" ;;
        *.bz2)  DECOMPRESSOR="bunzip2 -c" ;;
        *)  DECOMPRESSOR="cat" ;;
    esac
    if $DECOMPRESSOR -- "$1" | file - | grep -q troff; then
        if echo "$1" | grep -q ^/; then #absolute path
            man -- "$1" | cat -s
        else
            man -- "./$1" | cat -s
        fi
    else
        $DECOMPRESSOR -- "$1"
    fi ;;
  *.tar) tar tvvf "$1" ;;
  *.tgz|*.tar.gz|*.tar.[zZ]) tar tzvvf "$1" ;;
  *.tar.bz2|*.tbz2) bzip2 -dc -- "$1" | tar tvvf - ;;
  *.[zZ]|*.gz) gzip -dc -- "$1" ;;
  *.bz2) bzip2 -dc -- "$1" ;;
  *.zip) zipinfo -- "$1" ;;
  *.rpm) rpm -qpivl --changelog -- "$1" ;;
  *.cpi|*.cpio) cpio -itv < "$1" ;;
  *.gif|*.jpeg|*.jpg|*.pcd|*.png|*.tga|*.tiff|*.tif)
   if [ -x "`which identify`" ]; then
     identify "$1"
   else
     echo "No identify available"
     echo "Install ImageMagick to browse images"
   fi ;;
  *)
    case "$1" in
        *.gz)   DECOMPRESSOR="gunzip -c" ;;
        *.bz2)  DECOMPRESSOR="bunzip2 -c" ;;
    esac
    if [ ! -z $DECOMPRESSOR ] ; then
        $DECOMPRESSOR -- "$1" ;
    fi
  esac
}

if [ -d "$1" ] ; then
    /bin/ls -alF -- "$1"
else
    lesspipe "$1" 2> /dev/null
fi
Anthon
  • 79,293
ted
  • 623
  • 2
  • 6
  • 13
  • 2
    We can't know unless you show us the contents of the script. – terdon Mar 31 '13 at 14:24
  • I can't imagine how they manage to do that, I'd be very interested ;-) – vonbrand Mar 31 '13 at 14:44
  • @terdon: I added a min example, it is running under Redhat if that helps – ted Mar 31 '13 at 14:51
  • @vonbrand: From the fact, that the pros here think that it is actually related to script, and what I have found, I am afraid I have to disappoint you: either I have an old/broken/... version of less, or there is some bigger thing besides less doing the breaking – ted Mar 31 '13 at 14:52
  • 1
    I can't see anything that might have such a bizarre efect there. Anything in ~/.lessrc, any alias for less, setting any variable named LESS<something> somewhere? – vonbrand Mar 31 '13 at 15:00
  • @vonbrand: not that I can see, I have no ~/.lessrc. A lookarround in the enviornemntvariables yielded this: LESSOPEN=|/usr/bin/lesspipe.sh %s, I put this up top. – ted Mar 31 '13 at 15:30
  • However I don't see why my souricngtriggers this behaviour – ted Mar 31 '13 at 15:35

2 Answers2

2

I think your problem is addressed on this FAQ page for the less command (see section "Why are there problems if my .cshrc, .profile or ENV files produce output?"):

If you use the LESSOPEN feature, having your shell produce output can cause other problems. Since less uses the shell to invoke the LESSOPEN filter, the output from the shell will appear at the beginning of the output from the filter program, causing it to appear to be part of the file.

ted
  • 623
  • 2
  • 6
  • 13
  • 1
    Welcome to *nix.SE. Please paste the most relevant/important information from links in-line (e.g., in a quote block), to avoid link-rot. – HalosGhost Oct 02 '14 at 17:25
  • I have to accept this awnser unchecked since I no longer work on the relevant system or have access to it. I took the freedom to quote the link. Cheers – ted Oct 08 '14 at 17:59
1

I agree with @Tyler on why this is happening.

To answer your other two questions:

  1. Is it wrong/is there a better way, then adding source initscript to .cshrc
  2. How can I continue using less (without the output redirect, the script echos some ussage information I find helpfull)?

You most likely want to move the source /setup/script to your .login, as per the Startup and shutdown section of man tcsh:

Non-login shells read only /etc/csh.cshrc and ~/.tcshrc or ~/.cshrc on startup.

...

Commands like stty(1) and tset(1), which need be run only once per login, usually go in one’s ~/.login file.

less invokes the shell, but it's a non-login shell, and therefore does not produce any output from .login.

The caveat here is that something like screen won't execute the ~/.login file. That may or may not be important to you.

Randall
  • 445