The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Array inside an array manas_ranjan UNIX for Advanced & Expert Users 5 06-10-2008 02:25 PM
array ccp Shell Programming and Scripting 3 02-26-2008 03:19 AM
I need help with an array! djsal Shell Programming and Scripting 1 11-28-2007 11:26 AM
create array holding characters from sring then echo array. rorey_breaker Shell Programming and Scripting 5 09-28-2007 08:42 AM
array in awk asal_email2 Shell Programming and Scripting 5 06-13-2005 07:25 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-19-2005
TheCrunge TheCrunge is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 30
Do I need an array here?

I am still really foggy on how to make an array work for me....

I need to take the results of one command, and split it, then do another command on each result. For example:

Code:

# list all interfaces that are "up"
ifconfig -l -u

# Let's say I get this as a result:
lo0 en0 en1

#I need to then do this (for any item listed from the previous command):
ipconfig getifaddr lo0
ipconfig getifaddr en0
ipconfig getifaddr en1

# And I need each interface listed separately when all is said and done. Like this:
lo0 127.0.0.1
en0 10.0.0.1
en1 23.45.67.89
Thanks!
  #2 (permalink)  
Old 04-19-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
why do you need arrays?
you can do it with a loop - something like this - not tested:
Code:
#!/bin/ksh

ifconfig -l -u | while read if
do
    echo "${if} $(ipconfig getifaddr ${if})"
done
or [depending of your 'ifconfig' output]:

Code:
#!/bin/ksh

for if in $(ifconfig -l -u)
do
    echo "${if} $(ipconfig getifaddr ${if})"
done

Last edited by vgersh99; 04-19-2005 at 02:48 PM..
  #3 (permalink)  
Old 04-19-2005
TheCrunge TheCrunge is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 30
OK, the second one works, except I can't seem to getifaddr on lo0, I need to be able to list all interfaces, except lo0

Oh, and how can I get each interface listed on it's own line?

Thanks!
  #4 (permalink)  
Old 04-19-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by TheCrunge
OK, the second one works, except I can't seem to getifaddr on lo0, I need to be able to list all interfaces, except lo0

Thanks!
Code:
#!/bin/ksh

for if in $(ifconfig -l -u | grep 'lo0')
do
    echo "${if} $(ipconfig getifaddr ${if})"
done
Quote:
Originally Posted by TheCrunge
Oh, and how can I get each interface listed on it's own line?
doesn't it get listed now?
[my ifconfig is different - on Solaris - you'll have provide output of every command in your environment]
  #5 (permalink)  
Old 04-19-2005
TheCrunge TheCrunge is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 30
Yep, the grep (or grep -v) doesn't work, not sure why.

As far as being on their own line, they are.

The only problem is however, that I was trying to put the whole statment into a variable, like this:

Code:
interfaces=`(for if in $(ifconfig -l -u)
do
    echo "${if} $(ipconfig getifaddr ${if})"
done)`
I'm not too worried about getting rid of the lo0 if I can get each result into variables, so I can use each one separately.

Code:
# here's the result from your code:
get if addr lo0 failed, (os/kern) failure
lo0 
en0 205.139.21.89
en1 10.0.1.25

# Now I need to be able assign each line to a variable, so ultimately I get something like this:
Ethernet: 205.139.21.89
Airport: 10.0.1.25
  #6 (permalink)  
Old 04-19-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
do you have a file mapping 'en0' and '205.139.21.89' TO 'Ethernet'
'en1' and '10.0.1.25' TO 'Airport'

?

How would you do that mapping?
Closed Thread

Bookmarks

Tags
grep or

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:19 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0