While loop reading file with multiple conditions

Hi
Am trying to print the PIDs of process in a file and trying to grep any PID from that file
I set the if condition as $value != "PID" and $value != "-"
Assign that number to a variable
Am confused since am using while loop to read the line from file
and again if condition to check those values

I need, when if condition is executed the while loop should also break..
So i used "echo $?' to set the return value according to if condition.. but they doesnt work please help..

#!/usr/bin/ksh
set -x
ps -ef | grep "ftpd*" > PIDList
cat PIDList | awk '{print $2}' > ValidPIDList.txt
#echo $ValidPID > ValidPIDList.txt
lineCount=1
pid=0
Count=`wc -l ValidPIDList.txt`
FILE=ValidPIDList.txt
while [[ $lineCount -le $Count ] && [ $pid -eq 0 ]] && read LINE
do
    if [[ $LINE -ne "PID" || $LINE != "-" ]]; then
        pid=$LINE > ValidPIDList.txt_grepped
        echo "The PId is $pid\n"
    fi
linecount=$(($linecount+1))
done < $FILE

The output is

# ./validPID.sh
+ ps -emo THREAD
+ 1> PIDList
+ cat PIDList
+ awk {print $2}
+ 1> ValidPIDList.txt
+ lineCount=1
+ pid=0
+ + wc -l ValidPIDList.txt
Count=     346 ValidPIDList.txt
+ FILE=ValidPIDList.txt
./validPID.sh[10]: 0403-057 Syntax error at line 10 : `]' is not expected.

Please help..

Why are you using two [ ? you have to use (..

Unix interpretes [ to test command

1 Like

BTW perhaps you can simplify

ps -ef | grep "ftpd" > PIDList
cat PIDList | awk '{print $2}' > ValidPIDList.txt

to

ps -e | awk '/ftpd/ {print $1}' > ValidPIDList.txt

And then use

while [ $lineCount -le $Count ] && [ $pid -eq 0 ] && read LINE