Two for loops in ksh script only one not executing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Two for loops in ksh script only one not executing
# 1  
Old 09-26-2014
Wrench 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:
Code:
#!/bin/ksh

IFS=' '

printf "Enter Account name[s]: "
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:
Code:
Enter Account name[s]: 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:
Code:
# 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 Smilie
# 2  
Old 09-26-2014
For the first loop, you can read a single variable to hold the account names and set the loop on that:-
Code:
printf "Enter Account name[s]: "
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:-
Code:
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.:-
Code:
printf "Current process is $$\n"



I hope that this helps,
Robin
# 3  
Old 09-26-2014
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:
Code:
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:
Code:
 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):
Code:
 ps -eo pid,args | awk '$2~/BLANKET.CANCEL/ {print $1}' |

And another optimization is to replace the loop by
Code:
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
Code:
for x in `ps -ef`
do
   echo "$x"
done

It's broken into words. A work-around is
Code:
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...

Last edited by MadeInGermany; 09-26-2014 at 03:46 PM..
# 4  
Old 09-28-2014
Thank you all for your help!!! The code below worked perfect Smilie

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

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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Executing ksh script to remote server want output on same window

I'm having a brain freeze moment. I've created a ksh script in AIX that ssh's to a remote server, executes some commands, and then logs out. All of this is sent to a file. I then have the script cat the file so i can see the output. Even though the cat command is outside of the remote session part;... (5 Replies)
Discussion started by: seekryts15
5 Replies

2. Shell Programming and Scripting

executing ksh script

I am trying to call a ksh script from another ksh script. in the called script , i am doing sum calculation(used typeset etc) suppose a.ksh is the calling script and b.ksh is the called script . . b.ksh (used this inside a.ksh) this execution gives some error like bad number. but when i... (1 Reply)
Discussion started by: urfrnddpk
1 Replies

3. UNIX for Dummies Questions & Answers

Executing nested loops+foreach

It's been a while since I used csh formatting and I am having a little bit of trouble with a few things. Things seem so much easier to execute in Matlab, however I need to do this on the terminal because of the programs I am trying to interact with. So here's what I want to do: I have a file... (0 Replies)
Discussion started by: katia
0 Replies

4. Shell Programming and Scripting

KSH nested loops?

KSH isn't my strong suit but it's what my company has to offer. I've got a script with two nested loops, a FOR and UNTIL, and that works fine. When I add a CASE into the mix I end up getting "Unexpected 'done' at line xx" errors. Any suggestions on this? for divi in at ce ci cm co de di fl... (9 Replies)
Discussion started by: mrice
9 Replies

5. UNIX for Dummies Questions & Answers

problem in Executing script in ksh

Hi, I am in ksh. below mentioned 3 commands I executed at command prompt & got the expected results. csh source csh_infa <special command> Now I have to do this in the script in ksh. I entered it as it is. #!/bin/ksh csh source csh_infa <special command> Now after... (1 Reply)
Discussion started by: girish_kanak
1 Replies

6. Shell Programming and Scripting

executing a ksh script from another ksh script

Hi, I'm new to unix scripting.How can i call a script from another script. I have a.ksh and b.ksh .I have to call b.ksh from a.ksh after it is successfully exceuted. I tried using #!/bin/ksh -x in a.ksh and at the end i have used /path/b.ksh My problem is it is executing only a.ksh.it... (6 Replies)
Discussion started by: ammu
6 Replies

7. Shell Programming and Scripting

Full path of executing script in ksh?

Hello all, Here's the scenario: I've got a script, let's call it script1. This script invokes another script, which we'll call set_env, via the dot "." command, like so: File: #!/bin/ksh # region_id=DEV . set_env ${region_id} and so on. Script set_env sets up an... (2 Replies)
Discussion started by: BriceBu
2 Replies

8. Shell Programming and Scripting

ksh question, loops

i want to add about 60 printers using a ksh script. i am having trouble though, i am reading the input from the hosts file and using the lpadmin command to add like so: lpadmin -p -v /dev/null -m netstandard -o dest= i want printername and ipaddy to come from the hosts file, i am having... (1 Reply)
Discussion started by: BG_JrAdmin
1 Replies

9. Shell Programming and Scripting

Executing ksh script from cgi

Hi all, I'm developing a system which requires me to run a ksh script from within a cgi script. What sort of syntax will I need to do this, I'm sure it's simple but can't find out how anywhere! Thanks. (1 Reply)
Discussion started by: hodges
1 Replies
Login or Register to Ask a Question