Shell Programming

This page never seems to age, it just contains some idioms I like to use in my .login and/or .bashrc files. I don’t do this so often as when I started the page but the MAC keeps it in the house, and I have installed cygwin on nearly all my windows systems.

Aliases

alias whence="type -p "
export PS1="B$ "

Paths

 

contains()
{
# $1 must be set and grep must be in the PATH
    echo $1 | grep $2 > /dev/null
    code=$?
    return $code
}

for directory in /usr/local/bin /usr/java/bin
do
contains $PATH $directory
if [ $? ! = 0 ]
then
    PATH=$directory:$PATH
fi
done

Fashions change

for directory in /usr/local/bin /usr/java/bin;do
    contains $PATH $directory
    [ $? ! = 0 ] && PATH=$directory:$PATH
done

This can be done for any PATH like variable such as MANPATH & LD_LIBRARY_PATH. The code above doesn’t test for an empty variable because it is manipulating the $PATH, which is unlikely to be unset. Here is some code that performs the test. NB If we want to add to the MANPATH, we need to capture the default values, or do with the Cobalt Linux, since ti treats the man search path differently depending upon if $MANPATH is set or not.

if [ -z $MANPATH ]
then
# if MANPATH is unset get it
    MANPATH=`cat /etc/man.config | \
    awk '$1 == path {print $2;}' path=MANPATH | \
    tr '\n' ":"
fi

contains $MANPATH ${POSTGRES_HOME}/man
if [ $? != 0 ]
then
    MANPATH=${POSTGRES_HOME}/man
fi
export MANPATH

I should check that BASH implements the -z test in the same way as the Korn shell.

ksh

If I am going to use it

ENV=${HOME}/.kshrc
EDITOR=vi

File Scanner

A file scanner

#!/usr/bin/ksh
typeset -u input
while read  input
do
     print "$input"
     # if you like to add line to some file, then
     # print some >> somefile
     # single > is overwrite = file include only the lastline
done

Links

This was originally posted to davelevy.dyndns.info over 18 month ago.

Related Posts

  • RCS Revision Control System, one of the grand daddies of them all. I still use it because […]
  • File Transfer I need to revise file transfer over TCP/IP. This article consists of links on […]
  • Using “boomy” icons with tcl/tk I spent some time over the last few days trying to populate the TK images folder with a […]
  • Ubuntu 13.10 I am installing this on the Mac under Virtual Box. This snip is about Ubuntu. The […]
  • System V init scripts I really like Ubuntu's system V initialisation scripts and features. This is now based […]

3 thoughts on “Shell Programming

  1. hey there and thank you for your information ā€“ Iā€™ve definitely picked up something new from right here. I did however expertise some technical points using this site, since I experienced to reload the site many times previous to I could get it to load properly. I had been wondering if your web host is OK? Not that I am complaining, but sluggish loading instances times will often affect your placement in google and could damage your high quality score if advertising and marketing with Adwords. Well I am adding this RSS to my e-mail and could look out for much more of your respective interesting content. Make sure you update this again soon..

  2. My brother recommended I might like this web site. He was totally right. This post truly made my day. You can not imagine simply how much time I had spent for this info! Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *