More 2 Cent Tips & Tricks Issue 15

#### "Linux Gazette...making Linux just a little more fun! "

# More 2¢ Tips!

Send Linux Tips and Tricks to gazette@ssc.com

### Contents:

- Automatic Term Resizing
- Background Images
- Changing Directories
- Colorized Prompts
- Getting less to View gzipped Files
- Lowercased Filenames
- More on Xterm Tittlebar Tip
- A Quick & Dirty getmail Script
- Syslog 2c Tip Revised
- vi/ed Tricks and the .exrc File

### Automatic Term Resizing

Date: Mon, 17 Feb 1997 21:36:57 -0800 (PST)
From: pb@europa.com

Heya,
I spend a lot of time telnetting to my ISP from various sized terms under X and from the good ol' prompt. Typing "stty cols x rows y" got tedious, so I found a nice solution: Putting "eval `resize`" in my .cshrc. Now my remote terms automatically resize themselves to whatever convoluted geometry I've got locally.

Cheers,

Peat

### Background Images

Date: Tue, 18 Feb 1997 15:57:17 -0500
From: Christopher Fortin, cfortin@bbn.com

Hi.
I use fvwm2, and like to have four virtual screens, each with a different background. However, I found myself editing my .fvwm2rc file alot to change those backgrounds ( kept getting bored with the selection ). So I came up with a little tcl script to do the work for me. Now I just have a directory ( called .backgrounds ) filled with .xpm files that I like as backgrounds. On login, my .login file calls randBG.tcl, an executable tcl file thats in your path, ( if tclsh is not in /usr/bin, change the first line ).

    #---CUT HERE------randBG.tcl---------------------------
    #! /usr/bin/tclsh

    proc randomInit {seed} {
            global rand
            set rand(ia) 9301;      #multiplier
            set rand(ic) 49297;     #Constant
            set rand(im) 233280;    #Divisor
            set rand(seed) $seed;   #Last Result
    }

    proc random {} {
            global rand
            set rand(seed) \
                    [expr ($rand(seed)*$rand(ia) + \
                            $rand(ic)) % $rand(im)]
            return [expr $rand(seed)/double($rand(im))]
    }

    proc randomRange { range } {
            expr int([random]*$range)
    }

    randomInit [pid]
    random
    randomRange 100

    ### CHANGE THIS #####################
    set BGDIR /your.home.dir/.backgrounds
    #

    exec /bin/rm -f $BGDIR/desk1.xpm
    exec /bin/rm -f $BGDIR/desk2.xpm
    exec /bin/rm -f $BGDIR/desk3.xpm

    set files [ exec ls $BGDIR ]
    set nfiles [llength $files]

    set rnd1 [eval randomRange $nfiles]
    set rnd1file [lindex $files $rnd1]
    exec ln -s $BGDIR/$rnd1file $BGDIR/desk1.xpm

    set rnd2 [eval randomRange $nfiles]
    set rnd2file [lindex $files $rnd2]
    exec ln -s $BGDIR/$rnd2file $BGDIR/desk2.xpm

    set rnd3 [eval randomRange $nfiles]
    set rnd3file [lindex $files $rnd3]
    exec ln -s $BGDIR/$rnd3file $BGDIR/desk3.xpm
    #------------
    #-----CUT HERE-----------------------------------------

The rand part of this was from Welch's TCL book. Now you just need .fvwm2rc to use the ~/.backgrounds/desk?.xpm, like

    #----------------------------------------------
    ####
    # Set Up Backgrounds for different desktops.
    ####
    Module FvwmBacker

    *FvwmBackerDesk 0 xpmroot ./.backgrounds/desk0.xpm
    *FvwmBackerDesk 1 xpmroot ./.backgrounds/desk1.xpm
    *FvwmBackerDesk 2 xpmroot ./.backgrounds/desk2.xpm
    *FvwmBackerDesk 3 xpmroot ./.backgrounds/desk3.xpm
    #----------------------------------------------

and also

    #----------------------------------------------
    AddToFunc "InitFunction"    Desk "I" 0 0
    +               "I" Exec xpmroot ./.backgrounds/desk0.xpm &
    #----------------------------------------------

    to set desk0 prior to changing between desks. Just a little
    hack I thought someone might like. Note that this only changes
    desks 1-3, since I tend to keep desk0 constant ( I found a
    *really* nice background ).

Chris
-- Dr. Christopher S. Fortin

### Changing directories, A short enhancement to previous article's idea

