1748023 Members
4514 Online
108757 Solutions
New Discussion

Re: awk exit status

 
SOLVED
Go to solution
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