Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 06-27-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 27 May 2015, 11:14 PM EDT
Location: Amsterdam
Posts: 10,216
Thanks: 334
Thanked 2,696 Times in 2,412 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 Unix and Linux 06-27-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 27 May 2015, 6:54 PM EDT
Location: Saskatchewan
Posts: 20,672
Thanks: 896
Thanked 3,660 Times in 3,423 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 Unix and Linux 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 Linux 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 Unix and Linux 06-28-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 27 May 2015, 6:54 PM EDT
Location: Saskatchewan
Posts: 20,672
Thanks: 896
Thanked 3,660 Times in 3,423 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 Unix and Linux 06-28-2013
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 17 February 2015, 1:56 PM EST
Location: Southern NJ, USA (Nord)
Posts: 4,671
Thanks: 8
Thanked 586 Times in 559 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 09:28 AM
signal handling while in a function other than main Theju Programming 1 09-18-2008 04:06 PM
main function arun.viswanath Programming 6 03-07-2008 11:43 AM
Strange array handling in ksh93 bakunin Shell Programming and Scripting 3 10-24-2005 12:49 PM
c++ calling main() function norsk hedensk Programming 3 01-22-2003 07:28 PM



All times are GMT -4. The time now is 11:54 PM.