1

Issue:

I have a file named: env.sh which contains some environment variables and i want to initialize this file at the time of login.

I'm using bash, and after checking on various website, si tried to include this file in my ~/.bash_profile as well as in ~/.bashrc:

if [ -f ~/env.sh ]; then
    . ~/env.sh
fi

it's not working after i logout and again login to the system.

Below is the content of env.sh:

#!/bin/ksh
#####################################################################
# General environment variable settings                             #
#####################################################################

#ulimit -c unlimited
#umask 000

export HOSTNAME=$(/bin/hostname)
export DOMAINNAME=$(egrep ^'(domain|search)' /etc/resolv.conf | awk '{print $2}' | head -1)
export OS_NAME=$(uname -s)
case "$OS_NAME" in
  SunOS)
        export OS_VENDOR="Sun"
        export OS_NAME="Solaris"
        export XTERM="/usr/openwin/bin/xterm"
        export USER=$LOGNAME
        ;;
  Linux)
        export OS_VENDOR="$(cd /etc; ls [Sr]*-release | sed -e 's/-release//')"
        export OS_NAME="Linux"
        export XTERM="/usr/bin/xterm"
        ;;
esac

export LC_ALL=C
export LC_TYPE=C

#####################################################################
# Informatica 1011 required environment variable settings           #
#####################################################################
export PC1011_HOME=/cs/informatica
export INF_HOME=$PC1011_HOME/software/CS_InformaticaCLULin-10.1.1-1.x86_64
export LANG=C
export PWRCENTER_DIR=$INF_HOME/PowerCenter
export PCSERVER_DIR=$PWRCENTER_DIR/server
export PCSERVER_BIN=$PCSERVER_DIR/bin
export EXTRA_LIB=$SOFTWARE_DIR/extras/lib
export INFA_HOME=$PWRCENTER_DIR
#####################################################################
# JAVA Settings (Bundled with PowerCenter)                          #
#####################################################################
export JAVA_HOME=$PWRCENTER_DIR/java/jre
export TOMCAT=$PWRCENTER_DIR/server/tomcat
export JBOSS_HOME=$TOMCAT/jboss
export PC1011_LOG=$TOMCAT/logs/node.log

#####################################################################
# Set the WAIT4STARTUP parameter to 15 seconds for a fast server    #
# and 45 seconds for a slow server. Wait before proceeding          #
#####################################################################
export PWRCENTER_WAIT4STARTUP=30

#####################################################################
# Oracle Client Settings                                            #
#####################################################################
#export ORACLE_HOME=/cs/oracle/product/12102/client_1
#export ORACLE_LIBS=$ORACLE_HOME/lib
#export TNS_ADMIN=$PC1011_HOME/software/extras/oracle/network/admin

#####################################################################
# Sybase Open Client Settings                                       #
#####################################################################
export SYBASE=/cs/sybase/open_client
export SYBFORMLIST=". ${SYBASE}"
export SYBASE_ASE=ASE-12_5
export SYBASE_OCS=OCS-15_0

# Define the default Sybase server
export DSQUERY='DEV'
export SYBASE_LIBS=$SYBASE/OCS-15_0/lib:$SYBASE/OCS-15_0/lib3p64:$SYBASE/OCS-15_0/lib3p

#####################################################################
# Sybase IQ Client Settings                                                #
#####################################################################

export SYBASEIQ=/app/sybase/iq_client/IQ-16_0
export SYBASEIQ_LIBS=$SYBASEIQ/lib64

####################################################################
# ODBC Driver Settings                                                    #
####################################################################

export ODBC_HOME=$PWRCENTER_DIR/ODBC7.1
export ODBCINI=$ODBC_HOME/odbc.ini

#####################################################################
# RainStor ODBC Driver Settings                                             #
#####################################################################

export ODBCSYSINI=$ODBC_HOME
#export SIMBAINI=/cs/rainstor/client/lib/RainStor-ODBC-4/simba.ini
#export OASDK_ODBC_HOME=/cs/rainstor/client/lib/RainStor-ODBC-4/lib64
export SIMBAINI=/cs/rainstor/client/lib/RainStor-ODBC-4/simba.ini
export OASDK_ODBC_HOME=/cs/rainstor/client/lib/RainStor-ODBC-4/lib64


#####################################################################
# DB2 Client Settings                                                       #
#####################################################################

export DB2=/cs/db2client/sqllib
#export DB2BIN=$DB2/bin
#export DB2LIB=$DB2/lib

####################################################################
# Following added by Jessie Mundrathi/Joel Shandelman on Dec 8 2004#
# for MQ Series. The Connection properties of the IBM MQSeries     #
# queue manager should be in the following format:                 #
# <Channel Name>/<Transport Type>/<Connection Name>                #
####################################################################

#export MQSERVER="USUDE1_SVRCONN/TCP/169.37.67.133(1421)"
#export LDR_CNTRL="MAXDATA=0x60000000"

#export MQCLIENTPATH=/cs/mqm_client_6.0.2.0/bin
#export MQCLIENTLIB=/cs/mqm_client_6.0.2.0/lib
#export MQCHLTAB=AMQCLCHL.TAB
#export MQCHLLIB=/var/mqm/exits

#####################################################################
# Set PATH and LD_LIBRARY_PATH                                      #
#####################################################################
export PATH=$JAVA_HOME/bin:$ODBC_HOME/bin:$SCRIPTS_DIR:$SCRIPTS_DIR/bin:$SYBASE/OCS-15_0/bin:$SYABSEIQ/bin64:$MM_HOME/bin/ssunos5:$TOMCAT/bin:$PCSERVER_DIR:$PCSERVER_BIN:$ORACLE_HOME/bin:/usr/local/bin:/usr/bin:/bin:/eqsw/mqm/samp/bin:/cs/mqm_client_6.0.2.0/bin:/cs/mqm_client_6.0.2.0/samp/bin:$DB2/bin:$PATH

export LD_LIBRARY_PATH=$JAVA_HOME/bin:$ODBC_HOME/lib:/cs/informatica/RainStor-ODBC-3/lib64:$SOFTWARE_DIR:$PCSERVER_BIN:$ORACLE_LIBS:$MM_HOME/lib/ssunos5:$SYBASE_LIBS:$SYBASEIQ_LIBS:$MQCLIENTLIB:$MQCLIENTPATH:/usr/lib64:$PCSERVER_LIB:$DB2/lib:$EXTRA_LIB

and my .bash_profile contains:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

if [ -f ~/env.sh ]
then
. ~/env.sh
fi

export LD_LIBRARY_PATH=/cs/python3/python/lib/
#export LD_LIBRARY_PATH=/usr/lib64/python3.6
export JAVA_HOME=/cs/ctc/jre-server/linux/jdk1.8.0_202/jre/bin
#PATH=/cs/python3/python/bin:$PATH:$HOME/.local/bin:$HOME/bin
#================================================
#Informatica 1011 related env variables
#================================================
#export INFA_HOME=/cs/informatica/software/CS_InformaticaCLULin-10.1.1-1.x86_64/PowerCenter
#export INFA_DOMAINS_FILE=/cs/informatica/software/CS_InformaticaCLULin-10.1.1-1.x86_64/PowerCenter/domains.infa
#===============================================
export ORACLE_HOME=/cs/oracle/product/11204/client_1
export PATH=/cs/ctc:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/puppetlabs/bin:/cs/oracle/product/11204/client_1/lib:/cs/oracle/product/11204/client_1/bin
export TNS_ADMIN=/cs/oracle/product/11204/client_1
PATH=$PATH:/cs/ctc/jre-server/linux/jdk1.8.0_202/bin
export PATH

Any help is most welcome!!!

User123
  • 1,031
  • 1
  • 9
  • 21
  • What's the output when you type `echo $SHELL`? – Mihir Oct 04 '19 at 08:07
  • @Mihir: it's: `/bin/bash` – User123 Oct 04 '19 at 08:08
  • Also, wrap `~/env.sh` into pair of double quotes at both places so that if path contains spaces, it works well. – Mihir Oct 04 '19 at 08:08
  • Use "$HOME" instead of `~`, https://stackoverflow.com/questions/5748216/tilde-in-path-doesnt-expand-to-home-directory – Mihir Oct 04 '19 at 08:08
  • `[ -f ~/env.sh ] && echo yes`, could you tell me the output of this? – Mihir Oct 04 '19 at 08:10
  • the output is :`yes` – User123 Oct 04 '19 at 08:13
  • 1
    That exact piece of code is working fine for me from `~/.bash_profile`. I'm using `bash` shell as well. Can you double-check the file permissions? – Pacifist Oct 04 '19 at 08:14
  • It should be getting sourced then. Everything is fine with your code. Could you show me the contents of `env.sh`? What os do you use? – Mihir Oct 04 '19 at 08:15
  • 2
    [`~` should **not** be wrapped in quotes. That would prevent `tilde expansion` by the shell.](http://mywiki.wooledge.org/BashPitfalls#echo_.22.2BAH4.22) `~` is expanded to `"$HOME"` and not `$HOME`. So you're safe there. – anishsane Oct 04 '19 at 08:51
  • Since the code you showed us is perfect and should work, can you show us the content of the env.sh file? Let's see if it has something that makes the execution imposible. – Miki4444 Oct 04 '19 at 10:15
  • @Mihir: I've updated my Question. I'm using Linux. – User123 Oct 04 '19 at 10:35
  • Why does the file have a `#!/bin/ksh` shebang? – tripleee Oct 04 '19 at 10:39
  • `egrep ^'(domain|search)' /etc/resolv.conf | awk '{print $2}' | head -1)` can be refactored to just `awk /^(domain|search)/ { print $2; exit }' /etc/resolv.conf`. See also [useless use of `grep`.](http://www.iki.fi/era/unix/award.html#grep) – tripleee Oct 04 '19 at 10:40

1 Answers1

1

The stuff after you source env.sh overrides many of the things it tried to initialize. If you don't want to replace the LD_LIBRARY_PATH it created etc, move the sourcing of env.sh to after the stuff in your .bash_profile.

The code in env.sh generally does a better job of preserving any earlier customizations. For example, it extends the existing value of LD_LIBRARY_PATH by adding stuff to the old value, rather than flat out replacing it with a static value.

LD_LIBRARY_PATH=new_stuff:$LD_LIBRARY_PATH

rather than

LD_LIBRARY_PATH=new_stuff
tripleee
  • 139,311
  • 24
  • 207
  • 268