Exercise #6: Set Variables in Scripts

by mike on February 17, 2011

When you need to set variables, you can do that in the BEGIN section of an awk script.  Here the variable firstuser is set to 1000.  The BEGIN section sets that variable and it is then used later in the script.

awk file: variable

BEGIN {firstuser=1000}

/1000/ {print;

print “UserID of the First User Created:  ” firstuser}

END {print “Total Number of Records:  ” NR}

The search for the text string “1000” does not need to be in single quotes nor does it need to be preceded by awk, since it was already invoked on the command line.  Notice that you can have multiple print options separated by a semicolon and have the braces enclose the whole sequence.  The variable “firstuser” is printed in the body of the script.  Note, the first user created on your Linux distribution may be 500 instead of 1000.

awk -f awkfile /etc/passwd

z4:x:1000:1000:z4,,,:/home/z4:/bin/bash

UserID of the First User Created:  1000

Total Number of Records:  32

You can also set string variables in the awk script.   This script illustrates both a numeric variable and a text string used as a variable, separated by a semicolon.

BEGIN {firstuser=1000; admin="fred"}

/1000/ {print;

print “UserID of the First User Created:  ” firstuser;

print “The administrator is: ” admin}

END {print “Total Number of Records:  ” NR}


awk -f awkfile /etc/passwd

z4:x:1000:1000:z4,,,:/home/z4:/bin/bash

UserID of the First User Created:  1000

The administrator is: fred

Total Number of Records:  32

You may need to set the field separator variable in the BEGIN section so you an process records correctly.  In this example the FS is set to “:” so that the fields in /etc/passwd can be processed.

BEGIN {FS=":"; firstuser=1000}

/1000/ {print;

print “UserID of the First User Created:  ” firstuser;

print “Number of Fields: “NF }

END {print “Total Number of Records:  ” NR}


awk -f awkfile /etc/passwd

z4:x:1000:1000:z4,,,:/home/z4:/bin/bash

UserID of the First User Created:  1000

Number of Fields: 7

Total Number of Records:  32

awk file: condition

A common mistake when you are working with text is to forget the new line.  In this example the “\n” has not been added to the printf so it creates a mess.

BEGIN {

}

{

if ($4 >= 1) $1 = “Watch this process”

if ($4 <= .9) $1 = “This process is OK”

printf “%s %10d %s”, $1,$2,$11

}

END {print “Processing Complete”}


awk -f condition processes

This process is OK          0 COMMANDThis process is OK          1 initThis process is OK      13638 /sbin/udevdThis process is OK      13880 syslogdThis process is OK      13901 /usr/sbin/sshdThis process is OK      13910 xinetdThis process is OK      13977 sendmail:Watch this process      14022 /usr/sbin/httpdThis process is OK      14023 /usr/sbin/httpdThis process is OK      14031 crondThis process is OK      14039 /usr/sbin/saslauthdThis process is OK      14040 /usr/sbin/saslauthdWatch this process      15857 sshd:This process is OK      15864 -bashThis process is OK      15969 psProcessing Complete

awk file: condition

There are several things accomplished with this script.  First you have the output formatted with the BEGIN and END section to add information to the output.  The print “\n” creates empty space so the output is not run together.  The conditions are used to determine the text that is output to the first field.  What you see is that the first field is replaced with the text string.  The printf is important to make sure that the output is readable with a text string “%s” followed by a decimal with 10 spaces “%10d” and finally the last text string “%s” followed by a new line “\n”.  Only 3 fields are printed, note the comma between the formatting with printf and the fields to be printed.

BEGIN {print “### CHECKING HIGH MEMORY USAGE PROCESSES ###”;

print “\n” }

{

if ($4 >= 1) $1 = “Watch this process”

if ($4 <= .9) $1 = “This process is OK”

printf “%s %10d %s\n”, $1,$2,$11

}

END {print “\n”;

print “### PROCESSING COMPLETE ###”}


awk -f condition processes

### CHECKING HIGH MEMORY USAGE PROCESSES ###

This process is OK          0 COMMAND
This process is OK          1 init
This process is OK      13638 /sbin/udevd
This process is OK      13880 syslogd
This process is OK      13901 /usr/sbin/sshd
This process is OK      13910 xinetd
This process is OK      13977 sendmail:
Watch this process      14022 /usr/sbin/httpd
This process is OK      14023 /usr/sbin/httpd
This process is OK      14031 crond
This process is OK      14039 /usr/sbin/saslauthd
This process is OK      14040 /usr/sbin/saslauthd
Watch this process      15857 sshd:
This process is OK      15864 -bash
This process is OK      15969 ps

### PROCESSING COMPLETE ###

Comments on this entry are closed.

Previous post:

Next post: