[comp.unix.shell] shell / awk question

emwessel@praxis.cs.ruu.nl (Eric van Wessel) (10/16/90)

quota | awk 'BEGIN { FS=" " } { quot=$2 }
	     END { printf "%d\n",quot } '
				  ^^
				  How to make quot known here ?
							  |
							  |
							  |
							  |
du -s $HOME |						  |
awk 'BEGIN { FS=" " } { printf "\nDiskspace\n";		  |
			printf "---------\n";		  |
			used=($1)/2;			  |
			free=quot-used }  <----------------
     END   { printf "Used : %d kb. (%d%%)\n",used,used/(quot/100) ;
	     if (free>=0)
	       printf "Free : %d kb.\n\n",free
	     else printf "Too much : %d kb.\n\n",-free } '




Does anyone know?
Please e-mail ( emwessel@praxis.cs.ruu.nl )

Thanks, Eric

gt0178a@prism.gatech.EDU (BURNS) (10/19/90)

in article <4057@ruuinf.cs.ruu.nl>, emwessel@praxis.cs.ruu.nl (Eric van Wessel) says:
> quota | awk 'BEGIN { FS=" " } { quot=$2 }
> 	     END { printf "%d\n",quot } '
> 				  ^^
> 				  How to make quot known here ? [etc]

The following is a rework of your script. Since my quota(1) reports in the
format:

Your disk usage is approximately       788,480 bytes;
your disk quota is                   2,048,000 bytes.

and my du reports in 1k block sizes, there are minor cosmetic changes,
besides the eval and proper quoting tricks. If your quota(1) supports the
'-qv' option (see man excerpts following script), the first assignment can
be replaced w/the second commented one.

eval QUOTA=`quota | awk 'BEGIN { FS=" " } { quot=$5 }
	     END { print quot }' | sed 's/,//g'`
#eval QUOTA=`quota -qv`
du -s $HOME |
awk 'BEGIN { FS=" " } { printf "\nDiskspace\n";
			printf "---------\n";
			used=$1;
			free='$QUOTA'/1024-used }
     END   { printf "Used : %d kb. (%d%%)\n",used,used*100/('$QUOTA'/1024) ;
	     if (free>=0)
	       printf "Free : %d kb.\n\n",free
	     else printf "Too much : %d kb.\n\n",-free } '

QUOTA(1)         Georgia Institute of Technology         QUOTA(1)



NAME
     quota - display PRISM disk storage usage and maximum limit

SYNOPSIS
     quota [ -q ] [ -u ] [ -v ]

DESCRIPTION
     [...]
OPTIONS
     -q   Display only maximum limit.

     -u   Display only usage.

     -v   Display usage and/or maximum limit value(s) without
          title(s) and without comma formatting the numeric
          value(s).  This is useful for manipulating the value(s)
          in a shell script.

     By default, display usage and then maximum limit, both with
     titles and comma formatting the numeric values.

EXAMPLES
     Here's how to display your disk storage usage and maximum
     limit:
          {hydra:gtuser:22} quota
          Your disk usage is approximately     1,237,792 bytes;
          your disk quota is                   2,048,000 bytes.
          {hydra:gtuser:23}

     Here's how to get your maximum limit into a shell variable
     (assuming your shell is ksh):
          {hydra:gtuser:24} quota=`quota -qv`
          {hydra:gtuser:25} echo $quota
          2048000
          {hydra:gtuser:26}
     [...]
-- 
BURNS,JIM
Georgia Institute of Technology, Box 30178, Atlanta Georgia, 30332
uucp:	  ...!{decvax,hplabs,ncar,purdue,rutgers}!gatech!prism!gt0178a
Internet: gt0178a@prism.gatech.edu

roger@wrs.com (Roger Rohrbach) (10/20/90)

emwessel@praxis.cs.ruu.nl (Eric van Wessel) writes:


> quota | awk 'BEGIN { FS=" " } { quot=$2 }
> 	     END { printf "%d\n",quot } '
> 				  ^^
> 				  How to make quot known here ?
> 							  |
> 							  |
> 							  |
> 							  |
> du -s $HOME |						  |
> awk 'BEGIN { FS=" " } { printf "\nDiskspace\n";	  |
> 			printf "---------\n";		  |
> 			used=($1)/2;			  |
> 			free=quot-used }  <----------------
>      END   { printf "Used : %d kb. (%d%%)\n",used,used/(quot/100) ;
> 	     if (free>=0)
> 	       printf "Free : %d kb.\n\n",free
> 	     else printf "Too much : %d kb.\n\n",-free } '


  (I'm following up as well as replying by mail because I think this
is of general interest and obscure enough to be educational.)

  A general method for integrating two awk programs that read different
data streams is to identify unique characteristics of the two streams,
and then to read them both at once, guarding the actions in the single
awk program with conditions that ensure that the appropriate rules for 
each stream are executed only for input records read therefrom.

  For instance, in the example above, the guard for the output of the
"quota" output stream might be

    $2 ~ /^[1-9][0-9]*$/

and that for the "du -s" output stream might be

    index($2, "/" == 1)

so that the following (Bourne) shell program gives the desired result:

{
    quota
    du -s $HOME
} | awk '

    $2 ~ /^[1-9][0-9]*$/ {
        quot=$2
    }

    index($2, "/" == 1) {
        printf "\nDiskspace\n";
        printf "---------\n";
        used=($1)/2;
        free=quot-used
    }

     END {
	printf "%d\n", quot;
	printf "Used : %d kb. (%d%%)\n", used, used/(quot/100);

	if (free >= 0)
	    printf "Free : %d kb.\n\n", free
	else
	    printf "Too much : %d kb.\n\n", -free
    }
'

-- 
Roger Rohrbach                                  sun!wrs!roger    roger@wrs.com
- Eddie sez: ----------------------------------------------- (c) 1986, 1990 -.
|   {o >o                                                                     |
|    \<>) "If I were a gas, I'd be inert!"                                    |