Languages and Scripting

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.