- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Question with my awk section
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2012 07:18 PM - edited 10-05-2012 05:26 PM
06-22-2012 07:18 PM - edited 10-05-2012 05:26 PM
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-22-2012 10:59 PM
06-22-2012 10:59 PM
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}}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2012 04:39 AM
06-23-2012 04:39 AM
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 ??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2012 04:12 PM
06-23-2012 04:12 PM
Re: awk exit status
>but script doesn't exit with non zero?
Please paste your script so we can verify.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2012 11:41 PM - edited 06-23-2012 11:42 PM
06-23-2012 11:41 PM - edited 06-23-2012 11:42 PM
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".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2012 02:22 AM
06-24-2012 02:22 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2012 01:52 PM
06-24-2012 01:52 PM
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}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2012 03:41 PM
06-24-2012 03:41 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2012 01:05 PM - edited 06-25-2012 07:28 PM
06-25-2012 01:05 PM - edited 06-25-2012 07:28 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2012 02:57 PM
06-25-2012 02:57 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2012 03:01 PM
06-25-2012 03:01 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-25-2012 07:48 PM
06-25-2012 07:48 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2012 02:32 AM
06-26-2012 02:32 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2012 02:33 AM
06-26-2012 02:33 AM
Re: awk exit status
by the way im using korn shell--thnx
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2012 11:41 AM - edited 06-26-2012 11:46 AM
06-26-2012 11:41 AM - edited 06-26-2012 11:46 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2012 02:47 PM
06-26-2012 02:47 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2012 04:52 PM
06-26-2012 04:52 PM
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
}
}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2012 09:31 AM
06-27-2012 09:31 AM
Re: awk exit status
>Online Virtual Memory : -
Then why are you looking for: "^Variable Memory Capacity Weight"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2012 03:09 PM
06-27-2012 03:09 PM
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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2012 10:03 PM
06-27-2012 10:03 PM
Solution>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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2012 03:03 PM
06-28-2012 03:03 PM
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
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2012 04:01 PM
06-28-2012 04:01 PM
Re: awk exit status
You can also shorten it to:
function check_FQDN {
[[ "$(hostname)" != *.* ]] # match at least one "."
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2012 05:00 AM - edited 10-05-2012 05:49 PM
06-29-2012 05:00 AM - edited 10-05-2012 05:49 PM
Re: awk exit status
Many thanks again Dennis
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2012 04:59 AM - edited 07-06-2012 05:05 AM
07-06-2012 04:59 AM - edited 07-06-2012 05:05 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-06-2012 04:59 AM
07-06-2012 04:59 AM
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 ?