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:
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.
Within awk I have to check if that option exists and set internal debug mode like this:
And that's all. Now I have a debug option usable on demand.
And for completeness here's the full awk script:
---
What is strange, that I made an error in the calling script:
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 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)
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:
Or the (bit dirty) Unix awk style
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:
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:
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)
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)
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)
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)
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)
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)
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)