Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

piped variable scope issue 1

Status
Not open for further replies.

SJSFoxPro

Technical User
Jun 19, 2003
110
US
We are converting some scripts from POSIX to BASH and have bumped into the piped variable scope issue when using a loop. I've read other posts and sites and have realized that I need to do a redirection in order to pass the variable into the loop and come out with the value. Here's the part of the script that checks the listener status:
# check LISTENER status
lsnr_status=0
lsnr_grep="notfound"
ping_owner=$(whoami)
ps -ef | grep /bin/tnslsnr | grep -v grep | while read LS_LINE
do
lsnr_grep="found"
lsnr_owner=$( echo "$LS_LINE" | awk '{print $1}')
lsnr_col=$(echo "$LS_LINE" | awk '{ print match($0,"tnslsnr") }')
lsnr_alias=$(echo "$LS_LINE" | cut -c $lsnr_col- | awk -F: 'BEGIN {FS=",[ \t]*|[ \t]+"} {print $2}')
if [ "$lsnr_owner+zzz" = "$ping_owner+zzz" ]
then
lsnrctl status $lsnr_alias > /dev/null 2>&1
lsnr_status=$?
echo "\n$lsnr_owner Listener \"$lsnr_alias\" status: $lsnr_status\n" >> $sh_log_file
else
echo "\nListener \"$lsnr_alias\" owned by: $lsnr_owner - status NOT checked.\n" >> $sh_log_file
fi
done
if [ $lsnr_grep = "notfound" ]
then
echo NO listener found! >> $sh_log_file
lsnr_status=1
fi

Mostly, I'm not sure how to use redirection on the line: ps -ef | grep /bin/tnslsnr | grep -v grep | while read LS_LINE

Any "direction" would be greatly appreciated!
 
Hi

Use here-document, here-string or process substitution. The here-document is the most portable one :
Code:
[navy]lsnr_status[/navy][teal]=[/teal][purple]0[/purple]
[navy]lsnr_grep[/navy][teal]=[/teal][green][i]"notfound"[/i][/green]
[navy]ping_owner[/navy][teal]=[/teal][navy]$(whoami)[/navy]

[b]while[/b] [b]read[/b] LS_LINE
[b]do[/b]
  [navy]lsnr_grep[/navy][teal]=[/teal][green][i]"found"[/i][/green]
  [navy]lsnr_owner[/navy][teal]=[/teal][navy]$([/navy] echo [green][i]"$LS_LINE"[/i][/green] [teal]|[/teal] awk [green][i]'{print $1}'[/i][/green][navy])[/navy]
  [navy]lsnr_col[/navy][teal]=[/teal][navy]$([/navy]echo [green][i]"$LS_LINE"[/i][/green] [teal]|[/teal] awk [green][i]'{ print match($0,"tnslsnr") }'[/i][/green][navy])[/navy]
  [navy]lsnr_alias[/navy][teal]=[/teal][navy]$([/navy]echo [green][i]"$LS_LINE"[/i][/green] [teal]|[/teal] cut -c [navy]$lsnr_col[/navy]- [teal]|[/teal] awk -F[teal]:[/teal] [green][i]'BEGIN {FS=",[ [/i][/green][lime][i]\t[/i][/lime][green][i]]*|[ [/i][/green][lime][i]\t[/i][/lime][green][i]]+"} {print $2}'[/i][/green][navy])[/navy]

  [b]if[/b] [teal][[/teal] [green][i]"$lsnr_owner+zzz"[/i][/green] [teal]=[/teal] [green][i]"$ping_owner+zzz"[/i][/green] [teal]][/teal]
  [b]then[/b]
    lsnrctl status [navy]$lsnr_alias[/navy] [teal]>[/teal] /dev/null [purple]2[/purple][teal]>&[/teal][purple]1[/purple]
    [navy]lsnr_status[/navy][teal]=[/teal][navy]$?[/navy]
    echo [green][i]"[/i][/green][lime][i]\n[/i][/lime][green][i]$lsnr_owner Listener [/i][/green][lime][i]\"[/i][/lime][green][i]$lsnr_alias[/i][/green][lime][i]\"[/i][/lime][green][i] status: $lsnr_status[/i][/green][lime][i]\n[/i][/lime][green][i]"[/i][/green] [teal]>>[/teal] [navy]$sh_log_file[/navy]
  [b]else[/b]
    echo [green][i]"[/i][/green][lime][i]\n[/i][/lime][green][i]Listener [/i][/green][lime][i]\"[/i][/lime][green][i]$lsnr_alias[/i][/green][lime][i]\"[/i][/lime][green][i] owned by: $lsnr_owner - status NOT checked.[/i][/green][lime][i]\n[/i][/lime][green][i]"[/i][/green] [teal]>>[/teal] [navy]$sh_log_file[/navy]
  [b]fi[/b]

[b]done[/b] [teal]<<[/teal]END_OF_INPUT
[navy]$([/navy] ps -ef [teal]|[/teal] grep /bin/tnslsnr [teal]|[/teal] grep -v grep [navy])[/navy]
END_OF_INPUT

[b]if[/b] [teal][[/teal] [navy]$lsnr_grep[/navy] [teal]=[/teal] [green][i]"notfound"[/i][/green] [teal]][/teal]
[b]then[/b]
  echo NO listener found[teal]![/teal] [teal]>>[/teal] [navy]$sh_log_file[/navy]
  [navy]lsnr_status[/navy][teal]=[/teal][purple]1[/purple]
[b]fi[/b]


Feherke.
[link feherke.github.com/][/url]
 
Thank you! I had read various sections of documentation and blogs, but could not get the redirect syntax right! I've done some more reading about here-documents and your solution makes more "readable" sense to me. It's important to me to know "why" the code works, not just that it does. I have to be able to read it and explain it before I implement it. Thanks again! You get a big "STAR" from me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top