How to read from a .dat file in Unix

Hi All,

I have a .dat file named test.dat where I have stored some process IDs.
Now I need to pick a process ID, one by one and then fire kill -9 for each of those. The logic should be:

  1. open file <filename.dat>
  2. read until last line of file
  3. if process ID is found fire kill -9 <process ID>

Can anyone help me out with the Unix script for this?

Please help urgently.

Thanks in advance,
Sibasish.

Assuming filename.dat contains a single pid per line

#!/bin/ksh

while read pid
do
  while kill -0 $pid 2> /dev/null
  do
    kill -9 $pid 2> /dev/null
  done
done < filename.dat

Can you really explain the script? I am really new to Unix and unable to understand

I shall explain each while loop separately.

while read pid
do
  echo $pid
done < filename.dat

Read lines from filename.dat one at a time and print them.

while kill -0 $pid 2> /dev/null
do
  kill -9 $pid 2> /dev/null
done

kill -0 $pid can be used to detect if the process denoted by the pid $pid is alive or not. If it is alive, then issue a kill -9 $pid till it dies.

My .dat file abc.dat has only two lines

233465
234566.

Now what should I do?

If you understood my previous post, then you will know what to do. Where are you stuck ? What is it that you have not understood ?