Operating System - HP-UX
1748181 Members
3862 Online
108759 Solutions
New Discussion

Question with my awk section

 
SOLVED
Go to solution
Nadie_1
Frequent Advisor

Re: awk exit status

 

 Hi Dennis

 

 grep -q worked for me but i just have to write 2 functions to check pathid=0 and pathid=1, i just couldn't check it within one function--

 

here is my snippet

 

function test

{

some_command|grep -q "pathid=0"

if [ $? -ne 0 ]; then

 print "error: pathid=0 doesn't exit"

return 1

else

print "pathid=0 exist"

return 0

}

 

and i wrote the second function to check pathid=1----any idea how to check both as im tripping there :) i got what i wanted but still good to know where im lacking...honestly with exit 1 in awk statement it exits the entire program and not just awk which isn't what i want really...

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>but I just have to write 2 functions to check pathid=0 and pathid=1, I just couldn't check it within one function
some_command | grep -q "pathid=0"

 

You should be able to do:

some_command | grep -q -e "pathid=0" -e "pathid=1"

if [ $? -ne 0 ]; then

>with exit 1 in awk statement it exits the entire program and not just awk which isn't what I want

 

This shouldn't happen.  It should only exit awk.

Unless you have done "set -o errexit" or "set -e" but then if grep fails, it will exit.

Nadie_1
Frequent Advisor

Re: awk exit status

Many thanks again for showing me new ways of doing things, i'm all good for now, but will post question here as i will be writing a new script soon, have learned a lot'

 

will have to do a lot of reading on regular expression...i did post this earlier but it was linked to our thread

 

 

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>But we don't want swap to be the same size if system's memory is very large ...

>#!/usr/bin/ksh

 

Note: Good, ksh supports 64 bit ints but not sh.

 

>newlps=""

 

newlps is an integer, not a string:

typeset -i newlps=0 pp_size memsize memmb memlps swaplps lpsize

 

>memmb=`expr $memsize / 1024`

 

Don't use expr(1) at all.  It is limited to 32 bit ints.  Also, you don't need "$" for (( )).

(( memmb = memsize / 1024 ))

(( memlps = memmb / pp_size ))

 

These two are a problem.  memmb is in MB.  Then you divide MB by a page size?  Is that in MBs or bytes?

...

if [ $memlps -gt $swaplps ]; then

   (( newlps = memlps - swaplps ))

 

Assuming everything is in MBs:

 

   typeset -i newswap=$swaplps

   if (( memlps >= 20 * 1024 && memlps < 30 * 1024 )); then

      (( newswap = 10 * 1024 ))

   elif (( memlps >= 10 * 1024 && memlps < 20 * 1024 )); then

      (( newswap = 8 * 1024 ))

   elif (( memlps < 10 * 1024 )); then

      (( newswap = 4 * 1024 ))

   fi

 

   (( lpsize = swaplps - memlps )) # this is a check if swaplps are bigger than memlps

   `some_command` # this decreases the swap size so it is same as memory size,

 

This is hard, I don't think you want to do this.  Probably requires a reboot.

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>I noticed that you use (( brackets instead of square [ ] brackets in if statements so what's the difference?

 

[...] only allows -a -o for AND and OR.  () have to be escaped.

[[...]] takes && || for AND and OR.  It also does pattern matching for strings, not compares

((...)) only does C style arithmetic expressions for integers.  Since I was using $((...)), I wanted to match.

Instead of "newswap=$(( 10 * 1024 ))", you could use:

   (( newswap=10 * 1024 ))

This probably looks better.

> if (( memlps => 20 * 1024 && memlps < 30 * 1024 )); then

 

Oops, that should be:

if (( memlps >= 20 * 1024 && memlps < 30 * 1024 )); then

>      command_to_set_swap $swaplps # please correct me if im wrong here?

 

Don't you want $newswap here?

 

>do you want me to initialize all variable with typeset individually? Or does the below command does it for all them?

 

It declares them all as integers but only initializes one.

>I assume I don't need to check this anymore
>if [ $memlps -gt $swaplps ]; then

 

I would keep it so that you can centralize increasing swap.

>please correct my last elif statement where I'm checking if system memory is greater than 30G then set swap to 15G, so at least we have covered all scenarios?
################################

No need to initialize all of your variables to zero.
 
typeset -i newlps pp_size memsize memmb memlps swaplps newswap

pp_size=$(some_command | awk '/SIZE/ {print $6}')
memsize=$(command_to_display system_memory) # this displays memory in kb, divide by 1024 to get MB
(( memmb = memsize / 1024 ))
(( memlps = memmb / pp_size ))
swaplps=$(some_command) # This check the correct lp size of swap lv

(( newswap = swaplps ))

if (( memlps > swaplps )); then

   if (( memlps >= 30 * 1024 )); then
      print "memory is greater than 30G, so setting system swap to 15G"
      (( newswap = 15 * 1024 ))
   elif (( memlps >= 20 * 1024 )); then
      (( newswap = 10 * 1024 ))
   elif (( memlps >= 10 * 1024 )); then
      (( newswap = 8 * 1024 ))
   elif (( memlps < 10 * 1024 )); then
      (( newswap = 4 * 1024 ))  

   fi

   if (( newswap > swaplps )); then
    command_to_set_swap $newswap  # increase it

   fi

fi

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>what about if swaplps is greater than memlps we are not checking that scenario?

 

It is too hard to reduce swap, it requires a reboot.

But you could check and print out a message.  But you may want to allow swap greater than memory if you don't have much memory.

>also we need to check and ensure both conditions that is for e.g
>if (( memlps .<= 30 * 1024 && memlps >= 20 * 1024 )); then # I want to make sure I cover 20 to 30G here

This is automatically handled due to the order of checks.

 

>elif (( memlps < 10 * 1024 )); then

 

This check isn't needed and can just be: else

Nadie_1
Frequent Advisor

Re: awk exit status

Thanks a lot, will try out my code and let u know soon,

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>I just want to pick up the below 2 files

 

It may be easier to just use ll to look at the permission.  Otherwise you can ask find to check only those two files:

count=$(find /home/user/.ssh -perm 600 \( -name authorized_keys -o -name authorized_keys3 \) | wc -l)

 

If $count is 2, then everything is fine.

Nadie_1
Frequent Advisor

Re: awk exit status

Thanks Dennis, will test that out, should be all good