Problem with file redirection in while


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with file redirection in while
# 1  
Old 12-02-2012
Problem with file redirection in while

I've got a problem with file redirection in bash, in loop while (in my script done < bufor.txt). In every consecutive iteration there's a possibility that to the bufor.txt will be added some lines. Unfortunately, in loop, added lines are ignored (for example: bufor.txt has 5 lines, in 3rd iteration scripts add 2 lines to the file, but the loop ends after reading 5th line). Have you got any idea how to fix it? If the number of lines in bufor.txt changes, how to change dynamically number of iterations of loop?

Here's my code (there is no any huge functionality in it, it should look through files and put names included in them [*.h files] in bufor.bf). I want it to work recursively:

Code:
#!/bin/bash
    # kk9
    # 21-11-2012
    # makefile generator
     
    sed -n -e '/^#include "/p' $1 > bufor.b1
    sed -e 's/#include[[:space:]]"//g' bufor.b1 > bufor.b2
    sed -e 's/.h"/.c/' bufor.b2 > bufor.b3
     
    # in bufor.b3 there are now files included in main ($1) file
    cat bufor.b3 >> bufor.bf
     
    # deleting '\r'
    sed -i 's/\r$//' bufor.bf
     
    rm bufor.b1
    rm bufor.b2
    rm bufor.b3
     
    touch analized.bf
     
    while read WIERSZ; do
                    echo "---------------- $WIERSZ"
                    echo "$WIERSZ" > tmp.t
     
                    # deleting '\r'
                    sed -i 's/\r$//' tmp.t
     
                    ZMIENNA=`cat tmp.t`
     
                    echo "VARIABLE: $ZMIENNA"
                   
                    sed -n -e "/^#include \"/p" $ZMIENNA > bufor.b1
                    sed -e 's/#include[[:space:]]"//g' bufor.b1 > bufor.b2
                    sed -e 's/.h"/.c/' bufor.b2 > bufor.b3
     
                    echo "--BUFOR.B3"
                    cat bufor.b3
                    echo "--END OF BUFOR.B3"
                   
                    # checking if the file was analized
                    sed -i 's/\r$//' bufor.bf
                    cat bufor.bf > bufor.cmp
     
                    sed -i 's/\r$//' bufor.b3
                    cat bufor.b3 >> bufor.cmp
     
                    echo "--BUFOR.CMP"
                    cat bufor.cmp
                    echo "--END OF BUFOR.CMP"
                    echo "--BUFOR.BF"
                    cat bufor.bf
                    echo "--END OF BUFOR.BF"
                    echo "--sort BUFOR.CMP"
                    sort bufor.cmp | uniq
                    echo "--END OF BUFOR.CMP SORT"         
                    echo "--sort BUFOR.BF"
                    sort bufor.bf | uniq
                    echo "--END OF BUFOR.BF SORT"  
     
                    rm tmp.t
     
                    CMP1=`sort bufor.bf | uniq | tr -d '[:space:]'`
                    CMP2=`sort bufor.cmp | uniq | tr -d '[:space:]'`
     
                    echo "CMP1:"
                    echo $CMP1
                    echo "CMP2:"
                    echo $CMP2
                   
                    if [ $CMP1 = $CMP2 ]; then
                            echo "CMP1 i CMP2 ARE EQUAL"
                            rm bufor.cmp
                    else
     
                            echo "NEW FILE - CMP1 and CMP2 are not equal!"
                            cat bufor.b3 >> bufor.bf
                            sed -i 's/\r$//' bufor.bf
     
                            echo "BUFOR.BF AFTER CHANGE"
                            cat bufor.bf
                            echo "END OF BUFOR.BF AFTER CHANGE"
     
                            rm bufor.cmp
                    fi
    done < bufor.bf

