Find the answer to your Linux question:
Results 1 to 8 of 8
Trial and error is not real productive in programming. I want to execute the following line and assign it to the variable "THISIP" and I've tried everything I can think ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Guru
    Join Date
    May 2004
    Location
    forums.gentoo.org
    Posts
    1,817

    This is not homework!


    Trial and error is not real productive in programming. I want to execute the following line and assign it to the variable "THISIP" and I've tried everything I can think of to do it. Show me how smart you are:

    /sbin/ifconfig ppp0 | grep 'inet addr:' | gawk -F: '{ print $2 }' | gawk '{ print $1 }'

    Thanks-
    /IMHO
    //got nothin'
    ///this use to look better

  2. #2
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    You can use:
    Code:
    THISIP=$( /sbin/ifconfig eth0 | grep 'inet addr:' | gawk -F: '{ print $2 }' | gawk '{ print $1 }' )

  3. #3
    Linux Guru
    Join Date
    May 2004
    Location
    forums.gentoo.org
    Posts
    1,817
    Bananas for anomie!
    I would not have guessed that-
    Thanks

    (except change eth0 to ppp0...but I get it.)
    /IMHO
    //got nothin'
    ///this use to look better

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru
    Join Date
    May 2004
    Location
    forums.gentoo.org
    Posts
    1,817
    So now, after ssh-ing into my server I can run this script to dial-up the internet, display my IP to console, record my IP in a file for future reference and start my Firestarter firewall. Sure it's lame, but it works for now. Suggestions welcome:
    Code:
    #!/bin/bash
    /usr/sbin/usernetctl ifcfg-mailakanet1 up
    THISIP=$( /sbin/ifconfig ppp0 | grep 'inet addr:' | gawk -F: '{ print $2 }' | gawk '{ print $1 }' )
    echo "IP of ppp0: $THISIP"
    echo $THISIP >> ./theseIPs
    /etc/init.d/firestarter start
    echo \
    Cool.
    /IMHO
    //got nothin'
    ///this use to look better

  6. #5
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by drakebasher
    So now, after ssh-ing into my server I can run this script to dial-up the internet, display my IP to console, record my IP in a file for future reference and start my Firestarter firewall. Sure it's lame, but it works for now. Suggestions welcome:
    Code:
    #!/bin/bash
    /usr/sbin/usernetctl ifcfg-mailakanet1 up
    THISIP=$( /sbin/ifconfig ppp0 | grep 'inet addr:' | gawk -F: '{ print $2 }' | gawk '{ print $1 }' )
    echo "IP of ppp0: $THISIP"
    echo $THISIP >> ./theseIPs
    /etc/init.d/firestarter start
    echo \
    Cool.
    I'd have got THISIP by
    Code:
    THISIP=$(/sbin/ifconfig ppp0 | sed -n '/inet addr:/s/[^:]*:\([^ ]*\).*/\1/p')
    Why use 3 filters when 1 will do it all?

  7. #6
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    This is a *nix point of pride. There are many ways to get the same results.

  8. #7
    Linux Guru
    Join Date
    May 2004
    Location
    forums.gentoo.org
    Posts
    1,817
    Quote Originally Posted by anomie
    This is a *nix point of pride. There are many ways to get the same results.
    And a thing of beauty it is. Maybe within a couple of days I'll be able to decipher that gem from scm! Thanks to you both!
    /IMHO
    //got nothin'
    ///this use to look better

  9. #8
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Code:
    sed -n '/inet addr:/s/[^:]*:\([^ ]*\).*/\1/p'
    Here's how it's done: the -n tells sed not to output any lines by default - the p on the end of the command tells it to print the changed line(s). The first pattern (/inet addr:/) is the one that selects the lines which sed will apply the 's' command to. The 's' command will match (from the start of the line) zero or more occurrences of any character that isn't a colon ([^:]* followed by a colon, then it's saving zero or more occurrences of any character that isn't a space (\([^ ]*\)), followed by any character to the end of the line. What it's matched (the whole line) is then replaced by the first (and only in this case) saved pattern.

    So we've processed only the "inet addr" line, removed the start of the line up to the "inet addr:", and removed the end of the line from the space after the IP address. Simple, eh?

    I found when I was learning regular expressions that if I did simple things in a complicated way, I was ready for the complicated things because I already felt comfortable with the more tricky expressions.

Posting Permissions

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