Note: environment variables within awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Note: environment variables within awk
# 1  
Old 02-07-2019
Note: environment variables within awk

This is not a question. Just a little note, because I've been here some time and never read about awk accessing environment variables. So here's my use case and demonstration of how to use the ENVIRON array. My operating environment is ubuntu 18.04 / docker / GNU awk 4.1.4. ENVIRON seems to posix compatible as written here: awk

I wrote an awk script to check the mysql output if the mysql replication is fine. The script just returns an exit code because that's what's needed for the container health check.

I have an awk script and a script calling awk that is generating the status text, which awk should parse:

This is the calling script:

Code:
#!/bin/sh

mysql -e 'SHOW SLAVE STATUS \G' | mysql_slave_check

But from time to time when I'm debugging, I want verbose output, to see details of the state. So I'd like to have an -v Option to get some verbose output. So since parsing command line arguments within awk seems more difficult to me, I put it into the calling script.
Code:
#!/bin/sh

test "$1" = "-v" && DEBUG="DEBUG=1"
mysql -e 'SHOW SLAVE STATUS \G' | mysql_slave_check $DEBUG

Within awk I have to check if that option exists and set internal debug mode like this:
Code:
BEGIN {
        DEBUG = (ENVIRON["DEBUG"]!="") ? ENVIRON["DEBUG"] : 0
}

And that's all. Now I have a debug option usable on demand.

And for completeness here's the full awk script:
Code:
#!/usr/bin/env awk -f
#
# check if the mariadb-slave is connected and synced with master
#
#       expected text from stdin is the following command
#
#               LC_ALL=C mysql -e 'SHOW SLAVE STATUS \G' 
#
#       what to check:
#
#               "Master_Host"           -> not empty
#               "Slave_IO_Running"      -> "yes"
#               "Slave_SQL_Running"     -> "yes"
#               "Seconds_Behind_Master" -> <= 30
#

function debug(msg) {if(DEBUG==1) { printf "%s",msg }}

/Master_Host: (.+)/                             { MASTER_HOST_SET=1                                     }
/Slave_IO_Running: Yes/                         { SLAVE_IO_RUNNING=1                                    }
/Slave_SQL_Running: Yes/                        { SLAVE_SQL_RUNNING=1                                   }
match($0,/Seconds_Behind_Master: ([0-9]+)/,res) { if(res[1] <= 30 ) { REPL_SECS=res[1];REPL_STATE_OK=1 }}

BEGIN {
        DEBUG = (ENVIRON["DEBUG"]!="") ? ENVIRON["DEBUG"] : 0
}

END {

        debug(sprintf("Master_Host is set       : %10s\n",(MASTER_HOST_SET   == 1)?"OK":"FAILED"))
        debug(sprintf("Slave IO Running         : %10s\n",(SLAVE_IO_RUNNING  == 1)?"OK":"FAILED"))
        debug(sprintf("Slave SQL Running        : %10s\n",(SLAVE_SQL_RUNNING == 1)?"OK":"FAILED"))
        debug(sprintf("Replication Lag          : %10s(%s secs behind)\n",(REPL_STATE_OK     == 1)?"OK":"FAILED", REPL_SECS))

        if ( MASTER_HOST_SET && SLAVE_IO_RUNNING && SLAVE_SQL_RUNNING && REPL_STATE_OK ) {
                GLOBAL_STATUS="PASSED"
        } else {
                GLOBAL_STATUS="FAILED"
        }
        debug(sprintf("Overall DB Slave status  : %10s\n",GLOBAL_STATUS))
        exit (GLOBAL_STATUS=="PASSED")?0:1
 }

---

What is strange, that I made an error in the calling script:

Code:
mysql -e 'SHOW SLAVE STATUS \G' | mysql_slave_check $DEBUG

This was an error since I wanted a variable to be set for the awk program call, but it has to be placed in front of the call not after. I wonder why this is working?

The correct call should be this one:


Code:
 test "$1" = "-v" && DEBUG=1 || DEBUG=0
 mysql -e 'SHOW SLAVE STATUS \G' | DEBUG=$DEBUG mysql_slave_check


