0

I need to understand the below Script :

HEADER='CPU    pctUser    pctNice  pctSystem  pctIowait    pctIdle'

HEADERIZE="BEGIN {print \"$HEADER\"}"

PRINTF='{printf "%-3s  %9s  %9s  %9s  %9s  %9s\n", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}'

CMD='sar -P ALL 1 1'

FORMAT='{cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF}'

FILTER='/Average|Linux|^$|%/ {next} (NR==1) {next}'


$CMD | tee $TEE_DEST | $AWK "$HEADERIZE $FILTER $FORMAT $PRINTF"  header="$HEADER"

echo "Cmd = [$CMD];  | $AWK '$HEADERIZE $FILTER $FORMAT $PRINTF' header=\"$HEADER\"" >> $TEE_DEST

This script is actually used to find out cpu utilization (using sar) and print its values. I am find trouble in understanding the HEADER, HEADERIZE, PRINTF. How are these things working.

1 Answer 1

0

The person who wrote the script was simply trying to make it easier to see everything up top that drives the look of the output and the script's functionality. They seem to have failed :) to make it easier for you.

If you read about how awk works (google it or read man awk), you'll see it wants a sequence of pattern { statements ... }. (This is an over simplification.) The names like HEADER and HEADERIZE are shell variables whose values are assigned at the top of the script and whose values are used later where they are preceded by "$". See that $AWK "$HEADERIZE line? The script is going to run awk (presumably elsewhere the variable AWK is defined as something like awk or /usr/bin/awk or something like that). The parameters to awk will start with the value of the HEADERIZE variable as defined on the second non-blank line there. So the line will really read (assuming the AWK variable is defined to be awk):

awk "BEGIN {print "$HEADER"}" 

and so on.

If you want to see how this all works, substitute echo for awk or redefine the AWK variable to be echo using something like

export AWK=echo

and re-run the script. You'll see the command lines as they would have been passed to awk.

The reason there are two commands at the bottom appears to be so that the output file, whose name is the value of the variable TEE_DEST, will contain two entries as a result of this script's execution. One is from the first command where the output of the execution of a command line from the variable CMD will be stored in the file named $TEE_DEST and then passed to the awk command whose output will be displayed on the terminal. Then the second time simply uses echo to put the string Cmd = [$CMD] through the awk script, appending that script's output to the file $TEE_DEST as well.

You can find the value of each of a variable named VARIABLE easily by using

echo $VARIABLE

This allows you to run bits of the script by pasting them into your terminal window and examine the contents. This, I have found, is an excellent way to learn scripting.

If you want to set a variable's value, you do something like what I suggested above when I told you to overwrite the value of AWK with echo:

export VARIABLE=new value for the variable
6
  • Thanks a lot. Your description is really helpful. need some more help. And you were right awk is defined as below. AWK=awk Modified script. echo $HEADERIZE $CMD | $AWK "$HEADERIZE $FILTER $FORMAT $PRINTF" header="$HEADER" echo $HEADERIZE echo "Cmd = [$CMD]; | $AWK '$HEADERIZE $FILTER $FORMAT $PRINTF' header=\"$HEADER\""
    – user133617
    Commented Sep 28, 2015 at 17:25
  • Modified Script HEADER='CPU pctUser pctNice pctSystem pctIowait pctIdle' HEADERIZE="BEGIN {print \"$HEADER\"}" PRINTF='{printf "%-3s %9s %9s %9s %9s %9s\n", cpu, pctUser, pctNice, pctSystem, pctIowait, pctIdle}' CMD='sar -P ALL 1 1' FORMAT='{cpu=$3; pctUser=$4; pctNice=$5; pctSystem=$6; pctIowait=$7; pctIdle=$NF}' FILTER='/Average|Linux|^$|%/ {next} (NR==1) {next}' echo $PRINTF "Printf function" $CMD | echo "$HEADERIZE $FILTER $FORMAT $PRINTF" header="$HEADER" echo $HEADERIZE "after awk" echo "Cmd = [$CMD];" | awk '$HEADERIZE $FILTER $FORMAT $PRINTF' header=\"$HEADER\"
    – user133617
    Commented Sep 28, 2015 at 17:54
  • But this script is not printing the output of the sar command.
    – user133617
    Commented Sep 28, 2015 at 18:34
  • Hey Metalwheaties, Kindly advise :)
    – user133617
    Commented Sep 29, 2015 at 15:57
  • Could you edit that so it's readable please? I have only a mobile phone at the moment and it is completely incomprehensible because you didn't put it in as indented code. Thanks.
    – Alan Mimms
    Commented Sep 29, 2015 at 23:40

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.