Re: Usage of ECHO and grep commands (855 Views)
Reply
Advisor
sammer
Posts: 24
Registered: ‎10-12-2008
Message 1 of 13 (852 Views)

Usage of ECHO and grep commands

Hi,

set -A TELNET_HOST_ARR ${telnet_host_str}

The content present in telnet_host_str :-==========================================
alden 15.146.230.119 harpo 15.146.230.90 15.146.230.205

and target_system=alden

1>I found ipaddress by below way..Please let me know if you have any better idea.

ipaddress=$(nslookup ${target_system} |grep -p ${target_system} | grep Address | awk '{print $2}')

2>My doubt is in telnet_host_str eventhough i give ipaddress or target_system content "$? " status should be equal to zero.
echo ${telnet_host_str} || (grep $target_system && grep $ipaddress)
if [[ $? -eq 0 ]]; then

I did as above.Please let me know if you have better idea.

Thanks,
Shammer
Honored Contributor
Kapil Jha
Posts: 1,478
Registered: ‎01-23-2006
Message 2 of 13 (852 Views)

Re: Usage of ECHO and grep commands

Are you using this for any scripting purpose or something else.
Why don you use netstat -in or nslookup.
BR,
Kapil+
I am in this small bowl, I wane see the real world......
Honored Contributor
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 3 of 13 (852 Views)

Re: Usage of ECHO and grep commands

For a pipeline, the shell's status ("$?")
value is set by the last command in the
pipeline. In this case, that's your "awk"
command, not the "grep".

You could check for a null string ("-n" or
"-z"), instead of checking the final status.

(There are ways of extracting the status from
a command in the middle of a pipeline, but
they tend to be messy, or to use features
which are available only in fancy new shells,
like "bash".)
Advisor
sammer
Posts: 24
Registered: ‎10-12-2008
Message 4 of 13 (852 Views)

Re: Usage of ECHO and grep commands

Hi,

ipaddress=$(nslookup ${target_system} |grep -p ${target_system} | grep Addre
ss | awk '{print $2}')
echo $ipaddress<======== here getting correct answer
echo ${telnet_host_str} || (grep $target_system && grep $ipaddress)
if [[ $? -eq 0 ]]; then
lat_server=""
if [[ ${lat_server} = "rcon" ]]
then
...........
..........
else
lat_port=""
lat_server=""
echo $ipaddress<========= here getting value=0

please let me know...

Thanks,
Shammer

Honored Contributor
Rob Leadbeater
Posts: 3,582
Registered: ‎08-14-2002
Message 5 of 13 (852 Views)

Re: Usage of ECHO and grep commands

Hi,

I've read your original post a couple of time now and am struggling to see what you're actually trying to do...

Could you clarify please.

Cheers

Rob
Advisor
sammer
Posts: 24
Registered: ‎10-12-2008
Message 6 of 13 (852 Views)

Re: Usage of ECHO and grep commands

Hi Rob,

My main intention is:-

set -A TELNET_HOST_ARR ${telnet_host_str}
where {telnet_host_str} contains ipaddress as well as hostnames of machines.
Current script is checking only if you mention hostnames of machines in telnet_host_str.
Current script:-echo ${telnet_host_str} | (grep $target_system)
if [[ $? -eq 0 ]]; then

So now i am enhancing the script to check for ipaddress as well as hostnames.

So i found ipaddress as below:-
ipaddress=$(nslookup ${target_system} |grep -p ${target_system} | grep Address | awk '{print $2}')

Checking the content of telnet_host_str :-
echo ${telnet_host_str} || (grep $target_system && grep $ipaddress)
if [[ $? -eq 0 ]]; then


Hope you understood.. Please let me know if you need further information..

Please let me know if you have better way of doing it..

Thanks,
Shammer

Honored Contributor
Rob Leadbeater
Posts: 3,582
Registered: ‎08-14-2002
Message 7 of 13 (852 Views)

Re: Usage of ECHO and grep commands

Hi,

Personally I'd look to use Perl rather than shell scripting as you are at the moment...

It should make things much more efficient.

Cheers,

Rob
Advisor
sammer
Posts: 24
Registered: ‎10-12-2008
Message 8 of 13 (852 Views)

Re: Usage of ECHO and grep commands

Hi Rob,

My team suggested to do this in Shell scripting .Please let me know if you have better way of doing this in shell scripting.

Also i dont have much knowledge in perl scripting.


Thanks,
Shammer.
Advisor
sammer
Posts: 24
Registered: ‎10-12-2008
Message 9 of 13 (852 Views)

Re: Usage of ECHO and grep commands

Hi,

target_system=groucho
ipaddress="15.146.230.119"
set -A TELNET_HOST_ARR ${telnet_host_str}

Below are the list of contents present in $(telnet_host_str}:-

alden baknblak beggar biddy billion bingo bobcat bogey bradford bulldogs cos curly cyberstorm diamond dimaggi
o diverdown drooper dry ds10-1 ds10-2 ds10-3 ds10-4 hyd hel lit ber bor car nitoxy es40-1 es40-2 es40-3 es40-4 fall fleegle frigate aramis geezer gehrig getwings harp highway holein1 karloff killington larry magilla madhattr porthos million athos moe moronica mullins 15.146.231.62 ogee phantoms porcupine puddinhead reptile rhinos rinse rocks scoobydoo seawitch sine whale shemp snorky spring standish stoogeshsg sugarloaf summer tan topsail toys twldee twldum vanhal1 vanhal2 vanitya veena viola voltage wash waterville whomade winter yodel toolbox hoho humphry rose fold stooges2hsg stowe 15.146.230.119 harpo 15.146.230.90 15.146.230.205 north south east west square circle triangle rectangle oval heart crescent convex


Now I want to grep for this ipaddress "15.146.230.119".


echo ${telnet_host_str} || (grep $target_system && grep $ipaddress)

Thanks,
Shammer
Acclaimed Contributor
Dennis Handly
Posts: 25,291
Registered: ‎03-06-2006
Message 10 of 13 (852 Views)

Re: Usage of ECHO and grep commands

What's different from this thread and your other one?
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1278970

>Now I want to grep for this ipaddress "15.146.230.119".
echo ${telnet_host_str} || (grep $target_system && grep $ipaddress)

If you want to check for BOTH being in the string you can use:
echo ${telnet_host_str} | grep $target_system | grep $ipaddress
Advisor
sammer
Posts: 24
Registered: ‎10-12-2008
Message 11 of 13 (855 Views)

Re: Usage of ECHO and grep commands

Hi Dennis,

I tried below way as you told,but i didnt get any output.

==========================================
set -A TELNET_HOST_ARR ${telnet_host_str}

telnet_host_str="alden porthos million athos moe moronica mullins 15.146.231.62 15.146.230.119"

target_system=groucho

ipaddress=$(nslookup ${target_system} |grep -p ${target_system} | grep Address | awk '{print $2}')

status=$(echo $telnet_host_str | grep $target_system | grep $ipaddress)

# echo $status

#
=========================================

Since i kept ipaddress value in telnet_host_str array so am expecting that ipaddress value as output in status variable.

If i keep target_system value in telnet_host_str array then i may expect that output in status variable..

So what i want is it should handle both situations apropriately..

So please help me in changing below code:-

status=$(echo $telnet_host_str | grep $target_system | grep $ipaddress)

Thanks,
Shammer


Acclaimed Contributor
Dennis Handly
Posts: 25,291
Registered: ‎03-06-2006
Message 12 of 13 (855 Views)

Re: Usage of ECHO and grep commands

>I tried below way as you told,but i didn't get any output.

Do you want AND or OR for the two conditions?
See your other thread for OR.

>should handle both situations appropriately.

It seems like you want OR.
Advisor
sammer
Posts: 24
Registered: ‎10-12-2008
Message 13 of 13 (855 Views)

Re: Usage of ECHO and grep commands

OK Thanks Dennis..
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.