BashScripts.org
http://bashscripts.org/forum/

Global variable not working due to concurrent execution
http://bashscripts.org/forum/viewtopic.php?f=7&t=1001
Page 1 of 1

Author:  e633 [ Sat Feb 20, 2010 7:04 pm ]
Post subject:  Global variable not working due to concurrent execution

Title is BS i know, i tried my best to sum it up but, anyway, the problem is this:

Code:
someBigFile= ************* <<<-----<<<-<<<-<<<-<-<-<--<-<-<-<-<-<--<-<-<-<--- INIT THIS!
summ="Global"

function doMD5
{
md5=`md5sum $someBigFile`
echo ${md5:0:32}    #kind of returning
}

(
echo $'\n'
summ="$(doMD5)" &
)|
zenity --progress --pulsate --title="MD5" --text="Saving MD5 checksum..." --auto-close
if [ "$?" != 0 ]
   then   
      zenity --warning --title "CANCELED" --text "Aborted by user, FILES NOT DELETED."
      kill `pidof -s md5sum`
      kill $$                         # No time to tell if i need this too, i think i'll leave it where it is for now
fi

echo $summ

This script opens a zenity pulsating bar during the checksum and kills it should the user click "Cancel". Problem is $summ echoes his initialization value and not the output of that function. I'm definitely wasting too much time on this single issue!

Author:  thobbs [ Sat Feb 20, 2010 9:37 pm ]
Post subject:  Re: Global variable not working due to concurrent execution

Sister thread: http://bashscripts.org/forum/viewtopic.php?f=16&t=989&sid=679115af6e8ed55692bb2c7de7a2bb13

The parentheses create a subshell. There is a different scoping for the variables in the subshell than in the parent shell. Thus, the $summ in the subshell is not the same as the one from the main shell! It would be nice if bash warned you about this, but it doesn't do that kind of analysis.

This is why, in the other thread, I recommended doing things in a slightly awkward way. Combining pipes, subshells, and return values is tricky. It's not going to be easy to communicate between the subshell and the main shell, especially if you are piping to zenity -- that was the reason for touching the file.

Author:  e633 [ Sun Feb 21, 2010 4:12 am ]
Post subject:  Re: Global variable not working due to concurrent execution

So... what's the tricky way? :B
I don't really like the idea of writing to file just for a short string, i think i'll keep that as the last resort.

Author:  thobbs [ Sun Feb 21, 2010 9:56 pm ]
Post subject:  Re: Global variable not working due to concurrent execution

Keep it simple, my friend. Touching a file costs nothing compared to md5summing a large file or even the GUI stuff.

Your other options include, as far as I can tell:
1. An even more awkward chain of calls
2. Modifying the Zenity source (it's in C)
3. Some other magic

Author:  e633 [ Mon Feb 22, 2010 3:44 am ]
Post subject:  Re: Global variable not working due to concurrent execution

Ok then, will do! :-/

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com