Register
It is currently Mon Nov 24, 2014 12:52 am

Beginner with SED


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Mon Mar 01, 2010 7:48 am   

Joined: Mon Mar 01, 2010 7:43 am
Posts: 3
Hey guys, first up I made this based on examples from the inet. But I'm not getting any output at all.

my goal's are:
1. select adapter
2. set adaptername to $int1
3. set HWaddr, mac address to a $, no idea how...


Code:
intlist=ifconfig |grep HWaddr
ELM=${#intlist[@]}

# select inet adapter
echo "Select your inet conn. [#]"
for ((i=0;i<$ELM;i++)); do
  echo $i. ${intlist[${i}]}
done
read item
int1=${intlist[${item}]}


Top
 Profile  
 PostPosted: Thu Apr 08, 2010 2:49 pm   

Joined: Thu Apr 08, 2010 2:12 pm
Posts: 7
Hi, I don't know if I got what you want. I think it is after giving an interface name the script saves this name and the associated mac address. Is that true?
Here is an example of a script that does that.

Code:
#!/bin/bash

ifconfig -a |cut -f1 -d' '|grep  -v '^$' > /tmp/net.$$  # Put all inet names in a tmp file.

echo -n "Choose your device:"
read device

grep -q  "$device" /tmp/net.$$  #Test if there is such a device
test1=$?;

if [ $test1 -eq 0 ];then

ifconfig $device |grep -q HW # Test if this device has some Hardware address
test2=$?

if [ $test2 -eq 0 ];then
mac=`ifconfig $device |grep HW|cut -f2 -d'W'`         # Get the mac
echo "The mac address of the interface $device is $mac"
else
echo The device $device has no mac address
fi
else
echo "There is no such interface \"$device\"" >&2  #Send it to the error output
rm -f /tmp/net.$$                  # removes the file before exit
exit 1
fi
rm -f /tmp/net.$$                  # removes the file


Some corrections about your script:
IN
Code:
intlist=ifconfig |grep HWaddr

the command must be between the accent `
Code:
intlist=`ifconfig |grep HWaddr`

or
Code:
intlist=$(ifconfig |grep HWaddr)

also works


Top
 Profile  
 PostPosted: Fri Apr 09, 2010 1:42 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
This will also give the appropriate answer:
Code:
workstation:~ # ifconfig eth0 | grep HWaddr|awk {'print $5'}
00:0C:29:42:64:1E


Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Fri Apr 09, 2010 8:53 am   

Joined: Thu Apr 08, 2010 2:12 pm
Posts: 7
Good solution, but I need to put the quotes out of the braces, like that
Code:
ifconfig eth0 |grep -i hw | awk '{ print $7 }'

and the number of the field was 7 insted of 5. But the core idea is perfect!


Top
 Profile  
 PostPosted: Mon Apr 12, 2010 12:52 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Probably depends on your version of ifconfig...
Code:
workstation:/home/fredrik # cat /etc/SuSE-release
openSUSE 11.2 (i586)
VERSION = 11.2
workstation:/home/fredrik # ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:42:64:1E 


Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Mon Apr 12, 2010 6:38 am   

Joined: Thu Apr 08, 2010 2:12 pm
Posts: 7
Yes, that is true. The point is that I used LANG=pt_br. After changing to en_us I got the right field using the $5 argument.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP