Find the answer to your Linux question:
Results 1 to 4 of 4
I have a script that converts every flac file in a directory into a wav file, and then converts the wav files into mp3s. I want to allow this script ...
  1. #1
    Just Joined!
    Join Date
    May 2005
    Location
    Dallas, Texas
    Posts
    95

    How to have a bash function running in parallel make changes to a global variable?

    I have a script that converts every flac file in a directory into a wav file, and then converts the wav files into mp3s. I want to allow this script to use the second core on my processor without creating too many parallel operations. To do this I thought I would use a global variable to keep track of the number of running processes. However, in testing this out I've found that when a function is called in parallel (with the & character at the end of the line), it doesn't update the global variable as I had hoped. Can someone tell me what I'm doing wrong here?

    Code:
    #!/bin/bash
    processcount=5
    
    function testbranch {
    let processcount-=1
    } # end of function testbranch
    
    echo "Starting value is:  $processcount"
    
    testbranch
    echo "New value after running function in sequence is:  $processcount"
    
    testbranch& 
    # Make sure paralell branch has time to complete before writing the current value. 
    sleep 5
    echo "New value after running function in parallel is:  $processcount"
    
    exit 0
    This returns:
    mountainman@mountainman-desktop:~/bashtest$ ./vartest
    Starting value is: 5
    New value after running function in sequence is: 4
    New value after running function in parallel is: 4
    mountainman@mountainman-desktop:~/bashtest$

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    From the Bash man page:
    Code:
           If  a  command  is terminated by the control operator &, the shell exe-
           cutes the command in the background in a subshell.  The shell does  not
           wait  for  the command to finish, and the return status is 0.
    So when you background something, it is run in a subshell. A subshell cannot affect the parent, and this is why you do not see the update.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    With bash, some current shell constructs are run in subshells too, so you might find this behaviour without spawning background tasks. Try the following code snippet in bash and ksh (by swapping the first two lines) and see the difference:
    Code:
    #!/bin/bash
    #!/bin/ksh -p
    Lines=0
    echo "line one
    line two
    line three" | while read line
    do
        echo "$line"
        Lines=$(/usr/bin/expr $Lines + 1)
    done
    echo "Total lines: $Lines"

  4. #4
    Just Joined!
    Join Date
    May 2005
    Location
    Dallas, Texas
    Posts
    95
    Thanks to both of you for setting me straight on that. I ended up doing it a different way that didn't require tracking the number of open threads. Now every other file is processed in parallel, so unless there is a large difference in song lengths there should be roughly two threads running at any given time. I know it isn't perfect, but it seems close enough for the purpose at hand.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...