Unix Shell - ksh (88/93), setting of PATH in ~/.profile

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > April 2004 > ksh (88/93), setting of PATH in ~/.profile





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author ksh (88/93), setting of PATH in ~/.profile
ssvedin@passagen.se

2004-04-28, 6:34 pm

I've looked around for some weeks trying to find a way to solve my
problem.
I want to add paths (=values) to PATH and other environment variables.
I don't want the same value to be repeated in the EV. I want to be
able to add
one digit values (e.g. ".") as wéll as longer strings.
"." may be translated to "::" (null value) as ":" is used as
delimiter.

My aproach is a simple function defined within the script or maybe
separate in its own file, placed in a directory pointed to by both
¨$PATH and $FPATH.
The old way didn't work with "." as argument.
The new way don't work at all. (bash:
PATH=$PATH:/export/home/ssvedin/bin: A file or directory in the path
name does not exist.)
Target environment is IBM running AIX 5.2, ksh as well as ksh93 is
available.

Analyzing the errors makes me think that bash is used as shell
allthough ksh is specified in the shabang. (Bash is my default login
shell, but this script should be run for other users who have ksh as
shell.) It also seems that bash, or whatever shell is executing,
treats the statement as a file to be launched???

old way:
postfixpath()
{
if [ $( /bin/expr "$PATH" : ".*:*$1" ) -eq 0 ]
then
PATH=$PATH:/$1
fi
}

a slightly modified way (two params with the second defaulting to
PATH):
# $1 - path, e.g. ~/bin
# $2 - EV, e.g. MANPATH, dflt=PATH
postfixpath()
{
endix=$( /bin/expr "\$${2:-PATH}" : ".*:*$1" )
echo "endix=$endix"
if [ $endix -eq 0 ]
then
${2:-PATH}=\$${2:-PATH}:$1 #> /dev/null 2>&1
fi
}

the new one:
taken from linux (Mandrake 9.2):
Sorry, don't have it available at this computer for the moment!


A call to add /usr/bin to PATH looks like:

postfixpath /usr/bin


Any ideas would be appreciated!
Staffan Svedin
Icarus Sparry

2004-04-29, 8:34 pm

On 2004-04-28, ssvedin@passagen.se <ssvedin@passagen.se> wrote:
> I've looked around for some weeks trying to find a way to solve my
> problem.
> I want to add paths (=values) to PATH and other environment variables.
> I don't want the same value to be repeated in the EV. I want to be
> able to add
> one digit values (e.g. ".") as wéll as longer strings.
> "." may be translated to "::" (null value) as ":" is used as
> delimiter.
>
> My aproach is a simple function defined within the script or maybe
> separate in its own file, placed in a directory pointed to by both
> ¨$PATH and $FPATH.
> The old way didn't work with "." as argument.
> The new way don't work at all. (bash:
> PATH=$PATH:/export/home/ssvedin/bin: A file or directory in the path
> name does not exist.)
> Target environment is IBM running AIX 5.2, ksh as well as ksh93 is
> available.


It is rather more efficient to use the builtin pattern matching
available in the shells. Tested with on a debian linux system with
dash (0.2.45)
bash (2.05b-2-14)
ksh93 (Version M 1993-12-28 o+)
zsh-beta (4.2.0-dev-1+20040423-1)

postfixpath() {
# Append a directory specified by the first parameter
# to the environment variable specified by the second parameter
# but only if it is not already present. The second parameter
# defaults to PATH. Entries are seperated by : characters
# Only compares by name, and does not protect against
# /usr/bin/., /usr/./bin, /usr//bin, etc
# Icarus Sparry - 29 Apr 2004
target=${2:-PATH}
case "$1" in
"") d="." ;;
*:*) echo "Can not add a directory with a : to $target" >&2
return 1 ;;
*) d="$1" ;;
esac
if [ -d "$d" ] ; then
e='case "$'$target'" in
"$d"|"$d":*|*:"$d"|*:"$d":*) ;;
"") '$target'="$d" ;;
*) '$target'="$'$target':$d" ;;
esac'
eval "$e"
fi
}
ssvedin@passagen.se

2004-04-30, 4:33 am

Thanks for the idea Icarus, I will try it out on the AIX ksh next week.
//Staffan

Icarus Sparry <usenet@icarus.freeuk.com> wrote in message news:<slrnc935kf.re.usenet@icarus.freeuk.com>...
> On 2004-04-28, ssvedin@passagen.se <ssvedin@passagen.se> wrote:
>
> It is rather more efficient to use the builtin pattern matching
> available in the shells. Tested with on a debian linux system with
> dash (0.2.45)
> bash (2.05b-2-14)
> ksh93 (Version M 1993-12-28 o+)
> zsh-beta (4.2.0-dev-1+20040423-1)
>
> postfixpath() {
> # Append a directory specified by the first parameter
> # to the environment variable specified by the second parameter
> # but only if it is not already present. The second parameter
> # defaults to PATH. Entries are seperated by : characters
> # Only compares by name, and does not protect against
> # /usr/bin/., /usr/./bin, /usr//bin, etc
> # Icarus Sparry - 29 Apr 2004
> target=${2:-PATH}
> case "$1" in
> "") d="." ;;
> *:*) echo "Can not add a directory with a : to $target" >&2
> return 1 ;;
> *) d="$1" ;;
> esac
> if [ -d "$d" ] ; then
> e='case "$'$target'" in
> "$d"|"$d":*|*:"$d"|*:"$d":*) ;;
> "") '$target'="$d" ;;
> *) '$target'="$'$target':$d" ;;
> esac'
> eval "$e"
> fi
> }

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com