retreiving and assigning values and manipulating string in a for loop

Hi

I am new to shell scripting and i am preparing a script.
for now i am work on a sub part of it..but i am unable to make it work.

---
the test code that i am working on
--------------------------
IFS=""
Sample_eve=`psg proc_s | grep tY`
n=0
for line in $Sample_eve
do
n=`expr $n + 1`
Sam$n=$(`echo $line |awk -F" " {print $5}' |awk -F":" '{print $2}'`)
echo $Sam$n
done
----------------------------
what i am trying to this is..
list all the process named proc_s that are currently running and are on the machine tY..
populate them in the variable Sample_eve.
and then access each line from the Sample_eve and process them on an individual basis.
but it is not happening ..for loop only goes through once..if i do not set IFS="" then line takes word by word data from the $Sample_eve...by which i cannot get seperate start time for each process. (it runs 35 times for 4 lines..i want to make it run only 4 time for 4 line).

i am doing `echo $line |awk -F" " {print $5}' |awk -F":" '{print $2}'` ..so as to get the start time for each process in minutes..then i am assigning it into array Sam so as to get the start Minute for each process.
but getting an output error like this..
for this array assignment as Test[9]: 04^J28^J04^J09: not found.
04, 28,04,09 are correctly shown they are the minutes ..but is not in a proper manner.

i am stuck with this thing .. please help me. I am unable to make the code work.

i am using the korn shell.
thanks

You can not do like this... $ should not come on the left side.
This will work...

Sam=`echo $line |awk -F" " {print $5}' |awk -F":" '{print $2}'`

-----------

Hi thanks.. the code you gave worked.

but further while accessing the array Sam. i am having some issues.

1: When i echo ${Sam[$2]} rather than giving the 2nd element it prints all of them

