Operating System - HP-UX
1827775 Members
2601 Online
109969 Solutions
New Discussion

Question with my awk section

 
SOLVED
Go to solution
Nadie_1
Frequent Advisor

Question with my awk section

 

 

68 REPLIES 68
Dennis Handly
Acclaimed Contributor

Re: awk exit status

>all I need is either exit with 0 or non zero

 

END { if (FOUND != 1) {print "          TSM Server          =  Not configured"; exit 1}}'

Nadie_1
Frequent Advisor

Re: awk exit status

 

Thanks for the suggesstion I have tried putting exit 1 where u suggessted and simulated the test again but script doesn't exist with non zero ??

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>but script doesn't exit with non zero?

 

Please paste your script so we can verify.

Dennis Handly
Acclaimed Contributor

Re: awk exit status

> Above is the excerpt again, script itself works fine but doesn't exit with non zero code even when it is not registered.

 

I don't see my exit 1.  I also don't see what is after awk.

 

/usr/bin/dsmc q se | awk '

/Server Name/  {

   print "          TSM Server          =  " $NF

   FOUND=1

}

END {

if ( FOUND != 1 ) {

   print "          TSM Server          =  Not configured"

   exit 1

}

}'

if [ $? -ne 0 ]; then

   exit 1

fi

 

>Here is set -x for your perusal,

 

I see more than what you have in your script.

 

>exit status is 1 but it still goes ahead

 

You have to check for it.  Or use "set -e".

Nadie_1
Frequent Advisor

Re: awk exit status

just to be precise with my code as per my previous post

 

some_command | awk '

/retrive_this_value/  { # if the value is none ie blank then exit the program but it doesn't exit and goes further, i don't know how to check the blank character here , i tried "" but obviously that didn't work for me

   print "          Virtual Memory          =  " $NF

   FOUND=1

}

END {

if ( FOUND != 1 ) {

   print "          Virtual Memory         =  Not assigned--exiting program"

   exit 2

}

}'

if [ $? -ne 0 ]; then

   exit 2

fi

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>this is great help, I have got what I wanted.

 

If you are happy with the answers you were given, please click on the Kudos stars of each helpful post.

 

>lspath -l hdisk1 -s Enabled|grep fscsi|grep Enabled

 

You can optimize this to one grep:

lspath -l hdisk1 -s Enabled | grep "Enabled.*fscsi"

 

You can use "grep -c" to count and with a real shell should use $() vs ``:

DSKPATH=$(lspath -l $DISK -s Enabled | grep -c "Enabled.*fscsi") # this counts for 2 paths 1 per adapter

 

   if [ $DSKPATH -lt 2 ]; then
      echo "We don't have dual paths--exiting program now--"
      exit 1

   fi

done

echo "We have dual paths for all disks"

 

>parameter     # space - means there is no value assigned, and if it is true then simply exit the program)?

 

You could use:

if [ "$parameter" = "" ]; then

   exit

fi

 

>if the value is none ie blank then exit the program

 

If you want to check for an empty line, you can do:

/^#/ { exit 1}

Nadie_1
Frequent Advisor

Re: awk exit status

Thanks again , you have been great assist, I will test it and let you know if I have any hiccup but this should now resolve my hiccups :)

 

I've clicked on start to assign points but it only seems to add 1 point, is this maximum we can assign ?

 

once i have tested it i will close this thread by marking as "accept this as solution", i may have to ask some more question tho...

 

Regards

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>it only seems to add 1 point, is this maximum we can assign?

 

Yes, one per post.

 

>return 1 # not sure if this is where I need return or else where please correct

 

Yes, this will work for functions.

 

>if [ "$NF" = "" ]; then # How to check - :

 

Why bother?  If you don't get a good exit status (0), you didn't find what you wanted.

 

If you do want to check, you can just check in awk for:

if ($NF == ":")

 

>return 0 # Please correct

 

SInce you aren't in an awk function, I would just use:  exit 0

(In fact, awk will not let you use return unless you are in a function.)

 

># Main and do I need to execute my functions below?

 

Yes.

Nadie_1
Frequent Advisor

Re: awk exit status

 

Thanks again few more quest, my questions are next to # below

 

 

 

>return 1 # not sure if this is where I need return or else where please correct, i will test this today tho

 

