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/
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.:-
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: