if inside while statement (PLZ HELP)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if inside while statement (PLZ HELP)
# 1  
Old 06-10-2010
if inside while statement (PLZ HELP)

hey guys i need some help here i am trying to do a bash shell script to get a parent PID and then search for all childs and kill them all without killing the parent, my problem is in the loop it seems that i dont know how to make a if statment inside a while statment it does not give me what i want.

Code:
#!/bin/bash

raqam=$(lui | grep EQUATION | nawk '{print $2}') # this will give me the parent PID

ps -ef | grep "$raqam" | nawk '{print $2}' > dump #dumping all PIDs of parent and child to dump

cat dump | while read line
do
        if $line = $raqam
        then
                #i want here to skip this process ID
        fi

kill -9 $line

done

in the
Code:
cat dump | while read line

is my problem i want the script to read every line and execute kill -9 for whats written in the file unless the line is = to $raqam .


can anyone here help plz
# 2  
Old 06-10-2010
Hi,

the problem is that the pipe creates a new sub-shell...try using parenthesis:

Code:
cat dump | (while read line
do
        if $line = $raqam
        then
                #i want here to skip this process ID
        fi

kill -9 $line

done)

# 3  
Old 06-10-2010
Not sure I'm getting you ...

Code:
cat dump | while read line
do
    if [ "$line" != "$raqam" ]
    then
        kill -9 $line
    fi
done

# 4  
Old 06-10-2010
Don't try to win the Useless Use of Cat Award Smilie
Code:
while read line
do
    if [ "$line" != "$raqam" ]
    then
        kill -9 $line
    fi
done < dump

# 5  
Old 06-10-2010
i like the award .. congrats dr.house

do we have useless use of syg commands award??
# 6  
Old 06-10-2010
Quote:
Originally Posted by dazdseg
i like the award .. congrats dr.house
Thanks - but I prefer to focus on replies more essential Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command in inside awk statement

Hello can you please help me with below script which is meant to delete clients from multiple netbackup policies I want to run a command insdie awk statement apparelnlty this script is not working for me for i in $( cat clients_list) do bppllist -byclient $i | awk... (6 Replies)
Discussion started by: Sara_84
6 Replies

2. Shell Programming and Scripting

Multiple conditions inside if statement

I was trying to write multiple conditions inside the if statement but its not working. export VAR_NM=abc.txt export CURR_DT=20131011 export PREV_DT=20131012 if && then echo "Yes" else echo "NO" fi It should return Yes but returning NO always.Appreciate any help. (3 Replies)
Discussion started by: dr46014
3 Replies

3. Shell Programming and Scripting

Use sqlplus statement inside case sentence

Hello, I have a problem. I want to launch a different sql queries for different shell parameter values, something like this. #/bin/bash case $1 in "A") sqlplus -s user/pass << SQL query A; SQL "B") sqlplus -s user/pass << SQL2 ... (3 Replies)
Discussion started by: Vares
3 Replies

4. Shell Programming and Scripting

issues with sql inside if statement

Hi, I have problem with the following code. My IF block is not executed. And I see "syntax error near unexpected token `)'" error for line "EOF" in the stats_function(). but when I comment the IF block I don't see this error. Kindly help me with this issue. clean_function() {... (10 Replies)
Discussion started by: babom
10 Replies

5. Shell Programming and Scripting

Multiple conditions inside my if statement

Hello, I am using shell scripting and I am recieving odd results from my if statement if I want it to enter the loop only if L1 is equal to zero and one of the other criteria are filled, however it is entering at other times as well. What can i do to fix this? i tried seperating it... (6 Replies)
Discussion started by: ryddner
6 Replies

6. Shell Programming and Scripting

Sysdate inside awk print statement

Hi, I am using awk statement to extract data from a file and write a new file with certain columns rearranged and few hard coded values added to new file. Now i need to add a column with sysdate. can i do that inside the awk print statement? Now: nawk ' /^3/ BEGIN {FS=","}... (2 Replies)
Discussion started by: selvankj
2 Replies

7. Shell Programming and Scripting

Read from user inside a while statement

Hi there I want to ask to a user something about each line of a file. This is my code: #cat test.txt first line second line third line #cat test.sh #!/bin/ksh read_write () { echo "Do you want to print the line (YES/NO)?\n" read TEST case ${TEST} in YES) return 0;; ... (3 Replies)
Discussion started by: tirkha
3 Replies

8. Shell Programming and Scripting

multiple lines inside a case statement

echo "please enter ur choice.. 1. Make a file. 2. Display contents 3. Copy the file 4. Rename the file 5. Delete the file 6. Exit" read choice case $choice in 1 ) echo enter the file name read fname if then echo... (2 Replies)
Discussion started by: gotam
2 Replies

9. Shell Programming and Scripting

Exit statement ignored inside KSH function

Hi All, I have multiple functions in my script and I'm trying to capture stdout from some of them, but I also do some error checking in them (in the functions that output something to their stdout that needs capturing) and I need to be able to end the entire script with an error message. ... (2 Replies)
Discussion started by: gkubok
2 Replies
Login or Register to Ask a Question