0

I'm trying to get stored in a variable the output of a command chain with awk, but the result is incomplete,

this is what im trying to do.

nameserver="nas_server -list all |  awk '{print $6}'"
eval $nameserver
nameserverreal=$nameserver |awk '/encap|nameserver_/{  print }'
eval $nameserverreal

I'm using this command nas_server and with awk print 6, only get some output from my command nas_server.

What i need is to filter later the output only with "nameserver_" and storage the output in a variable, so i can print it, and use it later convined with other commands.

Cœur
  • 37,241
  • 25
  • 195
  • 267
A.lacorazza
  • 117
  • 1
  • 2
  • 8

1 Answers1

2

You appear to be confusing storing the output of commands with storing the text of the commands themselves (which is almost always a bad idea). I'm not sure exactly what you're trying to do (or what the output of nas_server -list all looks like), but I suspect you want something like this:

nameserver="$(nas_server -list all |  awk '{print $6}')"   # $() captures the output of a command
echo "$nameserver"  # Double-quote all variable references to avoid parsing weirdness!
nameserverreal="$(echo "$nameserver" |awk '/encap|nameserver_/{  print }')"
echo "$nameserverreal"

Here's a simplified version:

nameserverreal="$(nas_server -list all |  awk '$6 ~ /encap|nameserver_/ {print $6}'"

Oh, and anytime you're tempted to use eval in a shell script, it's a sign that something has gone horribly wrong.

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
  • Thank you for your reply, i thinked the same, but i saw in this answer that they are using eval https://stackoverflow.com/questions/5615717/how-to-store-a-command-in-a-variable-in-linux so i try it. . Te man idea is to capture the output and then filter only with the sarvername. Anyway, thanks a lot Gordon – A.lacorazza Aug 22 '17 at 13:21