Skip to main content
Corrected a typo
Source Link

Arpith'sslm's "pure awk" approach (see below) would fail on my BSD system. The split function into an array would not work as my 'ifconfig' command prints "127.0.0.1" and Arpith's "addr:127.0.0.1"...

Arpith's "pure awk" approach would fail on my BSD system. The split function into an array would not work as my 'ifconfig' prints "127.0.0.1" and Arpith's "addr:127.0.0.1".

slm's "pure awk" approach (see below) would fail on my BSD system. The split function into an array would not work as my 'ifconfig' command prints "127.0.0.1" and Arpith's "addr:127.0.0.1"...

Commented an answer by the OP himself.
Source Link

Pipe the result into sed to extract your only your IP addresses:

To write a portable script you would need to keep a kind of table with these numbers. On my system (BSD) yourthe IP addresses would be numbers 17 and 49 instead of your Linux numbers 7 and 47. Also Also the resulting strings look different (disregarding my local and yourArpith's global ID address):

echo ${array2[16]}
192.168.0.103

echo ${array2[48]}
127.0.0.1

Arpith's "pure awk" approach would fail on my BSD system. The split function into an array would not work as my 'ifconfig' prints "127.0.0.1" and Arpith's "addr:127.0.0.1".

read IPETH0 IPLO <<< $(ifconfig |\
 awk '/inet[[:space:]]/ { split($2,a,":"); print a[2]}')

Pipe the result into sed to extract your only IP addresses:

To write a portable script you would need to keep a kind of table with these numbers. On my system (BSD) your IP addresses would be numbers 17 and 49 instead of your Linux numbers 7 and 47. Also the resulting strings look different (disregarding my local and your global ID address):

echo ${array2[16]}
192.168.0.103

echo ${array2[48]}
127.0.0.1

Pipe the result into sed to extract only your IP addresses:

To write a portable script you would need to keep a kind of table with these numbers. On my system (BSD) the IP addresses would be numbers 17 and 49 instead of your Linux numbers 7 and 47. Also the resulting strings look different (disregarding my local and Arpith's global ID address):

echo ${array2[16]}
192.168.0.103

echo ${array2[48]}
127.0.0.1

Arpith's "pure awk" approach would fail on my BSD system. The split function into an array would not work as my 'ifconfig' prints "127.0.0.1" and Arpith's "addr:127.0.0.1".

read IPETH0 IPLO <<< $(ifconfig |\
 awk '/inet[[:space:]]/ { split($2,a,":"); print a[2]}')
Added another example
Source Link

I presume that you know for each of your commands which part of the command's return you want to store.

In your example this would be words number 7 and 47.

Do it like this (note the back ticks around your command ifconfig):

array=(`ifconfig`)

Show all elements of this array:

echo ${array[@]}

which gives you this result:

eth0 Link encap:Ethernet HWaddr 30:F7:0D:6D:34:CA inet addr:10.106.145.12 Bcast:10.106.145.255 Mask:255.255.255.0 inet6 addr: fe80::32f7:dff:fe6d:34ca/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1104666 errors:0 dropped:0 overruns:0 frame:0 TX packets:2171 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:444437904 (423.8 MiB) TX bytes:238380 (232.7 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.255.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:15900 errors:0 dropped:0 overruns:0 frame:0 TX packets:15900 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:467306 (456.3 KiB) TX bytes:467306 (456.3 KiB)

Show specific words:

echo ${array[6]} ${array[46]}
addr:10.106.145.12 addr:127.0.0.1

Pipe the result into sed to extract your only IP addresses:

echo ${array[6]} ${array[46]} | sed 's/addr://g'
10.106.145.12 127.0.0.1

Just use these echo calls as variables in your shell scriptUsing Arpith's nifty approach with 'read' here is the flexible answer.

read IPETH0 IPLO <<< $(echo ${array[6]} ${array[46]} |\
sed 's/addr://g')
echo $IPETH0 $IPLO
10.106.145.12 127.0.0.1

Please note that array elements are being counted from 0. Therefore your word number 7 would be referred to as "${array[6]}".

The array indexes are positive integers. So you can do all sorts of calculations in your shell script to pick specific words (like ranges or for-loops) ...

To write a portable script you would need to keep a kind of table with these numbers. On my system (BSD) your IP addresses would be numbers 17 and 49 instead of your Linux numbers 7 and 47. Also the resulting strings look different (disregarding my local and your global ID address):

echo ${array2[16]}
192.168.0.103

echo ${array2[48]}
127.0.0.1

HTH

bernie

I presume that you know for each of your commands which part of the command's return you want to store.

In your example this would be words number 7 and 47.

Do it like this (note the back ticks around your command ifconfig):

array=(`ifconfig`)

Show all elements of this array:

echo ${array[@]}

which gives you this result:

eth0 Link encap:Ethernet HWaddr 30:F7:0D:6D:34:CA inet addr:10.106.145.12 Bcast:10.106.145.255 Mask:255.255.255.0 inet6 addr: fe80::32f7:dff:fe6d:34ca/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1104666 errors:0 dropped:0 overruns:0 frame:0 TX packets:2171 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:444437904 (423.8 MiB) TX bytes:238380 (232.7 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.255.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:15900 errors:0 dropped:0 overruns:0 frame:0 TX packets:15900 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:467306 (456.3 KiB) TX bytes:467306 (456.3 KiB)

Show specific words:

echo ${array[6]} ${array[46]}
addr:10.106.145.12 addr:127.0.0.1

Pipe the result into sed to extract your IP addresses:

echo ${array[6]} ${array[46]} | sed 's/addr://g'
10.106.145.12 127.0.0.1

Just use these echo calls as variables in your shell script.

Please note that array elements are being counted from 0. Therefore your word number 7 would be referred to as "${array[6]}".

The array indexes are positive integers. So you can do all sorts of calculations in your shell script to pick specific words (like ranges or for-loops) ...

To write a portable script you would need to keep a kind of table with these numbers. On my system (BSD) your IP addresses would be numbers 17 and 49 instead of your Linux numbers 7 and 47. Also the resulting strings look different (disregarding my local and your global ID address):

echo ${array2[16]}
192.168.0.103

echo ${array2[48]}
127.0.0.1

HTH

bernie

I presume that you know for each of your commands which part of the command's return you want to store.

In your example this would be words number 7 and 47.

Do it like this (note the back ticks around your command ifconfig):

array=(`ifconfig`)

Show all elements of this array:

echo ${array[@]}
eth0 Link encap:Ethernet HWaddr 30:F7:0D:6D:34:CA inet addr:10.106.145.12 Bcast:10.106.145.255 Mask:255.255.255.0 inet6 addr: fe80::32f7:dff:fe6d:34ca/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1104666 errors:0 dropped:0 overruns:0 frame:0 TX packets:2171 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:444437904 (423.8 MiB) TX bytes:238380 (232.7 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.255.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:15900 errors:0 dropped:0 overruns:0 frame:0 TX packets:15900 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:467306 (456.3 KiB) TX bytes:467306 (456.3 KiB)

Show specific words:

echo ${array[6]} ${array[46]}
addr:10.106.145.12 addr:127.0.0.1

Pipe the result into sed to extract your only IP addresses:

echo ${array[6]} ${array[46]} | sed 's/addr://g'
10.106.145.12 127.0.0.1

Using Arpith's nifty approach with 'read' here is the flexible answer.

read IPETH0 IPLO <<< $(echo ${array[6]} ${array[46]} |\
sed 's/addr://g')
echo $IPETH0 $IPLO
10.106.145.12 127.0.0.1

Please note that array elements are being counted from 0. Therefore your word number 7 would be referred to as "${array[6]}".

The array indexes are positive integers. So you can do all sorts of calculations in your shell script to pick specific words (like ranges or for-loops) ...

To write a portable script you would need to keep a kind of table with these numbers. On my system (BSD) your IP addresses would be numbers 17 and 49 instead of your Linux numbers 7 and 47. Also the resulting strings look different (disregarding my local and your global ID address):

echo ${array2[16]}
192.168.0.103

echo ${array2[48]}
127.0.0.1

HTH

bernie

Added an example using sed. Updated my explanation.
Source Link
Loading
Source Link
Loading