Fw: shell script ?
Posted in 2000
Topics: Server Administration
> Below is a script that I got from a posting on this informix users list:
>
> I can recognize some c constructs, but this code is evidently interpreted
by
> the shell. I would appreciate some feedback from you listers.
> Where can the "getline" and "print" functions be found?
Where are the definitions of the arrays or functions?
> Regards,
>
> DW
==================================================>
> #!/bin/sh
>
> ########################################################################
> ########
> #
> # Shell Script: lockrep
> #
> # Description: Program to find out tables which is locked by users.
> #
> # Author: Jayakumar George Date: 12/08/97
> #
> # Ver 1.1 Modified the script(31/10/97) to access the database
> once
> for
> # each database since the script is very slow and also
> lists
> # user created temp tables if it is locked.
> # Ver 1.2 Modified output for just released locks to fix output
> bug
> # 08/14/2000 Gary Quiring
>
> ########################################################################
> ########
>
> onstat -u | nawk ' BEGIN {
> getline;getline;getline;getline;getline;ctr = 1
> }
> { if ( $2 == "active,")
> {
> while(getline)
> }
> else
> {
> if ($8 1)
> {
> username[ctr]=$4
> sessionid[ctr]=$3
> useraddr[ctr]=$1
> ctr++
> }
> }
> tmpctr=1>
> while("onstat -g sql"|getline)
> {
> if(tmpctr 5)
> databases[$1]=substr($0,22,18)
> tmpctr++
> }
> close("onstat -g sql ")
> }
> END {
> if (ctr == 1)
> {
> print "No User(s) Has a lock"
> exit 1
> }
> print "The following User(s) has lock(s)"
> print "User ID TTY Host/IP Sess.
> ID
> Database Table(s) "
> print "------- --- ------------- -------
> -
> -------- ----------------"
> for(forctr=1;forctr< ctr;forctr++)
> {
> cmd="onstat -g ses " sessionid[forctr]
> while(cmd | getline)
> {
> if($1 == "partnum")
> {
> while(cmd | getline)
> {
> tmptabname[$1]=$2
> }
> break
> }
> }
> close(cmd)
> cmd=""
> "listusers|grep " username[forctr] | getline
> uname
> printf "%-36s%8d %-15s",uname
> ,sessionid[forctr],substr(databases[sessionid[forctr]],1,15)
> close("listusers")
> cmd = "onstat -k | grep " useraddr[forctr]
> dbtabflag = 0
> printnull = 0
> while(cmd | getline)
> {
> if($4 != 0)
> {
> if(tmptabname[$6])
> tmptabs_locked[$6]=tmptabname[$6]
> else
> {
> dbtabflag++
> hextoint($6)
>
> select_tablename(databases[sessionid[forctr]])
>
> tabname_arr[res]=dbtab_arr[databases[sessionid[forctr]],res]
> }
> }
> }
> close(cmd)
> ctrcmd2=1
> if(dbtabflag)
> {
> for (tabname in tabname_arr)
> {
> if(ctrcmd2 == 1)
> printf "%-18s",tabname_arr[tabname]
> else
> printf "%60c%-18s",32,tabname_arr
> [tabname]
> ctrcmd2++
> printf "\\n"
> printnull = 1
> delete tabname_arr[tabname]
> }
> }
> for(tmptabvar in tmptabs_locked)
> {
> if(ctrcmd2 == 1)
> printf "(T) %-18s",tmptabs_locked
> [tmptabvar]
> else
> printf "%60c(T)
> %-18s",32,tmptabs_locked[tmptabvar]
> ctrcmd2++
> printf "\\n"
> printnull = 1
> delete tmptabs_locked[tmptabvar]
> }
> if (printnull == 0)
> printf "Lock just released\\n"
> }
> }
>
> function hextoint(a)
> {
> res=0
> base=16
> expn=0
> for(i=length(a);i0;i--)
> {
> val1=substr(a,i,1)
> if(match(val1,"[Aa]"))
> val1 = 10
> if(match(val1,"[Bb]"))
> val1 = 11
> if(match(val1,"[Cc]"))
> val1 = 12
> if(match(val1,"[Dd]"))
> val1 = 13
> if(match(val1,"[Ee]"))
> val1 = 14
> if(match(val1,"[Ff]"))
> val1 = 15
> res=res + (val1 * (base^expn))
> expn++;
> }
> }
>
> function select_tablename(dbname)
> {
> if(dbtab_arr[dbname ,0])
> {
> return
> }
> else
>
"Denmark B. Weatherburn" <dweatherb@btl.net> wrote in message
news:916690$gue$1@news.xmission.com...
>
> > Below is a script that I got from a posting on this informix users list:
> >
> > I can recognize some c constructs, but this code is evidently
interpreted
> by
> > the shell. I would appreciate some feedback from you listers.
>
> > Where can the "getline" and "print" functions be found?
> Where are the definitions of the arrays or functions?
>
> > Regards,
> >
> > DW
...8<...
> onstat -u | nawk ' BEGIN {
...8<...
It is an awk script (nawk).
Check your Unix documentation for the "awk" or "nawk" command. Awk is an
interpreted scripting language available on Unix systems. The script you
posted is a shellscript that pipes the output of onstat into an awk script.
"Denmark B. Weatherburn" <dweatherb@btl.net> wrote in message
news:916690$gue$1@news.xmission.com...
>
> > Below is a script that I got from a posting on this informix users list:
> >
> > I can recognize some c constructs, but this code is evidently
interpreted
> by
> > the shell. I would appreciate some feedback from you listers.
>
> > Where can the "getline" and "print" functions be found?
> Where are the definitions of the arrays or functions?
>
> > Regards,
> >
> > DW
> ==================================================>
> > #!/bin/sh
> >
> > ########################################################################
> > ########
> > #
> > # Shell Script: lockrep
> > #
> > # Description: Program to find out tables which is locked by users.
> > #
> > # Author: Jayakumar George Date: 12/08/97
> > #
> > # Ver 1.1 Modified the script(31/10/97) to access the database
> > once
> > for
> > # each database since the script is very slow and also
> > lists
> > # user created temp tables if it is locked.
> > # Ver 1.2 Modified output for just released locks to fix output
> > bug
> > # 08/14/2000 Gary Quiring
> >
> > ########################################################################
> > ########
> >
> > onstat -u | nawk ' BEGIN {
> > getline;getline;getline;getline;getline;ctr = 1
> > }
> > { if ( $2 == "active,")
> > {
> > while(getline)
> > }
> > else
> > {
> > if ($8 1)
> > {
> > username[ctr]=$4
> > sessionid[ctr]=$3
> > useraddr[ctr]=$1
> > ctr++
> > }
> > }
> > tmpctr=1> >
> > while("onstat -g sql"|getline)
> > {
> > if(tmpctr 5)
> > databases[$1]=substr($0,22,18)
> > tmpctr++
> > }
> > close("onstat -g sql ")
> > }
> > END {
> > if (ctr == 1)
> > {
> > print "No User(s) Has a lock"
> > exit 1
> > }
> > print "The following User(s) has lock(s)"
> > print "User ID TTY Host/IP Sess.
> > ID
> > Database Table(s) "
> > print "------- --- ------------- -------
> > -
> > -------- ----------------"
> > for(forctr=1;forctr< ctr;forctr++)
> > {
> > cmd="onstat -g ses " sessionid[forctr]
> > while(cmd | getline)
> > {
> > if($1 == "partnum")
> > {
> > while(cmd | getline)
> > {
> > tmptabname[$1]=$2
> > }
> > break
> > }
> > }
> > close(cmd)
> > cmd=""
> > "listusers|grep " username[forctr] | getline
> > uname
> > printf "%-36s%8d %-15s",uname
> > ,sessionid[forctr],substr(databases[sessionid[forctr]],1,15)
> > close("listusers")
> > cmd = "onstat -k | grep " useraddr[forctr]
> > dbtabflag = 0
> > printnull = 0
> > while(cmd | getline)
> > {
> > if($4 != 0)
> > {
> > if(tmptabname[$6])
> > tmptabs_locked[$6]=tmptabname[$6]
> > else
> > {
> > dbtabflag++
> > hextoint($6)
> >
> > select_tablename(databases[sessionid[forctr]])
> >
> > tabname_arr[res]=dbtab_arr[databases[sessionid[forctr]],res]
> > }
> > }
> > }
> > close(cmd)
> > ctrcmd2=1
> > if(dbtabflag)
> > {
> > for (tabname in tabname_arr)
> > {
> > if(ctrcmd2 == 1)
> > printf "%-18s",tabname_arr[tabname]
> > else
> > printf "%60c%-18s",32,tabname_arr
> > [tabname]
> > ctrcmd2++
> > printf "\\n"
> > printnull = 1
> > delete tabname_arr[tabname]
> > }
> > }
> > for(tmptabvar in tmptabs_locked)
> > {
> > if(ctrcmd2 == 1)
> > printf "(T) %-18s",tmptabs_locked
> > [tmptabvar]
> > else
> > printf "%60c(T)
> > %-18s",32,tmptabs_locked[tmptabvar]
> > ctrcmd2++
> > printf "\\n"
> > printnull = 1
> > delete tmptabs_locked[tmptabvar]
> > }
> > if (printnull == 0)
> > printf "Lock just released\\n"
> > }
> > }
> >
> > function hextoint(a)
> > {
> > res=0
> > base=16
> > expn=0
> > for(i=length(a);i0;i--)
> > {
> > val1=substr(a,i,1)
> > if(match(val1,"[Aa]"))
> > val1 = 10
> > if(match(val1,"[Bb]"))
> > val1 = 11
> > if(match(val1,"[Cc]"))
> > val1 = 12
> >