Last edited by stomp; 02-07-2019 at 06:31 AM..
These 2 Users Gave Thanks to stomp For This Post:
# 2  
Old 02-15-2019
The DEBUG=$DEBUG ensures that DEBUG exists in the environment for the awk.
But it's pure overhead if it was already in the shell's environment.
You can achieve the same with an exported variable (exported = placed in the shell's environment)
Code:
export DEBUG
test "$1" = "-v" && DEBUG=1 || DEBUG=0
mysql -e 'SHOW SLAVE STATUS \G' | mysql_slave_check

Because DEBUG is in the shell's environment, all the run commands inherit it.

The use of ENVIRON[ ] in an awk script is interesting. Thanks for sharing!
It is not used often. Reason: when awk is run from the shell there is easy passing of variables.
The Posix style:
Code:
test "$1" = "-v" && DEBUG=1 || DEBUG=0
mysql -e 'SHOW SLAVE STATUS \G' | awk -v DEBUG=$DEBUG -f mysql_slave_check

Or the (bit dirty) Unix awk style
Code:
test "$1" = "-v" && DEBUG=1 || DEBUG=0
mysql -e 'SHOW SLAVE STATUS \G' | awk -f mysql_slave_check DEBUG=$DEBUG

Either invocation sets the DEBUG variable in awk, so there is no need for pulling it from ENVIRON[ ]
These 2 Users Gave Thanks to MadeInGermany For This Post:
# 3  
Old 02-17-2019
The most important thing for me was here to have a version of a command with an option that is as short as possible, so it's convenient to use it.

And on a second thought, I think environment variables are not such a good way to use in general. I appreciate explicit parameter handover more since external dependencies and data access is not hiden in the code but directly visible at a program call.

And I would better write such script in a single file now:

Code:
#!/bin/sh

[ "$1" = "-q" ] && DEBUG=0 || DEBUG=1
mysql -e 'SHOW SLAVE STATUS \G' | awk -v DEBUG=${DEBUG} '

#
# awk program here
#
...
'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Environment Variables

Hi All, I need to understand following three environment variables and their usages in HP Unix. _M_ARENA_OPTS _M_CACHE_OPTS PTHREAD_SCOPE_SYSTEM How does these environment variables influence multi threaded applciation and how do we decide the value of these variables? Is there... (0 Replies)
Discussion started by: angshuman
0 Replies

2. Homework & Coursework Questions

Environment Variables

1. The problem statement: What is the mesg value set for your environment? If it is on, how would you turn off your current session? How would you set it permanently? 3. The attempts at a solution : Read Unix The textbook. 3rd chapter has many things like environment variables and... (5 Replies)
Discussion started by: mahinkhan22
5 Replies

3. HP-UX

Environment Variables

Hi Experts, Need your help in understanding the commands to setup the environment variables in hp-ux. Beleive need to use either set,setenv or export. I am confused between above three options, when to use which option? On command line, I have tried both set and setenv but couldn't... (1 Reply)
Discussion started by: sai_2507
1 Replies

4. Shell Programming and Scripting

get xml note by awk

Hi all, I have a file contain 100 lines xml. Would like to get the note value of special attribute. Could anyone help? input: <a>1</a><b>2</b><c>3</c><d>4</d><e>5</e><f>64</f> <a>1</a><b>2</b><c>33</c><d>4</d><e>56</e><f>63</f> <a>1</a><b>2</b><c>66</c><d>4</d><e>58</e><f>62</f>... (3 Replies)
Discussion started by: mimilaw
3 Replies

5. UNIX for Dummies Questions & Answers

Environment variables

why are all environment variables represented in a fixed format regardless of the shell you use? like $HOME $PATH etc (6 Replies)
Discussion started by: sravani
6 Replies

6. Shell Programming and Scripting

using environment variables

say i define an environment variable in a particular script (upgrade.sh). my script is upgarde.sh and it calls another script try.sh. will this environment variable be accessible to try.sh also. if not how to I make environment variables global so that they can be used by any script. (2 Replies)
Discussion started by: lassimanji
2 Replies

7. Programming

environment variables

hi, I want to create a new EV(Environment Variable) through a c program and I done this thing through setenv() method. But the newly created EV is not permanent, i.e. when I exit from the program the EV also no longer lives. But I want to make it a permanent EV for the current user. Actually I... (6 Replies)
Discussion started by: sumsin
6 Replies

8. UNIX for Dummies Questions & Answers

environment variables

Hi Folks, Is it possible somehow to unset all the environment variables which have been defined before in UNIX (Solaris). Thanks, Slava (3 Replies)
Discussion started by: spavlov
3 Replies

9. Programming

environment variables

Hi! How-to get the environment variables in GNU. getenv() only fetches the ones that you can find under export (not the ones under declare)... best regars .David (2 Replies)
Discussion started by: Esaia
2 Replies

10. UNIX for Dummies Questions & Answers

what is the use of Environment variables

what is the actual use of environment variables. I know only PS1, LOGNAME, PS2 variables what are the other variables & what is there use (2 Replies)
Discussion started by: indianguru
2 Replies
Login or Register to Ask a Question