Drucken

Beschreibung

Das Shell script environment2.sh dient dazu Environment Variablen die in einer Datei definiert sind auszulesen und zu setzen.

Achtung:
Dies ist nur ein Beispiel und sollte an die individuellen Bedürfnisse angepaßt werden

Die Konfigurationsdatei sieht wie folgt aus:
#Hostname/Standard      #Environment            #Variable
#-----------------------------------------------------------------------
...
STANDARD                #SYSTEM_NAME            #`hostname`
STANDARD                #DATE_DAY               #`date +%y%m%d`
STANDARD                #DATE_FILE              #`date +%Y%m%d_%H%M%S`
STANDARD                #LOGDIR                 #/tmp
STANDARD                #LOGOWNER               #root:sys
STANDARD                #LOGPERM                #644
STANDARD                #REPORTLOGFILE          #$LOGDIR/`date +%y%m%d`_CCF.log
STANDARD                #REPORTOWNER            #root:sys
STANDARD                #REPORTPERM             #644
STANDARD                #TMPFILEDIR             #/tmp
STANDARD                #CONFIGDIR              #$COMPANY_BASE/local/etc
STANDARD                #ORACONFIGDIR           #/usr/local/oracle/etc
STANDARD                #BACKUP_CONFIG          #$COMPANY_BASE/`hostname`/etc
STANDARD                #CLEANUP_AGE            #45
STANDARD                #FIND_EXCLUDE           #"/SD_CDROM"
STANDARD                #TRUE                   #1
STANDARD                #FALSE                  #0
STANDARD                #RTN_CODE               #0
STANDARD                #RTN_BORDER             #0
STANDARD                #MAIL_RECEIVER          #"Reinhold.Flecke@XXX.com"
...
hpjapp7                 #BACKUP_CONFIG          #/var/backup/`hostname`
hp11itest               #BACKUP_CONFIG          #/var/backup/`hostname`
...
#### IGNITE HPUX Setting: ###
#############################
IgniteUX                #IGNITE_SERVER          #hpjapp7
IgniteUX                #IGNITE_CLIENT          #`hostname`
IgniteUX                #IGNITE_ARCHIVE         #"nas073:/vol/ignite_ux/q_files/archives/`hostname`/`uname -r`"
IgniteUX                #IGNITE_N_ARCHIVE       #4
IgniteUX                #IGNITE_OPTIONS         #"-x inc_entire=vg00 -x exclude=/ccfmnt -d `hostname`-`uname -r`-Archive"
IgniteUX                #IGNITE_TAPE            #/dev/rmt/0mn
IgniteUX@hp11itest      #IGNITE_SERVER          #10.16.5.41
IgniteUX@hp11itest      #IGNITE_ARCHIVE         #"10.16.5.11:/var/opt/ignite/archives/`hostname`/`uname -r`"
IgniteUX@hpcapp1        #IGNITE_OPTIONS         #"-f /var/opt/ignite/etc/ignite_archive_content_`hostname`"
IgniteUX@hpcapp2        #IGNITE_OPTIONS         #"-f /var/opt/ignite/etc/ignite_archive_content_`hostname`"
IgniteUX@hpjapp8        #IGNITE_SERVER          #hp11itest
IgniteUX@hpx2660        #IGNITE_SERVER          #hp11itest
IgniteUX@hpx2660        #IGNITE_OPTIONS         #"-f /usr/local/edc/etc/testen"
...
#### Tivoli Setting: ########
#############################
TivoliFrmwk             #FRMWK_ENTERPRISE       #'1.3.6.1.4.1.50'
TivoliFrmwk             #FRMWK_HOST             #'aixhu02'
TivoliFrmwk             #FRMWK_RED_ALERT        #1410
TivoliFrmwk             #FRMWK_YELLOW_ALERT     #1420
TivoliFrmwk             #FRMWK_CLEAR_ALERT      #1430
TivoliFrmwk             #FRMWK_CALLOUT          #"CCF (Reinhold Flecke +49 XXXX XXXXXX)"
TivoliFrmwk             #FRMWK_CALLINFO         #"CCF-HU"
TivoliFrmwk             #FRMWK_ALERT            #"Test Alert. Please ignore"
TivoliFrmwk             #FRMWK_TRAP             #/opt/tivoli/frmwk/bin/wsnmptrap
TivoliFrmwk@hpx2660     #FRMWK_TRAP             #/opt/tivoli/frmwk/HPUX/B.11.31/bin/wsnmptrap
TivoliFrmwk@hpx2660     #FRMWK_CALLOUT          #"CCF (Ein Anderer +009 XXXX XXXXXX)"
TivoliFrmwk@hpjapp8     #FRMWK_TRAP             #/opt/tivoli/frmwk/HPUX/B.11.23/bin/wsnmptrap
...

Anwendung

mit Hilfe der -he Option erhält man Informationen bzgl. der benutzten Optionen.
environment2.sh -he