2: for (( i = 0 ; i < ${#Sam[@]} ; i++ ))
do
echo ${Sam[$i]}
done
its throwing an error
Test[16]: syntax error at line 17 : `((' unexpected

can you please tell how to go about accessing the array..

We can not write a for loop like this, unless you are using awk.
I am giving an example of Array and for loop for you...

set -A _Array 1 2 3 4 5
for i in ${_Array[@]}
do
   echo $i
done

Yeah . but what to do if only the second element of the array Sam has to be echoed..

and why does echo ${Sam[$2]} prints the entire Sam array??

this way...

echo ${_Array[1]}

Tried..but its not printing anything...any other way u know to do this.

the test code is

    IFS=""
    eve=\`psg ftp | grep tV\`
    n=0
    for line in  $eve
    do
       n=\`expr $n \+ 1\`
       echo $line
       array=\`echo $line |awk -F" " '\{print $5\}' |awk -F":" '\{print $2\}'\`
       echo  $\{array[2]\}
      done

Try something like this:

n=0
psg proc_s | awk '/tY/{split($5,a,":");print a[2]}'| while read s
do
  Sam[$n]="$s"
  n=$(( $n + 1))
  # other code
  # ....
done
echo $Sam[1]
psg proc_s | awk '/tY/{
m=split($5,a,":")
sam[FNR]=a[2]
}
END{
    for(i in sam){
     print i
    }
}    '

Hi Franklin ..thanks

tried but its not working...echo $Sam[1] is just giving [1].

i also tried to echo the value on n inside the while loop but it is also not printing..

how can i make this run????

-----Post Update-----

Hi

the following code
for(i in sam){
print i
}
for(i in sam){
echo $i
}

both are not showing any output

-----Post Update-----

--------------------------------

Please help...what i want..
1)is to get the time in minutes for a particular process
2)from the list of process generated i have to get its command name and arguments
3)and if it is running for more than 50 mins and has more than 2 argument i have to kill it..

but im unable to complete the no.1 also...please help
thanks.

------------

hi ghostdog74

why have u used
sam[FNR]=a[2]

is it pre declaration of the array Size..as we do in c..
i donno things im asking are silly or not..but im entirely new to shell script and any help from u guys is apreciated..
thanks

I apologize, the variable is only available outside the loop in some shells:

n=0
psg proc_s | awk '/tY/{split($5,a,":");print a[2]}'| while read s
do
  Sam[$n]="$s"
  n=$(( $n + 1))
  echo ${Sam[$n]}
  # other code
  # ....
done

Its still not echoing..

Post the output of the command psg proc_s within code tags (select the text and click on the # above the edit box).

n=0
event=`psg proc_s | grep de0`
for line in $event
do
  echo $line
done
 
#...............

n=0
psg proc_s | grep  de0 | awk '/tY/{split($5,a,":");print a[2]}'| while read s
do
  Sam[$n]="$s"
  n=$(( $n + 1))
  echo ${Sam[$n]}
  # other code
  # ....
done

Output
s04:11:/u/Script>Test
crmcint 15326 1 0 14:16:27 ? 0:01 proc_s de08.c
crmcint 16428 1 0 14:35:49 ? 0:00 proc_s de01.cl
crmcint 10272 1 0 13:12:48 ? 0:06 proc_s de03.c
s04:11:/u/Script>

Is this the output of the command psg proc_s?

yeah the above output is when i also grep the psg proc_s output

when only psg proc_s is run at command promt it gives the following output..

jnunly 10612 1 0 13:18:21 ? 0:02 proc_s gsCTrip.c
crmcint 15326 1 0 14:16:27 ? 0:01 proc_s de08.c
crmcint 16428 1 0 14:35:49 ? 0:00 proc_s de01.c
rscle 3481 1 0 Apr 13 ? 0:01 proc_s dt07.c
crmcint 22356 1 0 15:27:21 ? 0:00 proc_s rune.c
crmcint 10272 1 0 13:12:48 ? 0:06 proc_s de03.c

make sure in your code, you are grepping things that are there....
from your sample, i don't see /tY/, therefore, why are you using /tY/ in your code?

yes..Sorry
tY is not there.

so substituted in place of tY .. de0 in the frankiln52's code.
i.e. and also added some echos to see what values are going into the array..

 
IFS=""
event=`psg proc_s | grep de0`
for line in $event
do
  echo $line
done
 

n=0
psg proc_s | awk '/de0/{split($5,a,":");print a[2]}'| while read s
do
  Sam[$n]="$s"
  n=$(( $n + 1))
  echo ${Sam[$n]}
  echo ${Sam[0]}
   echo ${Sam[1]}
   echo ${Sam[2]}
   echo ${Sam[3]}
   echo ${Sam[4]}
    echo ${Sam[5]}
  # other code
  # ....
done
echo "time is ${Sam[5]}"
echo $n

and got the output as five blank lines..
echo ${Sam[$n]} seems to be printing blank lines.

Output:

crmcint 15326 1 0 14:16:27 ? 0:01 proc_s de081.c
crmcint 16428 1 0 14:35:49 ? 0:00 proc_s de08.c
crmcint 10272 1 0 13:12:48 ? 0:06 proc_s de01.c
crmcint 20229 1 0 15:06:23 ? 0:00 proc_s de05.c
crmcint 18285 1 0 14:51:38 ? 0:00 proc_s de083.c
crmcint 18902 1 0 14:57:02 ? 0:01 proc_s de083p.c
16

16
35

16
35
12

16
35
12
06

16
35
12
06
51

16
35
12
06
51
57
time is 57
6

There's one more bump in the code :eek:, the increment of n in the loop should be after the echo statement.

Regards

Hi guys.. thanks the code is running.
thanks all
Regards.

-----Post Update-----

Hello

is it possible in the above code to get the number of splits that have happened over her..
suppose if the above code goes like this

psg proc_s |awk -F" " '{print all fields }''| while read s
do
n=$(( $n + 1))
Sam[$n]="$s"
done

now in case if i do echo ${#Sam[0]} it gives number of charachters in the Sam[0] array..what if i have to find number of words...i.e to get the
number of spilts per line..how can it be done