Two for loops in ksh script only one not executing

Hello,
I have two "for loops" in my script and the second one is not executing the way i want.

Script:

#!/bin/ksh

IFS=' '

printf "Enter Account name: "
read A B C D E F G H I J K L M N O

        for i in ${A} ${B} ${C} ${D} ${E} ${F} ${G} ${H} ${I} ${J} ${K} ${L} ${M} ${N} ${O}; do

        # Check disk space
        echo
        df -g /home/$i
        done
        echo

        # Show the parent directory and pid
        for x in `ps -ef | grep BLANKET.CANCEL | grep -v grep | awk '{print $2}'`;do procwdx $x;done

Script output:

Enter Account name: MUSE SPORTS

Filesystem    GB blocks      Free %Used    Iused %Iused Mounted on
/dev/fslv01        6.00      1.15   81%    90760    25% /home2

Filesystem    GB blocks      Free %Used    Iused %Iused Mounted on
/dev/hd1          21.75      3.83   83%   208763    18% /home

procwdx : no such process : 15401052
17170584
18481212
23920844
28573800

When I run the second "for loop" out of the script it works fine:

# for x in `ps -ef | grep BLANKET.CANCEL | grep -v grep | awk '{print $2}'`;do procwdx $x;done
15401052:       /home2/MUSE/
17170584:       /home/ARTSDEMO/
18481212:       /home/ARTS/
23920844:       /home/SPORTS/
28573800:       /home2/ARENA/

Please help on what I'm doing wrong :confused:

For the first loop, you can read a single variable to hold the account names and set the loop on that:-

printf "Enter Account name: "
read A
for i in ${A}
do
   printf "Working on account ${i}\n"
done

The second loop confuses me a little. Assuming that you are looking for all process, you can slim this down too:-

ps -ef | grep BLANKET.CANCE[L] | cut -f2 -d" " | while read a x b
do
   procwdx $x
done

The a grabs the first item on the line, the x and the b grabs the rest.

The square brackets in the grep part of the line form an expression with only one option, i.e. and L , but it means that you will not match the grep command itself.

It is possible that you code as it stands is getting the process id of the running script. Pop in a simple message at the beginning to see if that matches.:-

printf "Current process is $$\n"

I hope that this helps,
Robin

ps can have multi-space delimiters, while cut -d" " handles one-space delimiters.
[ ] must be within quotes, otherwise the shell matches it against the current directory.
Enough reasons for awk:

ps -ef | awk '/BLANKET.CANCE[L]/ {print $2}' |
while read x
do
  procwdx $x
done

Maybe AIX ps has Posix options? These are more efficient:

 ps -eo pid,args | awk '/BLANKET.CANCE[L]/ {print $1}' |

Most efficient would be to only grep for the command not its arguments (and you don't need the [ ] trick):

 ps -eo pid,args | awk '$2~/BLANKET.CANCEL/ {print $1}' |

And another optimization is to replace the loop by

xargs procwdx

---------- Post updated at 01:41 PM ---------- Previous update was at 01:27 PM ----------

Back to the original problem.
A for loop on multi-word lines is problematic

for x in `ps -ef`
do
   echo "$x"
done

It's broken into words. A work-around is

for x in "`ps -ef`"
do
   echo "$x"
done

But, as I see now, it is filtered by awk, that gives one word per line, and I don't see a real problem...

Thank you all for your help!!! The code below worked perfect :slight_smile:

ps -ef | awk '/BLANKET.CANCE[L]/ {print $2}' |
while read x
do
  procwdx $x
done

output:

15401052:       /home2/MUSE/
17170584:       /home/ARTSDEMO/
18481212:       /home/ARTS/
23920844:       /home/SPORTS/
28573800:       /home2/ARENA/