Pages

Wednesday, December 21, 2016

detect if a script is being sourced by another script

Source: http://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced


ksh

[[ \
   $(cd "$(dirname -- "$0")" && printf '%s' "${PWD%/}/")$(basename -- "$0") != \
   "${.sh.file}" \
]] && 
sourced=1 || sourced=0
Special variable ${.sh.file} is somewhat analogous to $BASH_SOURCE; note that ${.sh.file}causes a syntax error in bash, zsh, and dash, so be sure to execute it conditionally in multi-shell scripts.
Unlike in bash, $0 and ${.sh.file} are NOT guaranteed to be exactly identical in the non-sourced case, as $0 may be a relative path, while ${.sh.file} is always a full path, so $0 must be resolved to a full path before comparing.



Friday, November 18, 2016

Show NLS Parameters


source: https://ferhatsengonul.wordpress.com/2010/07/21/to-show-nls-parameters-for-session-database-instance-together-by-pivot-and-listagg-on-11gr2/

set linesize 200
col "PARAMETER" format a30
col "SESSION" format a30
col DATABASE  format a30
col INSTANCE  format a30
select * from
(select 'SESSION' SCOPE,nsp.* from nls_session_parameters nsp
union
select 'DATABASE' SCOPE,ndp.* from nls_database_parameters ndp
union
select 'INSTANCE' SCOPE,nip.* from nls_instance_parameters nip
) a
pivot  (LISTAGG(VALUE) WITHIN GROUP (ORDER BY SCOPE)
FOR SCOPE
in ('SESSION' as "SESSION",'DATABASE' as DATABASE,'INSTANCE' as INSTANCE));
.

Tuesday, August 30, 2016

Directory Tree

Display folders hierarchy in tree style: 

ex: dt  -- all folders from current
ex: dt 2 -- 2 levels from current
ex: dt xxx - all folders from xxx, which is a 1st level subfolder from current
ex: dt xxx 3 - 3 levels from xxx, which is a 1st level subfolder from current

to look at only current folder , no sub-folder:

  • ls -d XXXX *.*
  • find XXXX -maxdepth 1 -type f 



OS: GNU/Linux
---------------------------------------------------------------------------------------------
dt(){
    if [[ -z $1 ]]; then
        find . -type d 2> /dev/null | sort | sed -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/';
    else
        if [[ -z $2 ]]; then
            if [ -d "$1" ]; then
               find $1 -type d 2> /dev/null | sort | sed -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/';
            else
               if [[ $1 =~ ^[0-9]+$ ]]; then
                  find . -maxdepth $1 -type d 2> /dev/null | sort | sed -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/';
               fi
            fi
        else
            find $1 -maxdepth $2 -type d 2> /dev/null | sort | sed -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/';
        fi;
    fi
}


------------------------ verbose: same as dt, with folder attribute info & size
d2(){
    if [[ -z $1 ]]; then
        find . -type d 2> /dev/null | sort | xargs -I % ksh -c "ls -ld % | tr '\n' ' ' ;   du -sh %" | awk '{print $9"        [ "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" " $10" ]"}' | sed -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/';
    else
        if [[ -z $2 ]]; then
            if [ -d "$1" ]; then
               find $1 -type d 2> /dev/null | sort | xargs -I % ksh -c "ls -ld % | tr '\n' ' ' ;   du -sh %" | awk '{print $9"        [ "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" " $10" ]"}' | sed -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/';
            else
               if [[ $1 =~ ^[0-9]+$ ]]; then
                  find . -maxdepth $1 -type d 2> /dev/null | sort | xargs -I % ksh -c "ls -ld % | tr '\n' ' ' ;   du -sh %" | awk '{print $9"        [ "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" " $10" ]"}' | sed -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/';
               fi
            fi
        else
            find $1 -maxdepth $2 -type d 2> /dev/null | sort | xargs -I % ksh -c "ls -ld % | tr '\n' ' ' ;   du -sh %" | awk '{print $9"        [ "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" " $10" ]"}' | sed -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/';
        fi;
    fi            
}