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 ...
- 02-02-2008 #1Just 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?
This returns: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
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$
- 02-03-2008 #2
From the Bash man page:
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.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.DISTRO=Arch
Registered Linux User #388732
- 02-03-2008 #3Linux 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"
- 02-05-2008 #4Just 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.


Reply With Quote