9

I have a command I'm using to get the hostname.localdomain:

dig axfr @dc1.localdomain.com localdomain.com | grep -i Lawler | awk '{ getline ; $1=substr($1,1,length($1)-1); print $1 ; exit }'

This nicely returns a result like:

michael.lawler.localdomain.com

I'd like to further use that result as a variable in a Bash script.

It seems I'm having trouble getting past the first pipe.

If I VAR="dig axfr @dc1.localdomain.com localdomain.com | grep -i Lawler | awk '{ getline ; $1=substr($1,1,length($1)-1); print $1 ; exit }'"

...I get back the entire zone transfer. I've also tried many minor changes, adding $ before the dig command, without quotes, but nothing seems to work. How can I fix this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
TryTryAgain
  • 7,632
  • 11
  • 46
  • 82
  • 1
    If your goal is to store the command to run it more than once, see http://mywiki.wooledge.org/BashFAQ/050 ("I'm trying to put a command in a variable, but the complex cases always fail!"). If your goal is only to store the output, `var=$(command)` – Charles Duffy Mar 06 '13 at 19:23
  • FYI -- you can probably ask `dig` to give you less information, to make the output easier to parse. See for instance the `+short` option. – Charles Duffy Mar 06 '13 at 19:25
  • @CharlesDuffy `awk '{ getline ; $1=substr($1,1,length($1)-1); print $1 ; exit }'` does what I need, `+short` actually doesn't work for my situation, not sure why..maybe because of OSX. Thanks for the suggestion though. – TryTryAgain Mar 06 '13 at 19:29
  • Both answers provided work as expected. Any thoughts on which is more susceptible to future complexity? I'd like to accept the one which provides the most forgiving flexibility. Thanks – TryTryAgain Mar 06 '13 at 19:31
  • possible duplicate of [How to execute a bash command stored as a string with quotes and asterisk](http://stackoverflow.com/questions/2005192/how-to-execute-a-bash-command-stored-as-a-string-with-quotes-and-asterisk) – Ciro Santilli OurBigBook.com Jul 05 '15 at 08:38

2 Answers2

8
VAR=$( dig axfr @dc1.localdomain.com localdomain.com |
     grep -i Lawler |
     awk '{ getline ; $1=substr($1,1,length($1)-1); print $1 ; exit }' )
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • 4
    Using `$()` is generally preferred over backticks as backticks do not nest. backticks are slightly more portable, but `$()` notation has been standard for about 20 years, and most shells accept it. – William Pursell Mar 06 '13 at 19:35
3

Use backtics instead of quotes:

VAR=`dig axfr @dc1.localdomain.com localdomain.com | grep -i Lawler | awk '{ getline ; $1=substr($1,1,length($1)-1); print $1 ; exit }'`

Backtics actually mean "run whatever is in here and return standard out as the expression's value", but quotes don't do that.

bchurchill
  • 1,410
  • 8
  • 23
  • 1
    Both do work well. I think this way is just easiest to remember and works well on many systems. As Pursell noted, it won't nest, but I don't think that's a problem in this case. You can nest $() inside the backtics though, so it's not like a dead-end. That said, I don't think there's really a clear winner. – bchurchill Mar 06 '13 at 19:38