Last active
May 13, 2022 23:43
-
-
Save jhyland87/d88538f1af3a27e8518cfe61e025186e to your computer and use it in GitHub Desktop.
Parse the executed command, assessing all variables set via -v or --assign, adding the key/values to an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env awk -f | |
# | |
# THIS IS CURRENTLY BROKEN - im working on it. | |
# | |
# Description: Grabs the fully executed command by using the ps command to find the "args" data | |
# for the pid PROCINFO["pid"], then parses it by looking for any awk variables set | |
# using -v or --assign via the execution. | |
# | |
# Example Command: | |
# awk -v foo=bar --assign baz=quux -f ./get_vars-2.awk | |
# | |
# Expected Output: | |
# foo = bar | |
# baz = qux | |
# | |
# Example Command: | |
# awk \ | |
# -v var_1=val_1 \ | |
# -vvar_2=val_2 \ | |
# --assign var_3=val_3 \ | |
# --assign var_4=val_4 \ | |
# -f ./get_vars-2.awk \ | |
# -v var_5=val_5 \ | |
# -vvar_6=val_6 \ | |
# --assign var_7=val_7 \ | |
# --assign var_8=val_8 \ | |
# - \ | |
# -v var_9=val_9 \ | |
# -vvar_10=val_10 \ | |
# --assign var_11=val_11 \ | |
# --assign var_12=val_12 | |
# | |
# Or: | |
# awk -v var_1=val_1 -vvar_2=val_2 --assign var_3=val_3 --assign var_4=val_4 -f ./get_vars-2.awk -v var_5=val_5 -vvar_6=val_6 --assign var_7=val_7 --assign var_8=val_8 - -v var_9=val_9 -vvar_10=val_10 --assign var_11=val_11 --assign var_12=val_12 | |
# | |
# Expected Output: | |
# var_1 = val_1 | |
# var_2 = val_2 | |
# var_3 = val_3 | |
# var_4 = val_4 | |
# var_5 = val_5 | |
# var_6 = val_6 | |
# var_7 = val_7 | |
# var_8 = val_8 | |
# var_9 = val_9 | |
# var_10 = val_10 | |
# var_11 = val_11 | |
# var_12 = val_12 | |
# | |
function getexecuted(){ | |
("ps -p " PROCINFO["pid"] " -o args=") | getline exec_command | |
return exec_command | |
} | |
function list2assoc( arr ){ | |
for ( a_idx in arr ){ | |
arr[arr[a_idx]] = arr[a_idx] | |
delete arr[a_idx] | |
} | |
} | |
function _d(msg){ | |
printf("[DEBUG] %s\n", msg) | |
} | |
BEGIN { | |
cfg["var_prefixes"] = "-v --assign" | |
cli = getexecuted() | |
_d("Command Executed: "cli) | |
split( cli, cli_segs, " " ) | |
split( cfg["var_prefixes"], var_prefixes, " " ) | |
list2assoc( var_prefixes ) | |
for ( vp in var_prefixes) _d("var prefix: "vp) | |
argvars[""] = "" | |
last_seg = "" | |
for ( cs in cli_segs ) _d("[Pre-loop loop] cli_segs["cs"] = "cli_segs[cs]) | |
# Iterate over cli segments | |
for ( cs in cli_segs ){ | |
_d("--------") | |
_d("# NEW CLI SEG ITERATION (#"cs") ") | |
_d("===> A cli_segs["cs"] = "cli_segs[cs]) | |
_d("===> A last_seg = "last_seg) | |
_d("Processing segment #"cs" - "cli_segs[cs]" (last_seg = "last_seg")") | |
# If the "this=that" segment is found, then redefine varval_combo to said value | |
# to later be checked and parsed (reset to "" each time) | |
varval_combo = "" | |
# If the last seg was a var switch, then set varval_combo | |
if ( last_seg in var_prefixes ){ | |
_d("The last_seg ("last_seg") WAS found to be in var_prefixes - Setting varval_combo to "cli_segs[cs]) | |
varval_combo = cli_segs[cs] | |
_d("===> B cli_segs["cs"] = "cli_segs[cs]) | |
_d("===> B last_seg = "last_seg) | |
} | |
# Otherwise, see if the current segment has a switch set without a space (EG: -vfoo=bar) | |
else { | |
_d("===> C cli_segs["cs"] = "cli_segs[cs]) | |
_d("===> C last_seg = "last_seg) | |
_d("The last_seg ("last_seg") was NOT found to be in var_prefixes") | |
for ( vpfx in var_prefixes ){ | |
_d("===> D cli_segs["cs"] = "cli_segs[cs]) | |
_d("===> D last_seg = "last_seg) | |
# Modify the key, prepending a ^ | |
_p = sprintf( "^%s", var_prefixes[vpfx] ) | |
# If regex matches, then THIS is a var/key with the switch also | |
if ( cli_segs[cs] ~ _p ) | |
# If so, then set varval_combo | |
varval_combo = gsub( _p, "", cli_segs[cs] ) | |
} | |
_d("===> E cli_segs["cs"] = "cli_segs[cs]) | |
_d("===> E last_seg = "last_seg) | |
} | |
_d("Redefining last_seg from "last_seg" to "cli_segs[cs]) | |
last_seg = cli_segs[cs] | |
_d("last_seg: "last_seg) | |
if ( varval_combo != "" ){ | |
_d("varval_combo set to "varval_combo) | |
split( varval_combo, var_segs, "=" ) | |
_d("KEY: "var_segs[1]) | |
_d("VAL: "var_segs[2]) | |
argvars[ var_segs[1] ] = var_segs[2] | |
} | |
#_d("Redefining last_seg from "last_seg" to "cli_segs[cs]) | |
#last_seg = cli_segs[cs] | |
} | |
for ( av in argvars ){ | |
if ( av != "" ) | |
printf("argvar %s was %s\n", av, argvars[av]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment