Kill pids in a text file column

My program read  in and loop through a column in a text file that contains pids.  kill -9 kills all the pids in the column. The program errors and do not kill the pids in the column of foo4.txt .

!#bin/bash
set vi
while read $f1; do
    echo "$f1"
    kill -9 $f1
done < "/path/foo4"

The foo.txt: Contents  in the foo4.txt is as follows:

PID
345678
678901
987659
456780
234567
456790
345671
234567
dfn48Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
The shebang must read "#!/bin/bash"
"read"  the variable "f1" not the variable's content "$f1"
"set vi" should  rather be "set -o vi", but is not meaningful in a script like this.

#!/bin/bash
while read f1; do
    echo "$f1"
    kill -9 $f1
done < "/path/foo4"

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gerwin Jansen, EE MVETopic Advisor Commented:
The loop is not really necessary, this should work as well:

kill -9 $(cat /path/foo4)
woolmilkporcCommented:
I just saw that the first line of your text file is "PID". You should get rid of this.

#!/bin/bash
grep -v "PID" /path/foo4 | while read f1; do
    echo "$f1"
    kill -9 $f1
done

Or, following Gerwin's suggestion:

kill -9 $(grep -v "PID" /path/foo4)

Please note that the combined length of all arguments to "kill" plus the size of the environment
must not execeed ARG_MAX (check with "getconf ARG_MAX"), so for a very, very huge input file this might be safer:

grep -v "PID" /path/foo4 | xargs kill -9
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.