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!
# 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!