Operating System - Linux
1753519 Members
5215 Online
108795 Solutions
New Discussion юеВ

awk to run execute within a script

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

awk to run execute within a script

Hi,

I have setup some simple code but would like to know if there is an awk alternative that if the string "Connection refused" is in $PROGMESG then exit the script.

NOSEND=`echo $PROGMSG |awk '/Connection refused/ {print}'`

if [ $NOSEND = "Connection refused" ] ; then

exit 1

fi

thanks
hello
8 REPLIES 8
lawrenzo_1
Super Advisor

Re: awk to run execute within a script

sorry I should give this example:

NOSEND=`echo $PROGMSG |grep -c "Connection refused"'`

if [[ $NOSEND -gt 0 ]] ; then

exit 1

fi
hello
Sandman!
Honored Contributor
Solution

Re: awk to run execute within a script

# echo $PROGMSG | awk '$0=="Connection refused" && exit
OldSchool
Honored Contributor

Re: awk to run execute within a script

here another one

echo $PROGMSG | grep "Connection Refused"
NOSEND=?*

if [ $NOSEND -eq 0 ]; then

exit 1
fi

Sandman!
Honored Contributor

Re: awk to run execute within a script

Update as I omitted the awk-action stmt.

# echo $PROGMSG | awk '$0=="Connection refused"{}' && exit
spex
Honored Contributor

Re: awk to run execute within a script

Hi Lawrenzo,

Why not use grep's 'quiet' option, as all you're really interested in is the return code?

echo ${PROGMSG} | grep -q 'Connection refused'
RC=${?}
if [ "${RC}" -ne "0" ]
then
echo "Exiting with rc ${RC}"
exit ${RC}
fi

If you're looking to kill the parent shell script from within awk, this should work:

awk '/Connection refused/ {system("kill $PPID")}'

This yields an exit status of '143' on my system.

PCS
Hein van den Heuvel
Honored Contributor

Re: awk to run execute within a script

- Why use an intermediary symbol? Just use the exit status!

- Why count? Just tell grep to quit on first match.

if $(echo $PROGMSG | grep -q "Connection refused");
then
echo aap
else
echo noot
fi

fwiw,
Hein.

Hein van den Heuvel
Honored Contributor

Re: awk to run execute within a script

And the shell itself can of course do string pattern matching and testing avoiding the extra processess:

$ PROGMSG="Connection refused"
$ if [[ $PROGMSG = "Connection refused" ]]; then
echo aap
else
echo noor
fi
aap

This is often best done for a positive match, such that various and new error message are also caught as needed.

Hein


lawrenzo_1
Super Advisor

Re: awk to run execute within a script

ok thanks guys - lotsw of examples there ...
hello