Pages

Thursday, January 7, 2010

Korn Shell Snippets

Numeric Test --

1.
if [[ -z $(echo $2 | sed 's/[0-9]//g') ]]
then
echo "integer only"
fi

if [[ -z $(echo $2 | awk '/^[0-9]+$/') ]]
then
echo "integer only"
fi

if [[ ! -z $(awk -v x=$a 'END{if(x==x+0) print "integer"}' /dev/null) ]]
then
echo "integer only"
fi

Select * From table_name Where Regexp_Like(column_name, '^[0-9]+
Display Shell line number --

typeset -x PS4='[$LINENO] '
ksh -x script)

Alias to display File access/mod/create time --
alias ft='_(){ echo "modify time: \c";ls -l $1; echo "create time: \c"; ls -lc $1; echo "access time: \c";ls -lu $1; }; _'

Display Date Time at prompt:
export SECONDS="$(date '+3600*%H+60*%M+%S')";typeset -Z2 _h; typeset -Z2 _m ; typeset -Z2 _s;_hh="(SECONDS/3600)%24";_mm="(SECONDS/60)%60";_ss="(SECONDS)%60";_time='${_x[(_m=_mm)==(_h=_hh)==(_s=_ss)]}$_h:$_m:$_s';
export PS1=$(echo "${_time}")':$PWD>';

Date math & TZ (AIX): /etc/environment (https://www-304.ibm.com/support/docview.wss?uid=isg3T1000252)

TZ=CST6CDT,M3.2.0/2:00:00,M11.1.0/2:00:00 (DST enabled)
  • CST6CDT is the time zone you are in;
  • M3 is the third month;
  • .2 is the second occurrence of the day in the month;
  • .0 is Sunday;
  • /2:00:00 is the time.
# get current TZ value
tz=`echo $TZ | tr -s '[:upper:]' '[\0*]' | cut -d, -f"1"`

# 2 hrs back
((tz_2hr_back=$tz+2))

# date string format = MMDDHHMIYY
TZ=GMT+$tz_2hr_back date +%m%d%H%M%y;


ksh93 Associative Array:
unsert color
set -A color
color=([Application Engine]=1 \
[COBOL SQL]=2 \
[Crystal]=3 \
[SQR Process]=4 \
[SQR Report]=5)


${color[@]} = all elements of the array
${!color[@]} = subscripts of an array
${#color[@]} = # of elements within the array.
${color[@]:offset:length} = Elements within a numeric subscript range


echo ${color[@]}
echo ${!color[@]}
echo ${#color[@]}
echo ${color[@]:1:length}

idx=0
while [ $idx -lt ${#color[@]} ]
do
echo ${color[@]:$idx:1}
((idx=idx+1))
done