Yes, this will work for functions.

 

>if [ "$NF" = "" ]; then # How to check - :

 

Why bother?  If you don't get a good exit status (0), you didn't find what you wanted.

 

If you do want to check, you can just check in awk for:

if ($NF == ":")  # So this check goes into the same place where I'm currently checking in my script

 

>return 0 # Please correct

 

SInce you aren't in an awk function, I would just use:  exit 0

# Even if I'm not in awk i need to check return 1 or return 0, so i can make it uniform in the script

I guess i can replace return 1 with exit 1 which i originally had but where to check return 0 tho ?

 

># Main and do I need to execute my functions below?

# The way i have stuff in my main, is this the correct way of checking it or am i missing anything such as executing function etc ? please correct

 

Yes.

Nadie_1
Frequent Advisor

Re: awk exit status

sorry in one of my comment i meant to say i can replace exit 1 which i originally has in my code with return 1 but where to place return 0 tho ?

 

also pls correct me where to place return 1 and return 0 within awk function

 

then in my main if I need to execute my functions and then check status with if statement or the way i currently have in my script is OK ?

 

thanks for your help really this clarifies and helps me learn a lot

Dennis Handly
Acclaimed Contributor

Re: awk exit status

># Even if I'm not in awk I need to check return 1 or return 0, so I can make it uniform in the script

>I guess I can replace return 1 with exit 1 which I originally had but where to check return 0 though?

 

If you are in an awk function, you can use return, to return a value.  If you want to exit awk, you use exit.

In a shell function, you can use return, unless you want to exit the script.

 

>in one of my comment I meant to say I can replace exit 1 which I originally has in my code with return 1 but where to place return 0 though?

 >pls correct me where to place return 1 and return 0 within awk function

 

You can only use exit, not return.  In your case, you only need "exit 1", since "exit 0" is done on a normal exit.

 

>in my main if I need to execute my functions and then check status with if statement or the way I currently have in my script is OK?

 

It's ok if you use a Posix shell but not ksh88.

Otherwise you need:

if [ ! check_tsm_reg ]; then

 

Or you could use:

check_tsk_reg || status=1

Nadie_1
Frequent Advisor

Re: awk exit status

Thanks again, I will test it but if you can correct my script with your suggestion it will save me sometime as im doing many things, however im trying myself too to get this sorted ,

 

correct me if im wrong if I have exit 1 within awk function then it still exits the program rather then just exiting the awk function and continue with further instruction ie functions, i don't want to exit the program, i just want to capture return code 1 if error and then exit only after completing everything.

 

Also some_command display in the below format, if it is assigned it gives out some number and in this case i would like to return with rc 0

Online Virtual Memory                : 3.00

 

If not assigned then it displays blank with ie (colon space -)

 

Variable Memory Capacity Weight            :  - (colon space -) i really need to capture (dash) i think since if the memory is available then it still prints colon (:)

 

i can't capture this with

 

if [ "$NF" == "-" ]; then

print "online memory is not configured, return 1"

return 1 #( at this stage if it type exit 1 it will exit the program which i don't want it)

 

can you please kindly correct my script that I posted with proper code so i can write more functions once i get the funtion both awk and shell functions right I can write several more functions. Please correct me if im not capturing the return code properly in my script, as i want to exit with either 0 or 1 , since i set my variable $success to 0....

 

Much appreciate your help

Nadie_1
Frequent Advisor

Re: awk exit status

by the way im using korn shell--thnx

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>if I have exit 1 within awk function then it still exits the program rather then just exiting the awk function

 

Right, it only exits awk.

 

>some_command display in the below format, if it is assigned it gives out some number and in this case I would like to return with rc 0
>Online Virtual Memory                : 3.00

 

If you don't exit awk with "exit 1", it will exit with 0.

>Variable Memory Capacity Weight            :  - (colon space -) I really need to capture (dash)

 

In awk, you check with:

if ($NF == "-")  {

   print "online memory is not configured, return 1"

   exit 1

}

 

Then in your script, you check for the bad awk exit status and return from that function.

Nadie_1
Frequent Advisor

Re: awk exit status

