After research, Im not certain this is possible in a bash script. The most likely reason ive found is due to bash's order of shell expansions. (maybe?)
What i would like to happen,
Is the ability to enable/disable redirection (>) of a statement via a prior variable.
for example: the printf output below should be (redirected >) based upon what the variable $SendGrafana contains.
However this example code does not work:
SendGrafana=" > /dev/tcp/192.168.1.242/5062"
awk -v vars="$vars" 'BEGIN{printf "output.api %s \n", vars;}' $SendGrafana
i was hoping the code above would work just like this:
awk -v vars="$vars" 'BEGIN{printf "output.api %s \n", vars;}' > /dev/tcp/192.168.1.242/5062
Additionally- When i run the script with set -x (for debugging), i see that single quotes (') are being wrapped around just the redirect (>) , which i assume is the problem, as nothing gets sent to my netcat listener. I have read that this is only for user readability when using set -x, however the problem remains.
Here is the relevant output with set -x enabled:
+ awk -v vars=3 'BEGIN{printf "output.api %s \n", vars;}' '>' /dev/tcp/192.168.1.242/5062
(note the '>' )
I have tried many methods, both at the variable declaration as well as at the variable expansion, but all fail to redirect the output to 192.168.1.242:5062 when script is called. Thank you
EDIT BELOW- (adding this part which was in my comments, as i was trying to keep the question brief, but was suggested it should be here):
To add some more context:
the reason im even using this $SendGrafana variable, as opposed to no variable and literally > /dev/tcp/192.168.1.242/5062 after each awk statement- is so i can easily set $SendGrafana= " " , and my script will output to STDOUT (and not send to 192.168.1.242 via > /dev/tcp/192.168.1.242/5062). I need the output on STDOUT ONLY during debugging / test adding new statements.