Register
It is currently Tue Jul 22, 2014 11:12 pm

Global variable not working due to concurrent execution


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Sat Feb 20, 2010 7:04 pm   
User avatar

Joined: Sun Nov 01, 2009 8:59 am
Posts: 23
Location: Try to guess!
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!


Top
 Profile  
 PostPosted: Sat Feb 20, 2010 9:37 pm   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
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.


Top
 Profile  
 PostPosted: Sun Feb 21, 2010 4:12 am   
User avatar

Joined: Sun Nov 01, 2009 8:59 am
Posts: 23
Location: Try to guess!
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.


Top
 Profile  
 PostPosted: Sun Feb 21, 2010 9:56 pm   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
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


Top
 Profile  
 PostPosted: Mon Feb 22, 2010 3:44 am   
User avatar

Joined: Sun Nov 01, 2009 8:59 am
Posts: 23
Location: Try to guess!
Ok then, will do! :-/


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP