Operating System - Tru64 Unix
1753511 Members
5247 Online
108795 Solutions
New Discussion юеВ

Usage of ECHO and grep commands

 
sammer
Advisor

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
12 REPLIES 12
Kapil Jha
Honored Contributor

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......
Steven Schweda
Honored Contributor

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".)
sammer
Advisor

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

Rob Leadbeater
Honored Contributor

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
sammer
Advisor

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

Rob Leadbeater
Honored Contributor

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
sammer
Advisor

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.
sammer
Advisor

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
Dennis Handly
Acclaimed Contributor

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