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.
- 09-20-2005 #1Linux Guru
- Join Date
- May 2004
- Location
- forums.gentoo.org
- Posts
- 1,814
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
- 09-20-2005 #2
You can use:
Code:THISIP=$( /sbin/ifconfig eth0 | grep 'inet addr:' | gawk -F: '{ print $2 }' | gawk '{ print $1 }' )
- 09-20-2005 #3Linux Guru
- Join Date
- May 2004
- Location
- forums.gentoo.org
- Posts
- 1,814
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
- 09-20-2005 #4Linux Guru
- Join Date
- May 2004
- Location
- forums.gentoo.org
- Posts
- 1,814
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:
Cool.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 \
/IMHO
//got nothin'
///this use to look better
- 09-20-2005 #5Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
I'd have got THISIP by
Originally Posted by drakebasher
Why use 3 filters when 1 will do it all?Code:THISIP=$(/sbin/ifconfig ppp0 | sed -n '/inet addr:/s/[^:]*:\([^ ]*\).*/\1/p')
- 09-20-2005 #6
This is a *nix point of pride. There are many ways to get the same results.
- 09-20-2005 #7Linux Guru
- Join Date
- May 2004
- Location
- forums.gentoo.org
- Posts
- 1,814
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!
Originally Posted by anomie /IMHO
//got nothin'
///this use to look better
- 09-21-2005 #8Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
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 ([^:]*Code:sed -n '/inet addr:/s/[^:]*:\([^ ]*\).*/\1/p'
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.


Reply With Quote
