--**GURUNANAK


--**AIX Search for DB2 profile
    find / -name db2profile  2>&1 | grep "^/" 
    
--**AIX read_ini_sec function
    ##==============================================read_ini_sec filename section [key] [var]
    ##--reads key=value pair from ini style text file. # is rem char for a line
    ##---- 1)  read_ini_sec <filename> <section> - reads all sectioned key=val pairs
    ##---- 2)  read_ini_sec <filename> <section> [key] - reads one sectioned key=val pair
    ##---- 3)  read_ini_sec <filename> <section> [key] [var] - reads one sectioned key=val pair into var
    ##----------------------------------------------
    read_ini_sec() {
     filename=$1 ; section=[$2]; key=""; val=""; sstart=""; fword=""
    	while read Line
    	do 
    		##-----read first word of line (fword)
    		echo $Line | read fword
    		##-----check if first word in non-null and first letter is "[" - termination of a section
    		if [[ -n $sstart ]] && [[ "$(echo $fword |cut -c1 )" = "[" ]] ; then 
    			    sstart=""
                  fi 
    		##-----while in section, read key=value as per parameters $3 and $4
    		if [[ -n $sstart ]] && [[ -n $fword ]]  ; then
    			echo $Line | read Line
    			echo $Line | cut -c1 | read fchar
    			if [[ "$fchar" != "#" ]]; then
    			echo $Line | cut -d'=' -f1 | read key
    			echo $Line | cut -d'=' -f2 | read val 
    		 	if [[ -n $key ]] && [[ -n $val ]] && [[ $key = $3 ]] && [[ -z $4 ]]; then echo $val | read $key ; fi
    			if [[ -n $key ]] && [[ -n $val ]] && [[ $key = $3 ]] && [[ -n $4 ]]; then echo $val | read $4 ; fi
    			if [[ -n $key ]] && [[ -n $val ]] && [[ -z $3 ]]; then echo $val | read $key ; fi
    			fi	 
    		fi
    		##-----signal start of section if fword equals section name enclosed in "[]"
    		if [[ -n $fword ]] && [[ "$fword" = "$section" ]]; then
    			   sstart=yes
          		fi
    	done < $filename
    }
    ##==============================================
    
    
--**AIX sendmail for simpler usage : send_email
    #!/bin/ksh
    ##======================= send email========================
    ##--author     : sarbjit singh gill
    ##--date       : 2011-12-08
    ##--type       : ksh function/sub routine
    ##--syntax     : send_email <email_ids> <subject> <body> [<attachment>]
    ##===========================================================
    typeset os=$(uname -s )	#--OS name
    rc=0
    case $os in
      	HP-UX)
      		send_mail=/usr/sbin/sendmail
      		;;
      	*)
      		send_mail=/usr/sbin/sendmail
      		;;
    esac
    [[ -n $1 ]]&&[[ -z $4 ]]&&{(print "to:$1\nsubject:$2\n\n$3";) | ${send_mail} -t ; rc=$? ;}
    [[ -n $1 ]]&&[[ -n $4 ]]&&{(print "to:$1\nsubject:$2\n\n$3";uuencode $4 $4;) | ${send_mail} -t ; rc=$? ;}              
    exit $rc 
    ##===========================================================
    
    
--**AIX txtout -- Convert unix text files to dos or mac
    ##======================= Text Out========================
    ##--author     : sarbjit singh gill
    ##--date       : 2011-12-08
    ##--type       : ksh function/sub routine
    ##--syntax     : txtout <infile> [[<outformat>] <outfile>]
    ##--txtout reads unix text files writes 0-dos 1-unix 2-mac
    ##===========================================================
    ##
    txtout(){ typeset Line fmt ; fmt="\r\n";
    [[ "$2" = "1" ]] && fmt="\n";[[ "$2" = "2" ]] && fmt="\r";
    if [[ -z $3 ]]; then while read Line;do printf "${Line}${fmt}" ;done < $1 ;
    else while read Line;do printf "${Line}${fmt}" >>$3; done < $1 ;
    fi
    }
    ##===================txtout
    
    
--**db2 simulate a deadlock (CLP)
    
    	Steps :
    			1. Open 3 db2 client windows : window 1 for Monitor, 2 and 3 for App1 and App2
    			2. Turn auto commit off ( App1 and App2 windows )
    			3. App1 inserts a row in Tab1. ( not commit )
    			4. App2 inserts a row in table Tab2. ( not commit)
    			5. App1 reads a row from table Tab2.
    			6. App2 reads a row from table Tab1.
    			
    		One application will be rolled back because of deadlock condition. which one ( 2 ) ?
    		
     
    
