Operating System - HP-UX
1827491 Members
2323 Online
109965 Solutions
New Discussion

Question with my awk section

 
SOLVED
Go to solution
Nadie_1
Frequent Advisor

Re: awk exit status

thanks for the quick response, appreciate it

 

some_command $DSK list many things but im only interested in the paramter ie something like below

 

DISK attr: 0 (or it could be higher than 0) so need to caputre this in my variable correctly or correct me where im tripping ?

 

also in another function I need to check if there are 3 files exist in some directory and that they are not 0 in size

can u pls correct this

 

dir=`ls -l /home/somedir`

if [ "$dir" -eq 3 -a "filesize != 0 ]; then

print "3 files exist all good"

return 0

else

print "error less then three files or the size is 0 --please check"

return 1

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>DISK attr: 0 (or it could be higher than 0) so need to capture this in my variable correctly or correct me

 

If that's what the line looks like then:

DISK=$(some_command | awk '/^DISK attr:/ {print $NF}')

 

Is non-zero the error case and you only need to find one of them?

 

>if there are 3 files exist in some directory and that they are not 0 in size

 

ls -l /home/somedir | awk '

BEGIN {count = 0; good = 0}

NF > 5 {  # Not Total line

   ++count

   if ($5 > 0) ++good

}

END {

if (good != 3 || count != 3) exit 1

}'

if [ $? -eq 0]; then

   print "3 files exist all good"

   return 0

else

   print "error less then three files or the size is 0 --please check"

   return 1

fi

Nadie_1
Frequent Advisor

Re: awk exit status

much appreciate it

 

i will test both functions soon and will let you know, i'm writing some more functions so I may need more help, i really learn a lot mate--

talk to you later...gotta test these functions and the new ones im writing so will post you any quest i may have :)

 

 

Nadie_1
Frequent Advisor

Re: awk exit status

sorry had typo it should be like below

 

function check_disk

