shell / awk question

shell / awk question

Post by Eric van Wess » Wed, 17 Oct 1990 20:56:35



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?

Thanks, Eric

 
 
 

shell / awk question

Post by BURN » Sat, 20 Oct 1990 16:51:03



Quote:> 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


 
 
 

shell / awk question

Post by Roger Rohrba » Sun, 21 Oct 1990 09:48:59



Quote:> 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

Quote:} | 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
    }
'

--

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

 
 
 

1. sh & awk: Quoting Awk Syntax in Bourne shell?

Hi

I want to get the user's home directory from YP passwd.

  $ new_user=userfoo
  $ ypcat passwd | awk -F: "\$1 == \"$new_user\" { print \$6 }"
  /home/userfoo

And I want to assign it to a shell variable, but awk returns an syntax
error.

  $ new_user_home=`ypcat passwd | awk -F: "\$1 == \"$new_user\" { print
\$6 }"`
  awk: syntax error near line 1
  awk: bailing out near line 1

The point that I don't quite understand is how the pair of backticks
change the quoting in Bourne sh such that it becomes invalid syntax for
awk, since the command within the backticks works on the command-line
without the backticks.  What have the pair of backticks done?

BTW, with trial-and-error, I found a better quoting that works.

  $ new_user_home=`ypcat passwd | awk -F: '$1 == "'$new_user'" { print
$6 }'`
  $ echo $new_user_home
  /home/userfoo

But, I'm still curious why the previous example fails.  FYI, awk is from
/usr/bin/awk on Solaris 2.5.1, 2.6 & 2.7.

Thanks.  I appreciate your enlightenment.

Regards
Hon-Chi

Sent via Deja.com http://www.deja.com/
Before you buy.

2. Message Router VMSmail Gateway nondelivery notification

3. shell - sed - awk question

4. SuSE 2nd nic not setting up

5. A simple question regarding shell scripts and awk

6. Printing with HP LaserJet 6L

7. korn shell (ksh) awk question

8. Linux on Gateway 2000?? Problems

9. Shell script question (awk || nawk)...

10. Basic shell/sed/awk question - float truncation and comparison

11. Passing shell var's into awk - another question

12. Shell and Awk arguments question

13. Question about using AWK in a BOURNE shell script