environment2.sh
#-------------
script setting standard definitions
usage:  environment2.sh [-he]
usage:  environment2.sh [-de] [-ce configfile] [-Fe Projekt]
        -he       -shows help message for environment.sh
        -de       -debug display only commands
        -ce value -configfile
        -Fe value [value2] [value3] ...
                  -search for specific environment settings
#-----------------------------------------------------------------------

Die -he Option ist ein Sonderfall, aber notwendig, da dieses Script Bestandteil einer Programmierumgebung ist und allein nicht genutzt wird. Die standardäßige -h oder --help Option wird in den eigentlichen Programmen benutzt.
Anbei eine detailiert Beschreibung der einzelnen Optionen:
  • -he
    ist ein Sonderfall, aber notwendig, da dieses Script Bestandteil einer Programmierumgebung ist und allein nicht genutzt wird. Das gleiche gilt für die -hf Option des function2.sh? scriptes. Die standardäßige -h oder --help Option wird in den eigentlichen Programmen benutzt (siehe auch Shell Programmierung ).
  • -de
    diese Option ist zum debuggen der aus dem configfile ausgelesenen Umgebungsvariablen.
  • -ce value
    kann für spezielle config files gesetzt werden, die nicht auf dem standard PATH
    /ccf/[Server]/etc/environment.cfg:/ccf/[OS]/etc/environment.cfg:/ccf/local/etc/environment.cfg
    gespeichert sind.
  • -Fe value [value2] [value3] ...
    dies ist der search String der dazu benutzt wird die Umgebungsvariablen aus dem environment.cfg file zu extrahieren.
    per default wird STANDARD und der Servername `hostname` benutzt

Beispiele

Source

#@(#) Programm: environment2.sh                                                                                                                                 
#@(#) Revision: 2.0                 ### Reinhold Flecke CCF ### 03.12.03                                                                                        
#@(#) Revision: 2.1                 ### Reinhold Flecke CCF ### 05.08.05                                                                                        
#@(#) Revision: 2.2                 ### Reinhold Flecke CCF ### 02.02.06                                                                                        
#@(#) modify -Fe and -F Option (multiple Project Definitions)                                                                                                   
#@(#) properly working also with -ce Definitions                                                                                                                
#@(#) ##################################################################                                                                                        
#set +o nounset                                                                                                                                                 
#set -x                                                                                                                                                         
#echo $SHELL                                                                                                                                                    
 
ENVIRONMENT_SCRIPT=environment2.sh
touch /tmp/$$_$ENVIRONMENT_SCRIPT 
 
EXIT_CODE=0
shift_number=0
TRUE=1        
FALSE=0       
: ${helpflag:=$FALSE}
: ${lflag:=$FALSE}   
: ${dflag:=$FALSE}   
: ${deflag=$FALSE}   
: ${ceflag=$FALSE}   
: ${Feflag=$FALSE}   
PROJECT=""           
 
#-----      set COMPANY_BIN name     -----------------------------------
#-----------------------------------------------------------------------
COMPANY_BIN=${COMPANY_BIN:-/ccf/local/bin}                              
 
#-----     print programm options     ----------------------------------
#-----------------------------------------------------------------------
add_to_help()                                                           
{                                                                       
   if [ "${HELP_ARRAY[0]}" = "" ]; then                                 
      HELP_ARRAY[0]=0                                                   
      HELP_ARRAY[1]=0                                                   
      HELP_ARRAY[2]=0                                                   
   fi                                                                   
   CHECK=${2:-0}                                                        
   if [ "${HELP_ARRAY[CHECK]}" = "0" ]                                  
   then                                                                 
      HELP_ARRAY[${#HELP_ARRAY[@]}]="$1"                                
   fi                                                                   
}                                                                       
 
programm_option()
{                
n_HELP_INDEX=${#HELP_ARRAY[@]}
HELP_INDEX=3                  
while (( HELP_INDEX <= n_HELP_INDEX )); do
  printf "${HELP_ARRAY[HELP_INDEX]}"      
  let HELP_INDEX=HELP_INDEX+1             
done                                      
}                                         
 
environment_help()
{                 
add_to_help "$ENVIRONMENT_SCRIPT\n" 2
add_to_help "#-------------\n" 2     
add_to_help "script setting standard definitions\n" 2
add_to_help "usage:\t$ENVIRONMENT_SCRIPT [-he]\n" 2  
add_to_help "usage:\t$ENVIRONMENT_SCRIPT [-de] [-ce configfile] [-Fe Projekt]\n" 2
add_to_help "\t-he       -shows help message for environment.sh\n" 2              
add_to_help "\t-de       -debug display only commands\n" 2                        
add_to_help "\t-ce value -configfile\n" 2                                         
add_to_help "\t-Fe value [value2] [value3] ...\n" 2                               
add_to_help "\t          -search for specific environment settings\n" 2           
add_to_help "#-----------------------------------------------------------------------\n" 2
HELP_ARRAY[2]=1                                                                           
}                                                                                         
 
#----------     check Option     ---------------------------------------
#-----------------------------------------------------------------------
while [ $# -gt 0 ]; do                                                  
   case $1 in                                                           
   -he)                                                                 
     environment_help                                                   
     programm_option                                                    
     exit 2                                                             
     ;;                                                                 
   -h|-hf)                                                              
     helpflag=1                                                         
     environment_help                                                   
     shift                                                              
     ;;                                                                 
   -de)                                                                 
     deflag=1                                                           
     shift                                                              
     ;;                                                                 
   -ce)                                                                 
     #ENVIRONMENT_CONFIG_FILE=`echo $2 | sed -e "s/\"//g"`              
     ENVIRONMENT_CONFIG_FILE=$2                                         
     ceflag=1                                                           
     shift 2                                                            
     ;;                                                                 
        -Fe)                                                            
                Feflag=1                                                
                shift                                                   
                PROJECT=`echo "$*" | sed -e "s/\-.*//" -e "s/[        ]*$//"`
                SHIFT_NUM=`echo "$PROJECT" | wc -w`                          
                shift $SHIFT_NUM                                             
                ;;                                                           
     *)                                                                      
     shift                                                                   
     ;;                                                                      
   esac                                                                      
