While loop reading file with multiple conditions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While loop reading file with multiple conditions
# 1  
Old 05-07-2013
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..

Code:
#!/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
Code:
# ./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..
# 2  
Old 05-07-2013
Why are you using two [ ? you have to use (..

Unix interpretes [ to test command
This User Gave Thanks to vidyadhar85 For This Post:
# 3  
Old 05-07-2013
BTW perhaps you can simplify
Code:
ps -ef | grep "ftpd" > PIDList
cat PIDList | awk '{print $2}' > ValidPIDList.txt

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

And then use
Code:
while [ $lineCount -le $Count ] && [ $pid -eq 0 ] && read LINE

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sequential Reading from two file in a loop

Hello All, I have two files with me file1.txt and file2.txt file1.txt has: 333 222 111 file2.txt has ccc bbb aaa ccc is related to 333 only, bbb is related to 222 only and aaa is related to 111 only. I have to get the values from each of the file and pass them in the URL... (3 Replies)
Discussion started by: ankur328
3 Replies

2. UNIX for Dummies Questions & Answers

Reading multiple variables in a loop

Hi, I managed to read and print variable as shown in the below code. table_name=table1,table2,table3 i=0 IFS="," for i in $table_name do echo $i done Is there a way how I can read more than one variable. For example I need to read 2 variables and populate the output... (6 Replies)
Discussion started by: shash
6 Replies

3. Shell Programming and Scripting

Removing \r and \n during reading file through while loop

Hi, I am writing in a file through cat command. This file will contain the path of file along with filename. e.g. /home/user/folder1/folder2/filename.txt There might be very large number of this path in same file like say 140 when I try to run while command: while read -r file do //command... (8 Replies)
Discussion started by: Pulkit Lall
8 Replies

4. Shell Programming and Scripting

Reset while loop to loop same file multiple times

Hi, I want to read file multiple times. Right now i am using while loop but that is not working. ex. While read line do while read line2 do echo stmt1 #processing some data based on data., done < file2.txt done < file1.txt # This will have 10... (4 Replies)
Discussion started by: tmalik79
4 Replies

5. Shell Programming and Scripting

Reading multiple values in while loop

I'm having trouble with a simple piece of code. IFS=, echo "1,2,3,4,5,6,7,8" | while read x y do echo "x=$x" echo "y=$y" done I'm hoping for x=1 y=2 x=3 y=4 . . . but I'm getting x=1 (3 Replies)
Discussion started by: sabbata
3 Replies

6. Shell Programming and Scripting

Loop is not reading tabs from the file

Hi, I am on HP-UX and K shell. When I am using while/for loop for reading a file. It is working fine but not reading tabs: Suppose, if the line is: ; ;COMP; ; ; ; then loop is reading as ; ;COMP; ;... (5 Replies)
Discussion started by: ezee
5 Replies

7. Shell Programming and Scripting

Read file using while loop not reading last line

I have written a script to read the file line by line. It is reading and printing the lines. But it is coming out of loop before reading last line. So I am not able to print last line. How do I solve it. (6 Replies)
Discussion started by: dgmm
6 Replies

8. AIX

How to pause a while loop while reading from a file

Hi, I am building a script to grep for a string in all the files from a folder and display the results. I am reading the files one by one by placing the names in other file using while loop my code is as below while read inp do chk=`grep -c "$str" $pth/$inp` ... (2 Replies)
Discussion started by: sekhar gajjala
2 Replies

9. Shell Programming and Scripting

Not access variable outside loop when a reading a file

I am writing a shell script using the korn shell. It seems that I am only able to use local variables within a while loop that is reading a file. (I can't access a variable outside a previously used while loop.) It's been a while since I wrote shell scripts. Here is a sample cat file.txt... (4 Replies)
Discussion started by: ricardo.ludwig
4 Replies

10. HP-UX

How to make a loop base on reading a file?

Need help on making a loop script base on what is inside a file... File to read: List.txt List.txt contains below w/c are file name as well: SAMPLEa SAMPLEb SAMPLEc SAMPLEd SAMPLEe SAMPLEf . . . Want to make a loop that will manipulate those that are inside the file.txt w/c are... (3 Replies)
Discussion started by: JohnBalayo
3 Replies
Login or Register to Ask a Question