I attached a package with some test files (executing /.mmf.sh main.c should return: test1.c test3.c test2.c test4.c in bufor.bf; as we can see, test2.c isn't analized in the loop).

Thanks in advance for help.
# 2  
Old 12-02-2012
I tested a while loop in KSH and I observed additional lines added to input file are getting read!

So can you test running this script in KSH instead of BASH and see if you are getting the desired result?

EDIT: Like Scrutinizer mentioned operation performed on the file within the script will not work, but I tried modifying the file outside the script and it works for KSH

Last edited by Yoda; 12-02-2012 at 02:04 PM.. Reason: Clarification
# 3  
Old 12-02-2012
bufor.bf gets fed into the loop and is being read line by line, but at the same time all kinds of operations are performed on this file from within the loop. That will not work..
# 4  
Old 12-02-2012
Thanks for responses.

@Scrutinizer - I wrote a simple script, which adds some lines to the file, redirected to while loop. And the number of iterations increased after modifying the file in loop. The problem is in my implementation of my actual project. In this case, as you have written - it doesn't work. But it is possible to change number of iterations, when new lines are being added to redirected file in iterations of loop.

@bipinajith - It musn't be in ksh - it's for my classes on university and it should be in bash.

Maybe you can see a solution to this problem? Maybe redirection to while is unnecessary?
# 5  
Old 12-02-2012
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

little problem of file redirection (awk)

I almost reach my objective (Youhouuu !!!!) But I really don't understand why it doesn't work until the end... :wall: For clarity's sake I am taking a very simple example. The operations I am doing in the script (gsub and print) really don't have any importance !!! I just matter about... (10 Replies)
Discussion started by: beca123456
10 Replies

2. Shell Programming and Scripting

Problem in redirection of output

Hi All, i m facing very strange problem . like suppose i am in one directory and then i type ls it will display the content of directory then i redirect the output as sneha-pardus $ ls > test sneha-pardus $ cat test a.sh b.sh c.sh sneha-pardus $ awk ' { print $1 } ' test > test... (3 Replies)
Discussion started by: aishsimplesweet
3 Replies

3. Shell Programming and Scripting

Problem of redirection with <<! ... !)

Hi, I have to migrate Korn Shell who call Sybase command into Oracle Command. I have a problem on this example : DATNUM=$( sqlplus -S SA/SASASA@MIGGVDEV <<! SET FEEDBACK OFF; SET HEADING OFF; select sybase_utilities.datediff('DAY', '01/01/1900', '01/01/2000') from... (4 Replies)
Discussion started by: buzzy1804
4 Replies

4. Shell Programming and Scripting

BASH Problem / Question regarding redirection

Hi all, Maybe someone is able to help: Need to redirect the output of a command in realtime to a second command. Command-A executes a remote shell to another host, and outputs its results. Command-B displays a "dialog" with the outputs of Command-A. Command-A Output: Updating FileA... (2 Replies)
Discussion started by: mharald
2 Replies

5. Shell Programming and Scripting

Redirection problem

hi i want a solution for the fallowing: i am redirecting output from bash script in which we are running background commands like ./test.sh contains ls -l & ps when we do as ./test.sh 1> test.txt 2>&1 but test.txt not containing output as ls -l and ps. it showing ls -l, ps and... (2 Replies)
Discussion started by: indianwomen
2 Replies

6. Shell Programming and Scripting

file redirection problem

my querry is suppose i have duplicate std i/p with FD-3 --exec 0<&3 now redirected std i/p to a file ----exec 0<file1 suppose i am reading the file line by line --while read LINE cutting some fields and comparing it with a variable and if a match is found ... (0 Replies)
Discussion started by: mobydick
0 Replies

7. Shell Programming and Scripting

redirection problem

hi all, how to redirect the stdout msg in command line and file at the same time? e.g i got the script named test.sh, content as following: #!/bin/sh echo "111" when i run the script ./test.sh > log.log, it will wirte the "111" into log.log, but how to show the "111" in command line... (2 Replies)
Discussion started by: eric_wong_ch
2 Replies

8. Shell Programming and Scripting

redirection problem

Hello Experts, I am facing the following problem .. >cat my_file prints initial begin StartSim; LDR(on); // Lock Detect Enable But when I trying the same with the following command it shows me all the line in the same line.Like ... (4 Replies)
Discussion started by: user_prady
4 Replies

9. UNIX for Dummies Questions & Answers

Redirection Problem

I was simply trying to redirect some output in a log file but it foxed me. truss a.out > log It gave only the output of the a.out which was only a simple print "Hello" Then I tried truss a.out 2&1>a echo `truss a.out` > a All failed. What am i missing. Thanks (7 Replies)
Discussion started by: vibhor_agarwali
7 Replies

10. UNIX for Dummies Questions & Answers

facing a problem in redirection

Hi, I am doing this perl script print (@line(1..15)); the lines 1 to 15 get printed... how can i redirect this to file? thanks and regards vivek.s (4 Replies)
Discussion started by: vivekshankar
4 Replies
Login or Register to Ask a Question