Date: Thu, 20 Feb 1997 19:13:38 +0100
From: jurriaan, thunder7@xs4all.nl

In an article in the October Linux Journal (or was it Gazette - I don't know) by Marc Ewing (marc@redhat.com) a shell script was presented to allow a user to go to any directory on the system, without getting to all directories in between.

Much as this script apealed to me, it didn't work as I expected:

(A part of) my directory tree look like:

    /root
    /root/angband
    /root/angband/2796
    /root/angband/2796/src
    /root/angband/2796/lib
    /root/angband/2796/lib/edit
    /root/angband/2796/lib/data
    /root/angband/myang
    /root/angband/myang/src
    /root/angband/myang/lib
    /root/angband/myang/lib/edit
    /root/angband/myang/lib/data
    etc.

Now when I typed cds myang, it offered me a choice between all directories containing myang. Instead I'd much prefer if the program decided that the one directory ending in myang would be the most logical choice.

I adapted this script, and the result is included below. Many comments are added, which you may or may not like. They may not even be correct, as I am not one of the guru-est of linux-dom, as Marc Ewing was described :-).

If you like it, use (ie include) it and let me know please.

If you don't, adapt it and then include it and let me know please.

If you really don't like it, consider this message not written.

Greetings from Holland,
Jurriaan (thunder7@xs4all.nl)

    function cds() {
    #  no arguments? then do nothing
    	if [ $# -ne 1 ]; then
    		echo "usage: cds pattern"
    		return
    	fi

    # $1 seems to disappear later on, or change value, so we declare a real
    target
    	target=$1

    # find $target in file $HOME/.dirs
    	set "foo" `fgrep $target $HOME/.dirs`

    # $# is the function return status, 1 means not found
    	if [ $# -eq 1 ]; then
    		echo "No matches"

    # 2 means just one found
    	elif [ $# -eq 2 ]; then
    		cd $2

    # we found a couple of possible directories
    	else

    # $ is the sign for end-of-line , -E tells fgrep to use extended regular
    # expressions
    # the \ before $ tells the shell not to see $ as an empty variable, but to
    # pass it right on to fgrep
    # if you are ever in doubt, use set -x to see what goes on in your scripts.
    # then use set +x to get rid of all the extra output
    		set "foo" `fgrep -E $target\$ $HOME/.dirs`

    # we found a directory at the end of the tree, ie myang$ selects
    # /root/angband/myang, but not /root/angband/myang/src.
    		if [ $# -eq 2 ]; then
    			cd $2

    # I'm not sure - in DOS you must reset your variables, in Linux too?
    			target=
    			return
    		else

    # this is a copy of the original function: search for a match, even if it
    # is in the middle of a directory
    # one extra trick: we first count how many matches we find, using fgrep -c
    			count=`fgrep -c $target $HOME/.dirs`

    # stty size gives on my terminal 51 116 (ie a 116x51 screen)
    # cut -b1-3 gives then 51
    			lines=`stty size | cut -b1-3`

    # if more than 2/3 of the terminal, it's too much
    			lines=$[$lines*2/3]
    			if [ $count -gt $lines ]; then
    				echo "More than $lines matches - respecify please"
    				count=
    				lines=
    				target=
    				return
    			fi

    # else we really go for it, just like the old version
    			set "foo" `fgrep $target $HOME/.dirs`
    			shift
    			for x in $@; do
    				echo $x
    			done | nl -n ln
    			echo -n "Number: "
    			read C
    			if [ "$C" = "0" -o -z "$C" ]; then
    				return
    			fi
    			eval D="\${$C}"
    			if [ -n "$D" ]; then
    				#echo $D
    				cd $D
    			fi
    		fi
    	fi;
    }

### Colorized Prompts

Date: Mon, 24 Feb 1997 12:03:57
From: arnim@rupp.de

    #!/bin/sh

    # script for colorized prompts, by arnim@rupp.de

    # start this script to see all possible colors then
    # include this ...
    # ------------------------- snip ------------------------

    BLACK='^[[30m'
    RED='^[[31m'
    GREEN='^[[32m'
    YELLOW='^[[33m'
    BLUE='^[[34m'
    MAGNETA='^[[35m'
    CYAN='^[[36m'
    WHITE='^[[37m'

    BRIGHT='^[[01m'
    NORMAL='^[[0m'

    # blink ;-)
    BLINK='^[[05m'
    REVERSE='^[[07m'

    # sample bash-prompt
    PS1=$BRIGHT$YELLOW'\u:'$NORMAL'/\t\w\$ '

    # ------------------------- snip ------------------------
    # .. in Your /etc/profile, .profile, .bashrc, .whatever, ...
    # ( don't cut & paste with the mouse, this would spoil the escape-characters )

    echo $BLACK   'BLACK'
    echo $RED     'RED'
    echo $GREEN   'GREEN'
    echo $YELLOW  'YELLOW'
    echo $BLUE    'BLUE'
    echo $MAGNETA 'MAGNETA'
    echo $CYAN    'CYAN'
    echo $WHITE   'WHITE'

    echo $BRIGHT$BLACK   'BRIGHT BLACK'
    echo $BRIGHT$RED     'BRIGHT RED'
    echo $BRIGHT$GREEN   'BRIGHT GREEN'
    echo $BRIGHT$YELLOW  'BRIGHT YELLOW'
    echo $BRIGHT$BLUE    'BRIGHT BLUE'
    echo $BRIGHT$MAGNETA 'BRIGHT MAGNETA'
    echo $BRIGHT$CYAN    'BRIGHT CYAN'
    echo $BRIGHT$WHITE   'BRIGHT WHITE'

    echo $NORMAL

### Getting less to View gzipped Files

Date: Fri, 7 Feb 1997 11:21:41 -0800 (PST)
From: Michael Bain, michael.bain@boeing.com

Here's how to use less to view gzipped files. Also, there is a way you can use this less feature that doesn't require temporary files and only needs one script file.

Put lesspipe.sh in your executable path.

lesspipe.sh:

    #! /bin/sh
    case "$1" in
         *.Z) uncompress -c $1  2>/dev/null
         ;;
         *.gz) gunzip -c $1  2>/dev/null
         ;;
    esac

Set the environmental variable LESSOPEN='|lesspipe.sh %s'. (Don't forget the pipe '|' symbol.) This works with less version 2.90.

Michael Bain

### Lowercased Filenames

Date: Thu, 20 Feb 1997 00:38:10 GMT
From: bubje@freemail.nl

Hello there
We've all read all those ways to convert uppercased filenames to lowercased ones. But why did we need it? One reason is because when we unzip a file, all filenames are uppercase. Well, try this (much much shorter :) )

    unzip -L filename.zip

This extracts the files as usual, but converts the filenames to lowercase, so there's no need to run any of those other two cent tips anymore... (and it's less to type, and faster)

Greatz
Jan Gyselinck, wodan@cryogen.com

### More on Xterm Titlebar Tip

Date: Tue, 11 Feb 1997 12:33:18 -0500
From: Raul D. Miller, rdr@tad.micro.umn.edu

I don't know if you've touched on this yet -- if so, please ignore this message.

With bash, you can reliably set the titlebar. Just set the PROMPT_COMMAND variable to be a command that sets your title bar.

Aside: I usually use the shortened host name, with a # suffix if I'm root. The most portable way of testing if I'm root is [ -w / ]

Raul

### A Quick and Dirty getmail Script

Date: Sat, 15 Feb 1997 12:45:59 +0200 (GMT+0200)
From: Markku J. Salama, msalama@hit.fi

Hi there!

Here is a quick and dirty script for fetching your mail without a POP account. It does it's thing by using telnet and ftp.

    --------------------------------BEGIN SCRIPT------------------------------

    #!/bin/sh
    # Brought to you by msalama@superfly.salama.fi
    # Caveat emptor: You use this entirely at your own risk, I'm not
    # responsible for any damages or loss of mail it might cause.

    # There are 3 things to remember:

    # 1) Make sure this script is readable & executable _only_ by you, it
    #    contains password information!

    # 2) You must have a .netrc-file in your home directory containing a
    #    hostname, your username and your passwd for ftp. Make sure this file
    #    is readable _only_ by you, too, and check the ftp man page for
    #    details.

    # 3) You must, of course, edit this script to provide all the necessary
    #    passwords, usernames etc. for telnet. Also, the remote system must
    #    have dd installed to empty the mailbox.

    (echo open your.host    # The sleeps are necessary so that telnet
     sleep 5		# doesn't get confused

     echo your.username
     sleep 5

     echo your.password	# For your eyes only...
     sleep 10		# 10 sec. break, let the motd etc. scroll by

     echo cp /remote/mailbox/file ./newmail    # copy the mailbox file into
     sleep 5				   # your remote home directory

     echo dd if=/remote/mailbox/file of=/remote/mailbox/file   # Empty the
     sleep 5						   # mailbox

     echo quit) | telnet -8E > /dev/null

    (echo binary				   # Now go get the mail using
     echo get newmail			   # ftp. Handy for those folks
     echo delete newmail			   # who don't have a POP account.
     echo bye) | ftp your.host > /dev/null

     mv ./newmail /local/mailbox/file	   # Move the new mail in place...

     chmod go-rwx /local/mailbox/file	   # Just in case it's readable
    				 	   # by someone else.
     # All done! Go read them.

    --------------------------------END SCRIPT--------------------------------

There. Have a nice spring & be an excellent person.

Markku Salama

### Syslog 2c Tip Revised

Date: Sun, 9 Feb 1997 23:26:46 -0800 (PST)
From: Ian Main, imain@vcc.bc.ca

Hi, just going through issue #14 of the linux gazzette, and I noticed the tip on logging *.* to a file so you can read it in an rxvt in X. I do a similar thing here, but rather than logging to a file, I log to a pipe (ah ha! Why didn't I think of that? :-) ).

Works really well. No disk space used, and you can just use cat to view it, and it scrolls along nicely.

To make a named pipe (FIFO) in /var/log/message-pipe:

    mknod /var/log/message-pipe p

and add this to your /etc/syslog.conf (note the pipe symbol there.) :

    *.*             |/var/log/message-pipe

and finally, just type:

    cat /var/log/message-pipe

Or of course.. you can stick it in a shells script or as the command rxvt runs when it starts.. whatever you like.

Hope you find it useful,

Ian

### vi/ed Tricks and the .exrc File

Date: Tue, 11 Feb 1997 16:28:30 -0600 (CST)
From: Sean Murray, murrsea@ripco.com

The vi editor is built on the foundations of the "ed" editor. Whatever applies to ed applies to vi. So if you where wondering if there was a way to customize your vi sessions wonder no longer.

In your home directory create a file called ".exrc", every time vi starts it will parse that file and customize it's actions. The below 5 lines are the contents of my .exrc file.

    set tabstop=8
    map ^N {!}sort^M
    map v {^M!}fmt^M
    map V 1G^M!Gfmt^M
    map ^W :!ispell %^M^M:e!^M

I didn't include any comments because I don't know if the .exrc file has a comment character, I'll comment theses lines later?

Ok the "set" command allows you to set various parameters in vi; in this case I've set the tab stop to 8 characters. So when ever I enter a tabstop in insertion mode the cursor will move over 8 spaces (8 spaces is what most printers will print tabs at regardless of your vi settings). But you can set it to what ever you like.

Sometimes when programming I manually set my tabstop to 4 spaces for indentation. To do this type in the following ":set tabstop=4". The nice thing about this is that the character is still really a tab and not a bunch of spaces, hence you don't force other ppl to view text with your spacing.

"map" maps a key or key combination to a sequence of commands. Note: that only ed commands work here so see view a list of ed commands while editing your .exrc file. It's a BAD idea to map key or key combinations that already have other meanings. The available combinations are:

    	letters:        "g K k q V v"
    	Control keys:   "^A ^K ^O ^T ^W ^X"
    	(where "^A" means press the control key and the letter a)
    	Symbols:        "_ * \ ="

(These above four lines where shamelessly stolen from ORA's _Learning the Vi Editor_; it's a must get for any vi user)

So what does "map ^W :!ispell %^M^M:e!^M" do -- well the "map" is the keyword telling vi to map the next character to the following commands. (If you map a key combination like ^W then remember to enter this by typing the control key and "v" first and then the key combination of control key and the letter "w".) Here we are mapping ^W to a set of commands. The first command is telling vi to execute the external program ispell with the current file we are editing (the variable that holds the current files name is "%"). The ^M is actually the character that appears after you have typed ^V and then typed the return key hence ^M denotes the instance of a carriage return. The last command is the vi command to reload the current file; this is necessary as the ispell program will update the file and not the vi buffer.

assuming that you have the external programs "ispell", "fmt" and "sort" the theses mappings should work. "map ^N {!}sort^M" will sort a paragraph. "map v {^M!}fmt^M" will format a paragraph. "map V 1G^M!Gfmt^M" will format the whole document.

A final note: if you have the environment variable EXINIT set it will take precedence over the .exrc file settings.

Sean Murray

Published in Linux Gazette Issue 15, March 1997

##### This page maintained by the Editor of Linux Gazette, gazette@ssc.com
Copyright © 1997 Specialized Systems Consultants, Inc.