Bash script issue

I can execute this command perfectly, with the output I want:

ifconfig eth0 | grep HWaddr | awk '{print $5}'

However, when I set the variable command and print the variable, I get an error message:

CASS_INTERNAL=`ifconfig eth0 | grep HWaddr | awk '{print \$5}'`
$CASS_INTERNAL

my internal xxx ip: command not found

The strange thing is - my internal IP really appears. How can I do without mistakes? It doesn't matter, but I'm using the latest version of Ubuntu.

+3
source share
5 answers

Well, first you use grep for HWaddr, so the fifth field on this line is the MAC address of the corresponding network adapter — not your local IP address.

, , , , eth0 , , .

, , , . bash eval:

#put the desired command in variable
CASS_INTERNAL='ifconfig eth0 | grep HWaddr | awk "{print \$5}"'

# ....

#at later on - evaluate its contents!
eval $CASS_INTERNAL
11:22:33:aa:bb:cc
+1
CASS_INTERNAL=`ifconfig eth0 | grep HWaddr | awk '{print \$5}'`
echo $CASS_INTERNAL

$CASS_INTERNAL

.

+4

, .

echo "$CASS_INTERNAL"

( .)

A more advanced shell note: in this case it does not matter, but in general it echocan have problems with some special characters ( -and \\), so it is better to use the following more complex, but completely reliable command:

printf "%s\n" "$CASS_INTERNAL"
+4
source

no need to use grep

ifconfig eth0 | awk '/HWaddr/{print $5}'
+4
source

You might want

echo $CASS_INTERNAL
+1
source

Source: https://habr.com/ru/post/1755625/


All Articles