done                                                                         
 
if [ $helpflag = $FALSE ]; then
#-----      set ENVIRONMENT_CONFIG_FILE name     -----------------------
#-----------------------------------------------------------------------
# Remember CONFIF_FILE is a result of ENVIRONMENT_CONFIG_DIR and SCRIPT 
# ======== (excluding extensions like .sh .ksh .csh and extended with .cfg)
if [ ${ENVIRONMENT_CONFIG_FILE:-NO} = NO ] ; then                          
   ENVIRONMENT_CONFIG_FILE=${ENVIRONMENT_CONFIG_DIR:-`dirname $COMPANY_BIN`/etc}/`echo $ENVIRONMENT_SCRIPT | sed -e 's/\.sh$//' -e 's/\.[ck]sh$//'`".cfg"
   OS_ENVIRONMENT_CONFIG_FILE=`dirname $ENVIRONMENT_CONFIG_FILE`/../../`uname`/etc/`basename $ENVIRONMENT_CONFIG_FILE`                                   
   HOST_ENVIRONMENT_CONFIG_FILE=`dirname $ENVIRONMENT_CONFIG_FILE`/../../`hostname`/etc/`basename $ENVIRONMENT_CONFIG_FILE`                              
   if [ -r $OS_ENVIRONMENT_CONFIG_FILE ]                                                                                                                 
   then                                                                                                                                                  
        ENVIRONMENT_CONFIG_FILE=$OS_ENVIRONMENT_CONFIG_FILE                                                                                              
   fi                                                                                                                                                    
   if [ -r $HOST_ENVIRONMENT_CONFIG_FILE ]                                                                                                               
   then                                                                                                                                                  
        ENVIRONMENT_CONFIG_FILE=$HOST_ENVIRONMENT_CONFIG_FILE                                                                                            
   fi                                                                                                                                                    
fi                                                                                                                                                       
 
 
#-----      running for standard or specified SETTINGS      ------------
#-----------------------------------------------------------------------
if [ $Feflag = $TRUE ]
then
        RUN_FOR_SETTING="$PROJECT"
else
        RUN_FOR_SETTING="STANDARD"
fi
 
#-----      set Environement using ENVIRONMENT_CONFIG_FILE     ---------
#-----------------------------------------------------------------------
#FUNCTIONS=${FUNCTIONPLUGINS:-""}
for SETTINGS in $RUN_FOR_SETTING
do
   cat $ENVIRONMENT_CONFIG_FILE | grep -v "^#" | grep "^$SETTINGS.[       ]"  |  cut -d"#" -f2,3 | sed -e "s/#//" | while read ENVIRONMENT DEFINITION
   do
      if [ "${ENVIRONMENT}" = "FUNCTIONPLUGINS" -a "${DEFINITION}" != "" ]; then
      if [ $deflag = $TRUE ] ; then
         echo load $DEFINITION
                        echo . $DEFINITION >> /tmp/$$_$ENVIRONMENT_SCRIPT
      else
                        echo . $DEFINITION >> /tmp/$$_$ENVIRONMENT_SCRIPT
      fi
      else
      if [ $deflag = $TRUE ] ; then
         echo $ENVIRONMENT="$DEFINITION"
         echo export $ENVIRONMENT="$DEFINITION" >> /tmp/$$_$ENVIRONMENT_SCRIPT
      else
         echo export $ENVIRONMENT="$DEFINITION" >> /tmp/$$_$ENVIRONMENT_SCRIPT
      fi
      fi
   done
done
 
. /tmp/$$_$ENVIRONMENT_SCRIPT
rm -f /tmp/$$_$ENVIRONMENT_SCRIPT
 
fi