Find the answer to your Linux question:
Results 1 to 7 of 7
Hi! I'm trying to order my outpu as I like but I have problems. I have this: Code: /sbin/ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2008
    Posts
    48

    Script to show IP's


    Hi!
    I'm trying to order my outpu as I like but I have problems.
    I have this:

    Code:
    /sbin/ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' > $LOG_TMP
    
    for i in $LOG_TMP; do
        IPS=`echo $i`
    done
    the result is:

    [5.5.5.9]
    [5.5.5.10]

    but I would like to see as:

    [5.5.5.9] [5.5.5.10]

    How can do this???

  2. #2
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    848
    How about:

    Code:
    /sbin/ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' > $LOG_TMP
    
    ips=""
    for i in $LOG_TMP; do
        ips="${ips} ${i}"
    done
    
    echo $ips
    Linux User #453176

  3. #3
    Just Joined!
    Join Date
    Oct 2008
    Posts
    48
    the output is the same as my script. I only change the last echo for a more

  4. #4
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Code:
    /sbin/ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' > $LOG_TMP
    
    ips=""
    while read i; do
        ips="${ips} ${i}"
    done < $LOG_TMP
    
    echo $ips
    Can't understand how

    for i in $LOG_TMP
    can produce

    [5.5.5.9]
    [5.5.5.10]
    or
    [5.5.5.9] [5.5.5.10]
    LOG_TMP is not a list of values.

  5. #5
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    848
    Quote Originally Posted by nmset View Post
    Code:
    /sbin/ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' > $LOG_TMP
    
    ips=""
    while read i; do
        ips="${ips} ${i}"
    done < $LOG_TMP
    
    echo $ips
    Can't understand how



    can produce


    LOG_TMP is not a list of values.
    It doesn't. That line just loops through each value in the list. The line:

    Code:
     ips="${ips} ${i}"
    Creates a variable containing all of the values in the list. So after the loop has complete the state of the application should be that:

    Code:
    ips=" [5.5.5.9]  [5.5.5.10]"
    Then the echo will print this to stdout
    Linux User #453176

  6. #6
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    848
    Okay, this works for me:

    Code:
    LOG_TMP=/tmp/test
    /sbin/ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}' > $LOG_TMP
    
    ips=""
    for i in `cat ${LOG_TMP}`; do
        ips="${ips} ${i}"
    done
    
    echo $ips
    Output:

    Code:
    kieren@George-Michael:~$ ifconfig
    eth0      Link encap:Ethernet  HWaddr 00:18:f3:0c:21:e1
              inet addr:192.168.1.9  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::218:f3ff:fe0c:21e1/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:196282 errors:0 dropped:0 overruns:0 frame:0
              TX packets:246967 errors:0 dropped:0 overruns:0 carrier:0
              collisions:3246 txqueuelen:1000
              RX bytes:95351045 (95.3 MB)  TX bytes:122916280 (122.9 MB)
              Interrupt:23 Base address:0xc800
    
    lo        Link encap:Local Loopback
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:16436  Metric:1
              RX packets:24 errors:0 dropped:0 overruns:0 frame:0
              TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0
              RX bytes:1240 (1.2 KB)  TX bytes:1240 (1.2 KB)
    
    wlan0     Link encap:Ethernet  HWaddr 00:1f:1f:01:93:38
              inet addr:192.168.1.11  Bcast:192.168.1.255  Mask:255.255.255.0
              inet6 addr: fe80::21f:1fff:fe01:9338/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:1571 errors:0 dropped:0 overruns:0 frame:0
              TX packets:24 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:347596 (347.5 KB)  TX bytes:5008 (5.0 KB)
    
    wmaster0  Link encap:UNSPEC  HWaddr 00-1F-1F-01-93-38-30-31-00-00-00-00-00-00-00-00
              UP RUNNING  MTU:0  Metric:1
              RX packets:0 errors:0 dropped:0 overruns:0 frame:0
              TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
    
    kieren@George-Michael:~$ ./test
    192.168.1.9 192.168.1.11
    Linux User #453176

  7. #7
    Just Joined!
    Join Date
    Oct 2008
    Posts
    48
    now it's work!

    very thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •