Converting string to integer

I have a function that is supposed to check for user processes and wait for 0 count before exiting the function. I am sure I have more than one issue in my code, but the stumbling block right now is that I am trying to convert the value of my variable from a string to integer.

process_count needs to be an integer to be evaluated in the while loop but while debugging I see that it is " 0" not "0". Any help will be greatly appreciated!

function CheckProcessCount
{
        #acively checks user processes for 0 count

                curent_user=`whoami`
                current_process=$$
                parent_process=$PPID

                process_count=`ps -ef|grep $current_user|grep -v $current_process|grep -v $parent_process|grep -v grep|grep -v ps|grep -v DomainDaemon|grep -
v -- -ksh|wc -l`

                while [[ $process_count != 0 ]]
                do ps -ef|grep $current_user|grep -v $current_process|grep -v $parent_process|grep -v grep|grep -v ps|grep -v DomainDaemon|grep -v
 -- -ksh|wc -l
                done
}

What OS do you have, there are a lot of useful commands to deal with this kind of problem?

If you need process trees, for example, there are commands like ptree , pgrep and id that can help depending on your flavor of UNIX.

Thanks, Jim. I have AIX 6.1

I don't think you need to convert yourself. Some (most?) shell do this implicitly if you use the integer operators:

A=0
B=" 0"
[ "$A" -eq "$B" ] && echo OK
+ '[' 0 -eq ' 0' ']'
+ echo OK
OK

Not quite so when using string operators:

[ "$A" = "$B" ] && echo OK || echo NOK
+ '[' 0 = ' 0' ']'
+ echo NOK
NOK

I have changed it to include quotations around the variable in the comparison and use an integer operator, per your suggestion, RudiC and still not working for me:

Code:

                curent_user=`whoami`
                current_process=$$
                parent_process=$PPID

                process_count=`ps -ef|grep $current_user|grep -v $current_process|grep -v $parent_process|grep -v grep|grep -v ps|grep -v DomainDaemon|grep -
v -- -ksh|wc -l`

                while [ "$process_count" -ne 0 ]
                do ps -ef|grep $current_user|grep -v $current_process|grep -v $parent_process|grep -v grep|grep -v ps|grep -v DomainDaemon|grep -v
 -- -ksh|wc -l
                done

Output

process_count=       0
+ [        0 -ne 0 ]
/db/scripts/review_load.ksh[59]: test: argument expected

I see you're using AiX. What shell interpreter the script is written for?
What's on the first line of the script? Something like that?

#!/usr/bin/ksh

The script is simply a standalone function that is called by a ksh script with the first line "#!/usr/bin/ksh"

hmmm... strange
don't have AiX in front of me, but try:

typeset -i process_count=`ps -ef|grep ....`

Thanks, tried the typeset -i approach and still get the same failure:

+ typeset -i process_count=       0
+ [ 0 -ne 0 ]
/db/scripts/review_load.ksh[59]: test: argument expected

Have tested:

# add to to the pipe : delete spaces
# | tr -d " "
# and then make string comparing
while [ "$process_count" !=  0 ]

In AIX the ps command itself has such a provision: ps -T

On the other hand:

process_count=`ps -ef|grep $current_user|grep -v $current_process|grep -v $parent_process|grep -v grep|grep -v ps|grep -v DomainDaemon|grep -
v -- -ksh|wc -l`

Isn't there "|kitchen|sink" missing somewhere? ;-))

First, instead of using backticks, please use what is standard in ksh: POSIX-like substitution: $(command|command...) .

Second: instead of doing a "ps -ef" and then grepping out a lot of things you do not need you could limit the output to the processes of a certain user by using the "-u"-option. Most of the other grep s are equally redundant because by grep -v $current_process you already filter out all the children too - they have the current process' PID as their PPID, no?

Then: the output of "wc -l" is not a number, but a text. A text with some leading blanks and this simply does not translate into a number, at least not with the backticks you use. Try the following:

typeset -i number=0
print - $(ps -fe | ....|wc -l) | read number

And finally: you want tthe function to wait until the process_count drops to zero. But you set process_count only once and then loop based on the value without ever changing it. Isn't that simply an endless loop if process_count is unequal to 0?

You might want to write something like:

while [ $(ps | [....] | wc -l) -gt 0 ] ; do
     sleep 1
done

I hope this helps.

bakunin

1 Like