0

Im having trouble calling a variable that should bring out the output of a command.

#!/bin/bash

ipAddresses = 'ifconfig | awk -v OFS=": " -v RS= '$1!="lo" && split($0, a, /inet addr:/) > 1{sub(/ .*/, "", a[2]); print $1, a[2]}''

echo -e "Sus direcciones IP son: \n " $(ipAddresses)

Appreciating any advice

nbari
  • 25,603
  • 10
  • 76
  • 131
  • What is the result/error you are getting? Also what OS are you running this on? – artdanil Apr 25 '18 at 18:39
  • This question is development of previous question by the same author: [Printing with multiple delimiters](https://stackoverflow.com/q/50007931/214178). – artdanil Apr 25 '18 at 19:03

3 Answers3

0

Replace the final $(ipAddresses) with ${ipAddresses} or just "$ipAddresses", but also save the output of your command using $().

Check Difference between ${} and $() in Bash.

A basic example:

#!/bin/sh

OUTPUT=$(uname -a)

echo "The output: $OUTPUT"
nbari
  • 25,603
  • 10
  • 76
  • 131
  • still not working :/ – Andres Julia Apr 25 '18 at 18:34
  • what is the error, output? check the basic example and try to see if that works – nbari Apr 25 '18 at 18:38
  • Argh, I'm sorry, I just realized you were only referring to the final `$(ipAddresses)`. There's still no need for the brace format, but I see where you're coming from now. If you approve my edit, I can remove the downvote. It'd be great if you could address some of the other issues too though! – terdon Apr 25 '18 at 19:04
0

Variable assignments cannot have space around the = in the shell. Also, you don't want single quotes there, you want either backticks or $(). The single quotes should only be for your awk command. Your awk is needlessly complicated as well, and you are using command substitution ($()) when printing, but ipAdresses is a variable, not a command.

Try something like this:

#!/bin/bash

ipAddresses=$(ifconfig | sed 's/^ *//' | awk -F'[: ]' '/^ *inet addr:/{print $3}')
printf 'Sus direcciones IP son:\n%s\n' "$ipAddresses"

But that is really not portable. You didn't mention your OS, but I am assuming it's a Linux and the output suggests Ubuntu (I don't have addr after inet in the output of ifconfig on my Arch, for example).

If you are running Linux, you could use grep instead:

ipAddresses=$(ifconfig | grep -oP 'inet addr:\K\S+')

ip is generally replacing ifconfig, so try this instead:

ipAddresses=$(ip addr | awk '/inet /{print $2}')

or

ipAddresses=$(ip addr | grep -oP 'inet \K\S+')

Or, to remove the trailing /N:

ipAddresses=$(ip addr | grep -oP 'inet \K[\d.]+')

And you don't need the variable anyway, you can just:

printf 'Sus direcciones IP son:\n%s\n' "$(ip addr | awk '/inet /{print $2}')"
terdon
  • 3,260
  • 5
  • 33
  • 57
0

I am not sure about your intention, since they are not stated, so I am trying to guess them from the script.

Option 1: you are trying to get IP address to into the variable ipAddresses and that is not happenning.

Start by changing single quotes around the long command and debug the command.

Option 2: you are storing a command in variable ipAddresses that you want to execute on the second line.

For both of the options you need to use the the value of the variable through $ipAdresses on the second line. Also fix the assignment to following formart:

varName="value" # Note no spaces around = sign
artdanil
  • 4,952
  • 2
  • 32
  • 49