Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am writing a shell script to start my network in my virtual machine on boot, since it does not do this right away for some reason with a snapshot of a virtual machine.

Since the eth device starts as being down, I have to get that device name with the following script and then start the device as well:

gateway=ifconfig -a | awk '/eth/ {print $1}'
dhclient $gateway

However I keep getting the following error, line 1: -a: command not found. ifconfig -a works however from the command line.

Is there a way to get ifconfig -a to work in my shell script?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

You must use command substitution, otherwise, bash will think you assigned ifconfig result to variable gateway then run command -a:

gateway=$(ifconfig -a | awk '/eth/ {print $1}')
dhclient $gateway
share|improve this answer
    
thank you that works, I knew it had to do with how it was interpreting the script but couldn't pinpoint it. –  jgr208 Jul 7 at 16:14
    
The right single quote is wrongly placed. For some reason, stack exchange is too stupid to accept a minor edit. –  Pavel Šimerda Jul 8 at 5:01
    
Thanks, I updated my answer and OP question. –  Gnouc Jul 8 at 5:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.