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.
is i have many (30x +) of these awk -v vars=... and i would like the ability to easily turn off the "> /dev/tcp/192.168.1.242/5062" parts, for when im debugging / adding new awk statements.
IE i can easily set $SendGrafana= " " , and the script will output to STDOUT (and not send to 192.168.1.242) which is what id like ONLY during debugging / adding new statements. tks – James Gaul Aug 19 '20 at 21:10
-x
and you can see additional single quotes, that single quotes neither break your strings nor they actually exist - that is just to illustrate you how the script see your strings (for example whitespaces causes word splitting) in your case the '>' is just a literal character and doesn't get evaluated that way you assume – alecxs Aug 19 '20 at 21:26>
in a variable and expect it to be identified as a redirection operator after expansion. Data may go in variables, not syntax. – Quasímodo Aug 19 '20 at 21:37>
from string) you can pipe your command through a function fortee
(with a little if then else decision on $SendGrafana) that would keep it close to the desired suffix syntax – alecxs Aug 19 '20 at 23:27