Operating System - HP-UX
1827459 Members
4049 Online
109965 Solutions
New Discussion

Question with my awk section

 
SOLVED
Go to solution
Nadie_1
Frequent Advisor

Re: awk exit status

yes you are correct, actually some other environmental script is sourcing the variables so not to worry as I have that part sorted so im not including here.....

 

to make it simple for you to assist, I have written code below and it works like a charm, now all i need to know if my main script doesn't exist in it's location then I want the program to exit without proceeding it further.

 

can u please check the underlined part and correct below part in my code below, i will have to test it anyway

 

if [ -x /tmp/main.script.ksh ] ; then

print "script exist continuing with the rest of the program"

return 0

else

print "main script does not exist into it's location please check"

exit 1

 

 

#!/bin/ksh

 

DATE=$( date )

NODENAME=$( hostname )

 

if [ -x /tmp/main.script.ksh ] ; then

print "script exist continuing with the rest of the program"

return 0

else

print "main script does not exist into it's location please check, exiting now without proceeding further"

exit 1

 

 

some_var="some_msg"

another_var="some_msg"

 

 

 

function test_it

{

        if ! /tmp/main.script.ksh > /dev/null 2>&1 ; then

                cat /tmp/error.log

                cat /tmp/complete.log

                return 1

        else

                cat /tmp/complete.log

                return 0

        fi

}

 

CHECK_IT=$( test_it )

if [[ $? -ne 0 ]]; then

        some_var="some_msg"

        another_var="some_msg"

fi

 

 

Dennis Handly
Acclaimed Contributor

Re: awk exit status

I would suggest you reverse the logic and not print if everything is fine and then send errors to stderr:

if [ ! -x /tmp/main.script.ksh ]; then

   print -u2 "main script does not exist in its location (/tmp/main.script.ksh) please check"

   exit 1

fi

 

>CHECK_IT=$(test_it)

 

Do you really want to put the whole content of those logfiles into a variable?

Nadie_1
Frequent Advisor

Re: awk exit status

 

 

also general question if you source a script within some other script to initialise variables etc, obviously that means that script will not just initialise variables if run with (dot space aboslute path) but also execute it's full program rite ?

 

and also fyi--im not sourcing any script but just wanted to understand, other question is if you rely on some other srcript to initialise variable for you and in this case im relying but let's say for example if variables are not set for you but u still want your current script to work how can we achieve this ?

 

 

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>initialise variables if run with (dot space absolute path) but also execute it's full program right?

 

Right.  You should probably solve this by splitting up the init part from the execution and then you can source that variable initialization in multiple places.

 

>let's say for example if variables are not set for you but you still want your current script to work how can we achieve this?

 

Well, you can check to see if they are already initialized and if not, set them:

: ${variable:=default_value}  # set to "default_value" if not set or is null

 

Or if not set, then source that variable initialization script fragment.

 

Or scripts can also return values by echo:

#!/usr/bin/ksh

echo "foo=bar sam=joe"

 

eval $(above-script)  # sets foo and sam

Nadie_1
Frequent Advisor

Re: awk exit status

I could not  thank you enough for all your help and sharing your knowledge, and always responding on time when I needed prompt help.

 

I have worked out everything I wanted thus far with your assistance as it now makes more sense to me, im also reading programing books etc...however I will be writing some more scripts in the very near future so i will continue to use this thread when required-----

 

take care and talk to you soon

Nadie_1
Frequent Advisor

Re: awk exit status

thanks again

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>if [ "$list" != "pathid=0" && $list != "$pathid=1" ]; then

 

This needs "-a" instead of "&&", unless you want to use "[[ ... ]]".

Nadie_1
Frequent Advisor

Re: awk exit status

Thanks again, and I've tried replacing && with '-a' but no difference

 

 

        {

          list=$( some_command|grep -e "pathid=0" -e "pathid=1"|awk '{print $3}' ) # list variable holds the value of # pathids ie pathid=0 and pathid=1--can u pls correct

 

             if [ "$list" != "pathid=0" -a $list != "$pathid=1" ]; then

            print "ERROR: not both pathids found--please check"

             return 1

             else

            print "list is OK"

             return 0

                fi

                   }

######################

 

also in regards to checking ssh dir perm, can u pls correct it so i can avoid using awk or at least not use exit 1 in awk statement ?

 

 

This needs "-a" instead of "&&", unless you want to use "[[ ... ]]".

>user_ssh=$(ls -ld /home/user/.ssh| awk '/^drwx------/ {print $1}') >if [ "$user_ssh" != "drwx------" ]; then

 

The immediate problem is that you are printing $NF instead of $1.

function check_list

 

Dennis Handly
Acclaimed Contributor

Re: awk exit status

>I've tried replacing && with '-a' but no difference

 

Then you need to print the value of $list to see what's there.  Is $3 the right field?

 

>please correct it so I can avoid using awk or at least not use exit 1 in awk statement ?

 

What's wrong with exit 1 in awk?

 

What's in $user_ssh?

 

You could also use grep -q to check permissions.  Or even read to check all at once:

ls -ld /home/user/.ssh | grep -q "^drwx------"

if [ $? -ne 0 ]; then

ls -ld /home/user/.ssh | read permissions link user group dummy

if [ "$permissions" != "drwx------" -o "$user" != $USER -o "$group" != $GROUP ]; then

Nadie_1
Frequent Advisor

Re: awk exit status

I've tried replacing && with '-a' but no difference

 

Then you need to print the value of $list to see what's there.  Is $3 the right field?

> $3 is the right field ? and holds the pathids which im checking

 

>please correct it so I can avoid using awk or at least not use exit 1 in awk statement ?

 

What's wrong with exit 1 in awk?

exit 1 within awk exit's the entire program regardless im trying to avoid any exit 1s

 

and thanks for perms one, i will check that snippet..

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