{

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

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

   if [ "$DISK" != 0 ]; then

     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

by the way also in file check function you have exit 1, exit 1 exits the program regardless if it's within awk or outside awk

 

>if there are 3 files exist in some directory and that they are not 0 in size

 

ls -l /home/somedir | awk '

BEGIN {count = 0; good = 0}

NF > 5 {  # Not Total line

   ++count

   if ($5 > 0) ++good

}

END {

if (good != 3 || count != 3) return 1 ##exit 1 # can i replace this with return 1 will this work ?

}'

if [ $? -eq 0]; then

   print "3 files exist all good"

   return 0

else

   print "error less then three files or the size is 0 --please check"

   return 1

fi

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>DISK=$(some_command $DSK |awk '^/DISK attr:/ {print $NF}'

 

The "^" should be inside the //.

 

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

 

SInce $DISK is an integer, you should use:

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

 

>exit 1 exits the program regardless if it's within awk or outside awk

 

Yes, exit will exit the shell.  But if in awk, it will just exit awk.

 

>##exit 1 # can I replace this with return 1 will this work?

 

Of course not.  You can only use an awk return inside awk functions.

Nadie_1
Frequent Advisor

Re: awk exit status

Hi Dennis

 

I tried with the code below that u suggested, but the problem is my program exits with RC 0, regardless if the value is 0 or greater than 0, can u please check

below is the excerpt again...

#!/bin/ksh

set -x

 

function check_disk

{

for DSK in $(some_cmd); do

DISK=$(lsvg $DSK | awk '/^DISK attr:/ {print $NF}')

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

print "Parameter is a number higher than zero"

return 1

fi

done

print "disk pameter is 0, all good"

return 0

}

 

Q2, you code to checck file size of greater than 0 and count 3 files doesn't work, when i ran script with set -x it complains that

if ($5 > 0) ++good ( it doesn't like this one ? and what  $5 means here ?

also is there a way we can avoid exit 1 in the awk function as it really exits the entire program so it won't allow the script to execute the rest of functions etc.

Nadie_1
Frequent Advisor

Re: awk exit status

in addition to my previous email, regarding counting files and their sizes, i wrote a small code below can u please corect this or the one u sent--

 

-s suggest if file exist and greater than 0 in size, my code is not correct tho

 

#!/bin/ksh

 

dirname=`ls -l /somedir` # this lists all files in the dir

files=`wc -l /dirname` # count no of files in directory

if [ -d "$dirname" && "$files" >= 3 && -s "$dirname" ]; then # if directory exist and we have 3 or more files then all good, and >0 size

print "we have 3 or more files exist"

else

print "we have less than 3 files or their sizes are 0"

fi

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>the problem is my program exits with RCA 0, regardless if the value is 0 or greater than 0
>DISK=$(less $DISK | awk '/^DISK attar:/ {print $NF}')

 

Can you echo DISK here to make sure we found the proper line.

Echo "DISK|$DISK|"

 

>your code to check file size of greater than 0 and count 3 files doesn't work, when I ran script with set -x it complains that
>if ($5 > 0) ++good #it doesn't like this one? and what  $5 means here?

 

Field 5 should be the size of the file, if I counted correctly.

 

>is there a way we can avoid exit 1 in awk as it really exits the entire program so it won't allow the script to execute the rest of functions etc.

 

Again, this can NOT happen.  The exit function in awk will only exit awk, not the invoking process/shell.

 

 

>I wrote a small code below can you please correct this or the one you sent:
>-s suggest if file exist and greater than 0 in size

>dirname=`ls -l /somedir` # this lists all files in the dir

 

Try:

filelist=$(ls /somedir/*)

files=$(echo $filelist | wc -w) # count no of files in directory

typeset -i count=0

for file in $filelist; do

   if [ -s $file ]; then # size > 0

      ((count  += 1))

   fi

done

if [ "$files" -ge 3 -a $count -ge 3 ]; then # if directory exist and we have 3 or more files then all good, and >0 size
   print "we have 3 or more files exist"
else
   print "we have less than 3 files or their sizes are 0"
fi

Nadie_1
Frequent Advisor

Re: awk exit status

Thanks Chief again

 

 

 

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>I tried putting echo but it doesn't display anything\


Then either the pattern match is wrong or is it not the last column.

That's why you need to get this working first:

some_command $DSK | awk '/^DISK attr:/ {print $0}'

 

>the problem is my program exits with RC 0, regardless if the value is 0 or greater than 0

 

Probably because an empty string is considered 0?  Or awk didn't match.

 

>"DISK|$DISK|" but it doesn't return anything


It has to at least have: DISK||

>if I want to check 3 attributes ie for e.g (DISK attr1, DISK attr2, DISK attr3)

 

That it has all of them?  Any particular values?  I would just program it all in awk.

Nadie_1
Frequent Advisor

Re: awk exit status

Thnks again, I got that working, i tested it slightlty differently as it was not picking up the value correctly so i tried it grep ie

 

grep "DISK attr" | awk '{ print $NF }' # This now works like a charm

 

 

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>so I tried it grep ie: grep "DISK attr" | awk '{ print $NF }'

 

There is no need to use grep followed by awk, you can use awk directly:

awk '/DISK attr/ { print $NF }'

 

>I'm curious how we can get it working without using grep

 

You have to use the right regular expression.  (Not that it matters in this case but awk uses ERE, not RE.)

 

>I want to check 3 attributes for disks instead of 1.  If any of the attributes below doesn't match the value I specified then it should return 1 in the awk program?

some_command | awk '

BEGIN { hc = 0; rr = 0; nr = 0 }

/health_check/ && $NF == 32 {

   ++hc

   next

}

/algorithm/ && $NF == "round_robin" {

   ++rr

   next

}

/reserve_policy/ && $NF == "no_reserve" {

   ++nr

   next

}

END {

if (hc == 0 || rr == 0 || nr == 0) exit 1  # not all attributes set correctly

}'

Nadie_1
Frequent Advisor

Re: awk exit status

thanks again----I tried your code but it exits with return code 0 regardless if value is true or false ?

 

instead I tried it differently with if statment and it worked but it's good to know how to get your code working ?

 

also how can we verify if some_dir has 770 perms set and

some_file has 660 perms set ?

 

thnx

 

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>I tried your code but it exits with return code 0 regardless if value is true or false?

 

I'm not sure how?  Try adding some print statements in the code:

/health_check/ && $NF == 32 {

   ++hc

   print "Found health_check 32"

   next

}

/algorithm/ && $NF == "round_robin" {

   ++rr

   print "found round_robin"

   next

}

/reserve_policy/ && $NF == "no_reserve" {

   ++nr

   print "found no_reserve"

   next

}

END {

print "hc:, hc, "rr:", rr, "nr:", nr

if (hc == 0 || rr == 0 || nr == 0) exit 1  # not all attributes set correctly

print "There was one good match"

}'


>but it's good to know how to get your code working?


The code makes sure it finds all of the attribute matches.  It allows for the same attribute more than once.

>how can we verify if some_dir has 770 perms set and some_file has 660 perms set?

 

You can do "ll -d" on directories and files and then convert the letters to numbers.

Or feed your files and directories into find(1) and use:

find files directories ... -type f -perm 660 -o -type d -perm 770 -print -o -type d -prune

 

This will print the names of files and directories that match.  (And won't descend directories.)

Nadie_1
Frequent Advisor

Re: awk exit status

 

 sure thnx, i will test that out with print---

 

one more quest:

 

i have written bunch of functions and i only want error going to errlog and also i want to generate the full log

so in the begining of the script i have both variables set

 

rc=0

logfile=/tmp/full.log

errorlog=/tmp/errorlog

 

exec /tmp/full.log # this generates the full log

 

and in my functions

if check_something; then

print " all good"

else

print "error" >> ${errorlog}

 

in the end i check the exit status of rc so it is, below is just an extract

 

if [ ${rc} -ne 0 -a -s ${errorlog} ]; then

    echo "Full log can be found: ${logfile}"

    cat ${errorlog}

fi

 

exit ${rc} >> ${errorlog} # problem is this overrides and write the exit status to full.log instead of error log if i have

                                       # exec  /tmp/full.log (if this is enabled then it doesn't write the exit status to errorlog ?

can u pls point out how to maintain full log and send errors to onlly errorlog including return code status ?

 

 

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>exec /tmp/full.log # this generates the full log

 

The proper syntax is: exec > /tmp/full.log

(If you want stderr also sent there, add "2>&1" to the end.)

 

>exit ${rc} >> ${errorlog} # problem is this overrides and write the exit status to full.log instead of error log

 

exit doesn't print anything so this will do nothing.

Perhaps you want to echo it before the exit?: echo "exit ${rc}" >> ${errorlog}

 

>can you pls point out how to maintain full log and send errors to only errorlog including return code status?

 

The latter isn't recorded anywhere, you must add echoes before your exit.

To redirect all stderr to a file you can do:

exec 2> ${errorlog}

 

 

 

 

Nadie_1
Frequent Advisor

Re: awk exit status

Many Thanks for your help and clarification.

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>which will check the exit status of my current script, any suggestion what might be the best practice to achieve this?

 

Well, it's pretty simple.  After you invoke your script, you either need to check right away using $? or save the value for checking later:  status=$?

Nadie_1
Frequent Advisor

Re: awk exit status

basically in short, my new script will first check the exit status and if it is non zero then it will check for errors in my main script's errorlog and send it somewhere..

 

so u mean that my new script will check the exit status first?

 

can you pls give me a small example based on the above ?

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>my new script will first check the exit status and if it is non zero then it will check for errors in my main script's errorlog

 

Does you new script invoke the old?  Or look at the error log for the exit status?

 

>so you mean that my new script will check the exit status first?

 

I wasn't sure if one script invoked the other?  Or is it just logfile scanning?

Nadie_1
Frequent Advisor

Re: awk exit status

Good quest, I will let you know 2moror if it's going to invoke my existing script or only scan the error log for exit status and errors, i definitey want to scan the errorlog though for any errors so they can be sent to somewhere etc

 

 

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>I definitely want to scan the errorlog though for any errors

 

Then you want to use a rigorous format to make it easier.

 

Nadie_1
Frequent Advisor

Re: awk exit status

 

Sorry $ infront of test was my typo..

 

so based on my above code since how do correct my code, i don't really want to run it twice if I can manage to get what i want by running it once ?

 

I guess i can take out the part where i'm sourcing my main script and just test in if condition ?

 

can u pls correct it ?

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>I guess I can take out the part where I'm sourcing my main script and just test in if condition?

 

Yes.  But where is value_of_especial_env_variable set?