Thread: Bash scripting redirection issue

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    7

    Bash scripting redirection issue

    I compiled a program exec in c, and I've made some bash code to emulate a series of executions and place the output in some file. However it just interprets one execution and appends it 15 times to the outfile.

    Code:
    #!/bin/bash
    
    count=15
    
    while [ $count -gt 0 ]; do
        ./exec < infile.txt >> outfile.txt
        let count=$count-1
    done


    The main program contains randomization of numbers based on the system, and it always yields the same set of random numbers in the outfile.
    However when I provide the line '
    ./exec < infile.txt >> outfile.txt' to the command line multiple times the numbers are randomized as they should be.

    How can I get it to interpret each execution separately and not just pass one execution 15 times into the outfile?



    Cheers guys.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Are you suggesting that the source for ./exec contains srand(time(NULL)) ?

    Because if you are, then running the code in such a tight loop WILL result in the same random values each time, because it will be too quick for time to change.

    To better simulate keyboard/user input, try
    Code:
    #!/bin/bash
    
    count=15
    
    while [ $count -gt 0 ]; do
        ./exec < infile.txt >> outfile.txt
        let count=$count-1
        sleep 2 # a slow user simulator
    done
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    7
    Thanks that seems to of worked. However, what if I want to run 1000 simulations without having to wait for ever?
    I've tried setting it a tenth of a second for instance, or lower, but it seems like it can only change its random values every second.

    Thanks.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    7
    Just figured it out.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I've done this before:

    Code:
    srand(time(NULL) + getpid());
    Each execution will have a different PID so even if time(NULL) returns the same time as last time, the seed will be different.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redirection Issue
    By Oblivious88 in forum C Programming
    Replies: 5
    Last Post: 04-08-2009, 03:13 AM
  2. Linux file redirection buffering issue !!!!
    By AirSeb in forum Linux Programming
    Replies: 8
    Last Post: 03-14-2009, 05:32 PM
  3. BASH scripting.
    By Kennedy in forum Tech Board
    Replies: 6
    Last Post: 05-06-2007, 06:13 PM
  4. bash scripting?
    By Draco in forum Linux Programming
    Replies: 1
    Last Post: 01-08-2007, 06:15 AM
  5. Replies: 6
    Last Post: 06-08-2006, 04:11 PM