0

Yes, this is related to Putting IP Address into bash variable. Is there a better way but nothing of the ideas there work for me on the microblaze uclinux. I wish to have my ip address of eth0 stored to a shell variable that I can write a script using it. I need alternative ideas how to do this.
ifconfig is available if that helps. I found that in the file /etc/config/dhcp0.conf the correct ip address is stored, here's the file's content:

1 192.168.10.102

How can I remove the 1 and space without using following commands

  • grep
  • sed
  • cut
  • this also does not work: echo ${variable:2}
Stefatronik
  • 312
  • 2
  • 16

2 Answers2

2

You can use the shell's read built-in:

read num ip </etc/config/dhcp0.conf

$num will contain the number at the beginning of the line, $ip will contain the IP.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Assign ifconfig output of eth0 to array

ifout=($(ifconfig eth0))

Strip off everything before the semicolon of the 6th element of array and assign it to the variable $ethip

ethip=${ifout[6]#*:}

  • Although @Barmar solution works I tried this but I am getting an error: `/var/tmp # ifout=($(ifconfig eth0))` `sh: syntax error at 'ifout='` – Stefatronik Dec 22 '17 at 19:31
  • It appears you are using sh not bash. What version of sh is it? 4.1 seems to work `sh-4.1# ifout=($(ifconfig eth0)) sh-4.1# ` – jracine Dec 22 '17 at 19:48
  • uuuh good question. it is on busybox 1.23. sh --version is an illegal option – Stefatronik Dec 22 '17 at 20:27