[olug] Unix Tip: GET YOUR IP-ADDRESS (long explanation)
Daniel Pfile
daniel at pfile.net
Thu Mar 20 23:32:40 UTC 2003
Hmm ok... Half to be a smart ass, half to show people just why the
command line is cool... This does explain why that learning the bash
shell book is always near Vince. Please don't nitpick, this is kinda
hard to explain in english, that's why programing languages exist.
[ $# -gt 0 ]
If there are more than 0 positional parameters .
&& (
And this works:
while [ "$1" ];
Loop through the params
do ( [ -f $1 ]
Do this if the argument is a file
&& [ "$( file $1 | grep text)" ] )
The results of running the file command on the argument contains the
word 'text'
&& ( echo -e
and this echo, grep, wc, tr group of commands work
"$( grep -vE '^[[:space:]]*#|^[[:space:]]*$' $1
Print out anything in that file that doesn't either contain a comment:
'^[[:space:]]*# or a blank line: ^[[:space:]]*$
| wc -l
Count the number of lines in that output
| tr -d '[[:space:]]' )\t$1" );
Convert the output of spaces in wc -l into tabs.
shift;
Next please
done )
Done with the do.
|| echo "Usage: $0 <file> [file file file...]"
Since there were less than 0 parameters, show the user the usage of the
script.
Do I get a cookie Vince?
-- Daniel
On Thursday, March 20, 2003, at 04:20 PM, Vincent wrote:
> Would you provide a long explanation for this one-line script ? :)
>
> [ $# -gt 0 ] && ( while [ "$1" ]; do ( [ -f $1 ] && [ "$( file $1 |
> grep text)" ] ) && ( echo -e "$( grep -vE
> '^[[:space:]]*#|^[[:space:]]*$' $1 | wc -l | tr -d '[[:space:]]'
> )\t$1" ); shift; done ) || echo "Usage: $0 <file> [file file file
> ...]"
>
>
>
> ----- Original Message -----
> From: "Eric Penne" <epenne at olug.org>
> To: <olug at olug.org>
> Sent: Thursday, March 20, 2003 2:11 PM
> Subject: Re: [olug] Unix Tip: GET YOUR IP-ADDRESS (long explanation)
>
>
>> ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk '{print
>> $2;}' |
>> awk -F':' '{print $2;}'
>>
>>
>> Here is a rundown on this command for the newbies.
>>
>> 1) ifconfig is usually located in /sbin/ifconfig shows the network
>> interfaces that are up and how they are configured.
>>
>> 2) grep pulls lines out of the ifconfig info that contain inet addr
>>
>> 3) grep -v inverts. It prints all the lines that don't have 127.0.0.1
>> in them.
>>
>> 4) awk looks at the info and pulls the second field out, where the
>> fields are separated by whitespace by default.
>>
>> 5) Then awk -F':' is used again to pull the second field out, where
>> the
>> fields are separated by a colon.
>>
>> The individual outputs follow:
>>
>> 1)
>>
>> eth0 Link encap:Ethernet HWaddr 00:80:AD:0C:BA:43
>> inet addr:192.168.1.42 Bcast:192.168.255.255
>> Mask:255.255.0.0 UP BROADCAST RUNNING MULTICAST MTU:1500
>> Metric:1
>> RX packets:34866 errors:0 dropped:0 overruns:0 frame:0
>> TX packets:5882 errors:0 dropped:0 overruns:0 carrier:0
>> collisions:0 txqueuelen:100
>> RX bytes:7133322 (6.8 MiB) TX bytes:850473 (830.5 KiB)
>> Interrupt:3 Base address:0xd800
>>
>> lo Link encap:Local Loopback
>> inet addr:127.0.0.1 Mask:255.0.0.0
>> UP LOOPBACK RUNNING MTU:16436 Metric:1
>> RX packets:8 errors:0 dropped:0 overruns:0 frame:0
>> TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
>> collisions:0 txqueuelen:0
>> RX bytes:400 (400.0 b) TX bytes:400 (400.0 b)
>>
>> 2)
>> inet addr:192.168.1.42 Bcast:192.168.255.255
>> Mask:255.255.0.0 inet addr:127.0.0.1 Mask:255.0.0.0
>>
>> 3)
>> inet addr:192.168.1.42 Bcast:192.168.255.255
>> Mask:255.255.0.0
>>
>> 4)
>> addr:192.168.1.42
>>
>> 5)
>> 192.168.1.42
>>
>>
>> Now don't we all love the pipe command. Hopefully if you are having
>> trouble with the above command this explanation can step you through
>> the
>> issues.
>>
>> Eric
>>
>>
>>
>> _______________________________________________
>> OLUG mailing list
>> OLUG at olug.org
>> http://lists.olug.org/mailman/listinfo/olug
>>
>
> _______________________________________________
> OLUG mailing list
> OLUG at olug.org
> http://lists.olug.org/mailman/listinfo/olug
More information about the OLUG
mailing list