--**db2 database size
    
    . /opt/db2inst1/sqllib/db2profile
    
    db2 connect to $1
    
    db2 " call get_dbsize_info(?,?,?,0)"
    
    db2 -x "SELECT 'SERVER',TS.TBSP_PAGE_SIZE * TP.TBSP_TOTAL_PAGES / 1024 /1024 \
            FROM table (snap_get_tbsp_part('',-2)) as TP\
            inner join table (snap_get_tbsp('',-2)) as TS ON TP.TBSP_ID = TS.TBSP_ID "| grep SERVER | awk '{ sum+=$2}END{ print "TableSpaces Total = "sum " MB\n"}'
         
    db2 connect reset
    
     
    
--**Show Lock Wait Chains
     
    
    select substr(ai_h.appl_name,1,10) as "Hold App",
    
        substr(ai_h.primary_auth_id,1,10) as "Holder",
    
        substr(ai_w.appl_name,1,10) as "Wait App",
    
        substr(ai_w.primary_auth_id,1,10) as "Waiter",
    
        lw.lock_mode as "Hold Mode",
    
        lw.lock_object_type as "Obj Type",
    
        substr(lw.tabname,1,10) as "TabName",
    
        substr(lw.tabschema,1,10) as "Schema",
    
        timestampdiff(2,char(lw.snapshot_timestamp - lw.lock_wait_start_time)) 
    
            as "waiting (s)"
    
    from  sysibmadm.snapappl_info ai_h, 
    
          sysibmadm.snapappl_info ai_w, sysibmadm.snaplockwait lw
    
    where lw.agent_id = ai_w.agent_id
    
    and   lw.agent_id_holding_lk = ai_h.agent_id
    
    
--** DB2 Queries
    
    --**Find Long Running SQL:
    --------------------------
     
    
    SELECT ELAPSED_TIME_MIN,
    
                  SUBSTR(AUTHID,1,10) AS AUTH_ID, 
    
                  AGENT_ID,
    
    APPL_STATUS,
    
    SUBSTR(STMT_TEXT,1,20) AS SQL_TEXT
    FROM    SYSIBMADM.LONG_RUNNING_SQL
    WHERE ELAPSED_TIME_MIN > 0
    ORDER BY ELAPSED_TIME_MIN DESC
    
    
    --**Show Queries and Statics from the Package Cache:
    ----------------------------------------------------
     
    
    SELECT 
    
        SUBSTR(STMT_TEXT,1,20) AS STMT,
    
        SECTION_TYPE AS TYPE,
    
        NUM_EXECUTIONS,
    
        TOTAL_ACT_TIME AS TOTAL_TIME,
    
        TOTAL_ACT_WAIT_TIME AS WAIT_TIME
    
    FROM TABLE(MON_GET_PKG_CACHE_STMT('','','',-1))
    
    
--**List open ports and listening services
    
      netstat -a | egrep 'Proto|LISTEN'
      netstat -an | egrep 'Proto|LISTEN'
      lsof -i -n | egrep 'COMMAND|LISTEN'
      netstat -lnptu
      
    
--**List open ports and listening services Windows
    
    netstat -a | find "LISTENING"
     
    
--**Check Argument Numeric -KSH/BASH/AWK
    
    ( echo ${myVar} | awk '(( $1 + 0 ) != $1 ) { exit 1}' ) && echo The Arg is a number
    
    	OR
    
    ( awk -v arg=${myVar} ' BEGIN { if (arg+0!=arg) exit 1}' ) && echo The Arg is a number
    
    	IF construct is as below
    	
    if ( awk -v arg=$1 ' BEGIN { if (arg+0!=arg) exit 1}' )
    	then
    	
    	echo Arg is numeric
    		
    fi
    
    
    
--** String of chars
    
    lstr=""
    nchars=10
    while [[ ${#lstr} -lt ${nchars} ]]; do lstr="${lstr} ";done
    
    
    
--**tar bz2 xtract
    
    tar xvzf file-1.0.tar.gz - for uncompress a gzip tar file (.tgz or .tar.gz) 
    tar xvjf file-1.0.tar.bz2 - for uncompress a bzip2 tar file (.tbz or .tar.bz2) 
    tar xvf file-1.0.tar - for uncompressed tar file (.tar)