Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By brewbuck

Bash scripting redirection issue

This is a discussion on Bash scripting redirection issue within the Linux Programming forums, part of the Platform Specific Boards category; I compiled a program exec in c, and I've made some bash code to emulate a series of executions and ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    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
    MK27 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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.
    MK27 likes this.
    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, 05:15 AM
  5. Replies: 6
    Last Post: 06-08-2006, 04:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21