Can you please give me an example based on my script template how we capture the status , i tested it with exit 1 but it exits the program, and when i put return it complains that return should be within awk statement etc ? although i think i've put return 1 within awk......but anyway we can use exit 1 within awk since you recon it won't exit the program but appreciate if u can  correct my code---thanks

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>Can you please give me an example based on my script template how we capture the status

 

I'm confused in your command output format.  You mentioned:

>Online Virtual Memory                : 3.00

>If not assigned then it displays blank with ie (colon space -)

>Variable Memory Capacity Weight            :  - (colon space -) 

 

Does the one "Some_command" produce both outputs?  Or there are two commands?

If you are only looking for "Variable Memory Capacity Weight" and need to check for "-", you can do:

Some_command | awk '

/Variable Memory Capacity Weight/ {

   if ($NF == "-") next   # see if better value later?

   print "Memory = " $NF

   FOUND=1

}

END {

if ( FOUND != 1 ) {

   print " Virtual Memory is not OK"

   exit 3

}

}'

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>Online Virtual Memory                : -

 

Then why are you looking for: "^Variable Memory Capacity Weight"

Nadie_1
Frequent Advisor

Re: awk exit status

sorry please ignore that, it should be read as "Virtual Online Memory"---my typo but in my actual script I have "Online Virtual Memory" instead of "Variable Capacity Weight"

 

problem is that script doesn't pick up (-) if it's not assigned and doesn't return NON zero return code ?

Dennis Handly
Acclaimed Contributor
Solution

Re: awk exit status

>MEMORY=`some_command | grep "^Variable Memory Capacity Weight" | awk -F: '{print $2}'`

 

With -F:, you need to look for " -".  But better to only use the default delimiter and remove the grep:

MEMORY=$(some_command | awk '/^Virtual Online Memory/ {print $NF}')

if [ "$MEMORY" != "-" -a "$MEMORY" != "" ]; then

   echo "memory is configured"

else

   print "memory is not configured"

fi

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>my main part exits the program with rc 0 even if the memory is not defined which is what we are checking in our function which works fine...

 

A "return 1" was needed.

 

>#!/bin/ksh

>#Function:

>check_memory()

 

I would suggest you stop using this obsolete Bourne style functions and use the new style:

function check_memory

 

MEMORY=$(lparstat -i | awk '/^Virtual Online Memory/ {print $NF}')

if [ "$MEMORY" != "-" -a "$MEMORY" != "" ]; then

   echo "memory is configured"

else

   print "memory is not configured"

   return 1   # just add this return

fi

}

 

>I'm also writing a simple function and checking the fully qualified hostname, if hostname is a short name then rc 0 if it is fully qualified then return non 0

>can you pls check on how to check this?

 

You could count periods?

 

>hostname command returns either short name or fully qualified name and I'm after short name only

>nodename.co.some.uk (this is fully qualified, and i need to check shortname using hostname?

 

If you want to just assume any name with just at least one period is fully qualified, you can do:

function check_FQDN {

if [[ "$1" != *.* ]]; then

   return 0

else

   return 1

fi

}

Dennis Handly
Acclaimed Contributor

Re: awk exit status

You can also shorten it to:

function check_FQDN {

[[ "$(hostname)" != *.* ]]  # match at least one "."

}

Nadie_1
Frequent Advisor

Re: awk exit status

 

 

Many thanks again Dennis

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>DISK=$(some_command $DSK |awk '^/disk_parameter/   # now here I need to check the disk value 0 or more


What is the output of:

some_command $DSK | awk '^/disk_parameter/ {print $NF}'

(You spelled parameter wrong?)

 

>if [ "$DISK" != 0 ]; then # can you please correct me here?

 

If you want an integer > 0, you should use:

if [ "$DISK" -gt 0 ]; then

 

Also, you go through the loop many times so you can't do a return unless you want to stop on one condition:

for DSK in $(some_cmd); do# this holds the names of all the disks

   DISK=$(some_command $DSK |awk '^/disk_paramenter/ {print $NF}'

   if [ "$DISK" != 0 ]; then ## can u please correct me here ?

     print "Parameter is a number higher than zero"

     return 1

   fi

done

print "disk pameter is 0, all good"

return 0

Nadie_1
Frequent Advisor

Re: awk exit status

 

just a correction in my line

 

DISK=$(some_command $DSK |awk '^/disk_paramenter/ {print $NF}')

 

my function doesn't achieve what im looking for ?