• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

script output not proper as expected

Hi Experts,

I want to create a script for 2 hardwares dell and HP ..so if the dell hardware then nic will be em1 etc then it greps em1 IP and if HP then grep eth0 etc..and display eth0 ip.

please help



/sbin/ifconfig > /tmp/testnic

grep -q "em1" /tmp/testnic

# if not then create it
if [ $? -ne 0 ]; then
        echo -e "

Address = `/sbin/ifconfig em1 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
"

else
        echo -e "


Address = `/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
"
fi

rm -rf /tmp/testnic
0
The Rock
Asked:
The Rock
1 Solution
 
woolmilkporcCommented:
I think your logic is wrong. You grep for em1 and try to display the address if not found. How should this work?

Try the following which is a bit shorter than your version:

if /sbin/ifconfig | grep -q em1; then
        echo -e "

Address = $(/sbin/ifconfig em1 | awk '/inet addr:/ {print substr($2,index($2,":")+1)}')
"

else
        echo -e "


Address = $(/sbin/ifconfig eth0 | awk '/inet addr:/ {print substr($2,index($2,":")+1)}')
"
fi

Open in new window

0
 
The RockAuthor Commented:
thanks got it
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now