Fotolia_52415534_XS.jpg

What?

To leverage DB2 commands from a *nix environment, you typically append the following to your shell profile script:

1
. /path/to/db2/sqllib/db2profile

But, if you try that in zsh, you get the lovely errors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
zsh: awk: command not found...
AddRemoveString:12: command not found: awk
zsh: sed: command not found...
AddRemoveString:14: command not found: sed
zsh: sed: command not found...
AddRemoveString:18: command not found: sed
zsh: awk: command not found...
AddRemoveString:12: command not found: awk
zsh: sed: command not found...
AddRemoveString:14: command not found: sed
zsh: sed: command not found...
AddRemoveString:18: command not found: sed
zsh: awk: command not found...
AddRemoveString:12: command not found: awk
zsh: sed: command not found...
AddRemoveString:14: command not found: sed
zsh: sed: command not found...
AddRemoveString:18: command not found: sed
zsh: awk: command not found...
AddRemoveString:12: command not found: awk
zsh: sed: command not found...
AddRemoveString:14: command not found: sed

And worse, you completely lost everything in $PATH. Due to differences between the Bourne shell and the Z shell, the AddRemoveString function in db2profile splits your path variables in a way it can’t recombine.

So how can I fix it?

Luckily, there’s an easy fix I’ve come up with. First, I copy db2profile to db2profile.zsh, then towards the bottom of AddRemoveString I add a line that replaces the newlines in the path variable with the correct delimiter:

In the end, it looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
AddRemoveString()
{
if [ $# -ne 3 ]; then
return 0
fi
var=$1 #The enviornment variable to be processed
addrm_str=$2 #The new path to be used
action=$3 #Action: a -> add, r -> remove
if [ "X${action?}" != "Xa" -a "X${action?}" != "Xr" ]; then
return 0 # return without doing anything
fi
awkval='$1 != "'${addrm_str?}'"{print $0}'
newval=`eval echo \\${$var:-""} | awk '{for (i=1; i<= NF; ++i) \
if( $i != VAR && length($i) > 0 ) print $i":"}' FS=":" VAR=${addrm_str?}`
newval=`echo ${newval?} | sed 's/\: /\:/g'`
if [ "X${action?}" = "Xa" ]; then
newval=${newval?}"${addrm_str?}"
else
newval=`echo ${newval?} | sed 's/:$//'`
fi
+ newval=`echo ${newval?} | tr '\n' ':'`
eval $var=\"${newval?}\"
unset var addrm_str awkval newval
}

That line marked with a + is the line added (make sure to remove the + if you copy/paste).

Then, just update your .zshrc with:

1
. /path/to/db2/sqllib/db2profile.zsh

That’s it!