Visit Our UNIX and Linux User Community


C Shell - Command Inside a Loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting C Shell - Command Inside a Loop
# 1  
Old 06-16-2009
C Shell - Command Inside a Loop

I have a command nested in some while loops to parse some data that looks something like this.

Code:
while ($condition)
  while ($condition)
    ...
    gzcat /dir/$fileName.gz | grep $searchString > out_file
  end
end

On the first loop, the command is executed properly (and takes maybe 10 seconds to finish). But on all subsequent loops, something happens and the command is not executed properly. I know this because the data I expect does not get output to "out_file" and the process takes a split second, whereas it should take the usual 10 seconds.

To make sure the command is not getting messed up during the looping, I put an echo just before the command is executed, so it looks like this.

Code:
while ($condition)
  while($condition)
    ...
    echo "gzcat /dir/$fileName.gz | grep $searchString > out_file"
    gzcat /dir/$fileName.gz | grep $searchString > out_file
  end
end

The echo gives me what I expect.

Code:
gzcat /dir/file1.gz | grep string1 > out_file
gzcat /dir/file2.gz | grep string2 > out_file
gzcat /dir/file3.gz | grep string3 > out_file

If I run those 3 commands in a row at the prompt manually, I get the results I expect. But inside the script, only the first loop of the command gets the results. Any ideas what could be happening?

Thanks,
hobbers
# 2  
Old 06-17-2009
Hi
Try to insert the instruction
PHP Code:
exit 
between the commands

Cheers
# 3  
Old 06-17-2009
Quote:
Originally Posted by hobbers
I have a command nested in some while loops to parse some data that looks something like this.

Code:
while ($condition)
  while ($condition)
    ...
    gzcat /dir/$fileName.gz | grep $searchString > out_file
  end
end

On the first loop, the command is executed properly (and takes maybe 10 seconds to finish). But on all subsequent loops, something happens and the command is not executed properly. I know this because the data I expect does not get output to "out_file" and the process takes a split second, whereas it should take the usual 10 seconds.

To make sure the command is not getting messed up during the looping, I put an echo just before the command is executed, so it looks like this.

Code:
while ($condition)
  while($condition)
    ...
    echo "gzcat /dir/$fileName.gz | grep $searchString > out_file"
    gzcat /dir/$fileName.gz | grep $searchString > out_file
  end
end

The echo gives me what I expect.

Code:
gzcat /dir/file1.gz | grep string1 > out_file
gzcat /dir/file2.gz | grep string2 > out_file
gzcat /dir/file3.gz | grep string3 > out_file

If I run those 3 commands in a row at the prompt manually, I get the results I expect. But inside the script, only the first loop of the command gets the results. Any ideas what could be happening?

Thanks,
hobbers


Do the following and prvide me with your feedback please:-

Code:
while ($condition)
  while ($condition)
    ...
    gzcat /dir/$fileName.gz | grep $searchString >> out_file &
    wait
  end
end

# 4  
Old 06-18-2009
Quote:
Originally Posted by Hammadi dali
Hi
Try to insert the instruction
PHP Code:
exit 
between the commands

Cheers
This caused the script to stop after the first command.

Quote:
Originally Posted by ahmad.diab
Do the following and prvide me with your feedback please:-

Code:
while ($condition)
  while ($condition)
    ...
    gzcat /dir/$fileName.gz | grep $searchString >> out_file &
    wait
  end
end

This works. But after further investigation, it may have also been a file availability problem with the *.gz files.

Thanks.

Previous Thread | Next Thread
Test Your Knowledge in Computers #747
Difficulty: Medium
The Z80 uses 248 out of the available 256 codes as single byte opcodes.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to write a Boolean variable which succeed and failed inside the if loop in shell script ?

I have if loop with multiple variable value check in if loop. How can i print only if loop satisfied variable and its value in shell script ? I dont want to check each variable in if loop. That makes my script larger. if ] then echo "Only satisfied variable with value" ... (3 Replies)
Discussion started by: prince1987
3 Replies

2. Shell Programming and Scripting

CUT command not giving correct result inside loop

Hi, i have a source file and have 3 columns and separated by "|" .i want to split this 3 columns in different variable.When i am executing this values indivisually giving correct result but when the same execute inside a for loop,it's giving issues. Src file(jjj.txt) -------... (8 Replies)
Discussion started by: raju2016
8 Replies

3. Shell Programming and Scripting

How to execute a command inside a while loop?

How do we execute a command inside a while loop? (7 Replies)
Discussion started by: Little
7 Replies

4. Shell Programming and Scripting

If else condition inside for loop of awk command in UNIX shell scripting

Hi , Please excuse me for opening a new thread i am unable to find out the syntax error in my if else condition inside for loop in awk command , my actual aim is to print formatted html td tag when if condition (True) having string as "failed", could anyone please advise what is the right... (2 Replies)
Discussion started by: karthikram
2 Replies

5. Shell Programming and Scripting

help in running while loop inside a shell script

I have an input file at ./$1.txt with content of seq numbers like : 1234567890 1234589700 . . so on.. I need to take each seq nbr from the input file ,run the query below: select ackname,seqnbr from event where event_text like '%seqnbr( from the input file)' and redirect the... (11 Replies)
Discussion started by: rkrish
11 Replies

6. Shell Programming and Scripting

how to use sqlplus command inside for loop

I tried this: for region in 'raj' 'kt' 'kol' 'krl' 'chn' 'dl' 'hr' 'bih' 'ap' do sqlplus -s huw$region/`echo $region`huw#321@huw$region<<! set serveroutput on begin select count(*) from tcd_preferred_cust_201109 end; / exit; done but the error shows like: Syntax error at line 4 :... (1 Reply)
Discussion started by: deepakprasad29@
1 Replies

7. Shell Programming and Scripting

How to give a variable output name in a shell script inside a for loop

Hi all I run my program prog.c in the following way : $ ./prog 1 > output.txt where 1 is a user defined initial value used by the program. But now I want to run it for many a thousand initial values, 1-1000, and store all the outputs in different files. Like $ ./prog 1... (1 Reply)
Discussion started by: alice06
1 Replies

8. Shell Programming and Scripting

loop to replace txt and add command inside of colon (:)

in one of my script..I have some thing like john:christina : one:: and i want to make it like john:chritina:two:(date command):jackey: basically change 'one' to 'two' and run date :command and add other name: (30 Replies)
Discussion started by: Learnerabc
30 Replies

9. Windows & DOS: Issues & Discussions

How to log current timestamp inside a for loop in windows shell?

below is the output where i'm getting the same times tamp throughout the loop., where i'm expecting that to log the current time stamp.. C:\Users\ilango>for /l %i in (1,1,5) do echo for %i C:\Users\ilango>echo for 1 for 1 C:\Users\ilango>echo for 2 for 2 C:\Users\ilango>echo... (1 Reply)
Discussion started by: ilan
1 Replies

10. Shell Programming and Scripting

read command (input) inside the while loop

Hi, 'read' command is not working inside the while loop, How can I solve this? Rgds, Sharif. (2 Replies)
Discussion started by: sharif
2 Replies

Featured Tech Videos