[ksh93+] Array fed by function is empty when used in main. | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

[ksh93+] Array fed by function is empty when used in main.

UNIX for Dummies Questions & Answers


Tags
array, ksh, ksh93

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-27-2013
bibou25 bibou25 is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 6 August 2013, 12:39 PM EDT
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
[ksh93+] Array fed by function is empty when used in main.

I feel that i am missing something obvious but i can't find what is wrong.

I have a script that is launching some functions with "&" and each call is feeding the array with a value. When all calls are finished I just want to retrieve the values of that array.

It is looking like that :


Code:
#!/user/bin/ksh

#Function called with & 
fct()
{
 sleep $1
 tab_track[$1]="$(date)"
}

#Main
for OneIteration in 1 2 3 4 5
do
fct $OneIteration &
done

wait

echo "Display time"

for OneLine in "${tab_track[@]}"
do
   echo ${OneLine}
done

Any idea ?
Sponsored Links
    #2  
Old 06-27-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 December 2014, 6:51 AM EST
Location: Amsterdam
Posts: 9,697
Thanks: 301
Thanked 2,492 Times in 2,231 Posts
Hi,

Code:
fct $OneIteration &

will get executed in a subshell in the background, so any assigned values will be lost, once it finishes..
Sponsored Links
    #3  
Old 06-27-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 22 December 2014, 2:25 AM EST
Location: Saskatchewan
Posts: 20,043
Thanks: 856
Thanked 3,460 Times in 3,231 Posts
Quote:
Originally Posted by bibou25 View Post
I feel that i am missing something obvious but i can't find what is wrong.

I have a script that is launching some functions with "&" and each call is feeding the array with a value. When all calls are finished I just want to retrieve the values of that array.

It is looking like that :


Code:
#!/user/bin/ksh

#Function called with & 
fct()
{
 sleep $1
 tab_track[$1]="$(date)"
}

#Main
for OneIteration in 1 2 3 4 5
do
fct $OneIteration &
done

wait

echo "Display time"

for OneLine in "${tab_track[@]}"
do
   echo ${OneLine}
done

Any idea ?
If you wanted to do multiple commands at once and save their results in order, you must first save them separately, because once they're in the background they'll run in no particular order at all.


Code:
#Function called with & 
fct()
{
 sleep $1
 date
}

for X in 1 2 3 4 5
do
        fct > /tmp/$$-$X &
done

wait # Let them all finish

# Set the array
set -a ARR `cat /tmp/$$-*`

# Remove temp files
rm -f /tmp/$$-*

    #4  
Old 06-28-2013
bibou25 bibou25 is offline
Registered User
 
Join Date: Jun 2013
Last Activity: 6 August 2013, 12:39 PM EDT
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
I am not trying to get the results in particular order but I need to retrieve all of them I was not considering the use temporary file but it seems the way to go. Thanks for pointing me to the right directions.
Sponsored Links
    #5  
Old 06-28-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 22 December 2014, 2:25 AM EST
Location: Saskatchewan
Posts: 20,043
Thanks: 856
Thanked 3,460 Times in 3,231 Posts
Considering that they might return results inside each others with no respect for things like linebreaks, one big pile is not the way to go.
Sponsored Links
    #6  
Old 06-28-2013
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 18 December 2014, 4:59 PM EST
Location: Southern NJ, USA (Nord)
Posts: 4,560
Thanks: 8
Thanked 562 Times in 537 Posts
Yes, each bg proc needs its own output file or pipe, unless you can ensure they write() blocks of whole lines. I had to clean up some shared logs in C/C++, using setvbuf() to make the log's FILE* buffer big, and fflush() to ship each buffered message in a single write(). In shell, you might have problems ensuring atomic line write(). If you want sorted outputsort can be a buffer:
Code:
sort -m <( script_1 | sort ) <( script_2 | sort ) <( script_3 | sort ) <( script_4 | sort ) <( script_5 | sort ) >output_file

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to access argv[x] from another function other than main??? gabam Programming 2 12-19-2011 10:28 AM
signal handling while in a function other than main Theju Programming 1 09-18-2008 05:06 PM
main function arun.viswanath Programming 6 03-07-2008 12:43 PM
Strange array handling in ksh93 bakunin Shell Programming and Scripting 3 10-24-2005 01:49 PM
c++ calling main() function norsk hedensk Programming 3 01-22-2003 08:28 PM



All times are GMT -4. The time now is 08:41 AM.