LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-05-2010, 10:17 AM   #1
rylphs
LQ Newbie
 
Registered: Aug 2010
Posts: 17

Rep: Reputation: 0
Question Problem with a function having a background process inside a command substitution


What is the problem with the following code:
Code:
somefunction(){
sleep 30&
echo endfunction
}

somefunction;
echo $(somefunction);
The first call to "somefunction" works as expected. The function prints "endfunction" and a process in background sleeps 30 seconds. In the second call I thought it should work in the same way, but the script sleeps 30 seconds before it prints "endfunction".

Does someone know the reason of this behavior? Is there another way to do a command substitution of a function that has a background process without have to waiting for that process?


I hope you have understood my problem. My english is not very good.
Thank you.
 
Old 10-05-2010, 11:23 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Echo won't run until the substitution finishes what it's doing.

Bash has to build a command before executing it, so it first runs any embedded commands and replaces them with their output, does the same with variable substitutions, globbing, etc., then finally executes the main command.

So bash has to wait the full 30 seconds for the function to exit before it can execute the echo command that prints the output of the function to stdout.
 
Old 10-05-2010, 11:36 AM   #3
rylphs
LQ Newbie
 
Registered: Aug 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by David the H. View Post
Echo won't run until the substitution finishes what it's doing.

Bash has to build a command before executing it, so it first runs any embedded commands and replaces them with their output, does the same with variable substitutions, globbing, etc., then finally executes the main command.

So bash has to wait the full 30 seconds for the function to exit before it can execute the echo command that prints the output of the function to stdout.
I undertand. Thank you for helping me.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
can't find my jboss process with ps aux, what is the command to list background proce Niceman2005 Linux - General 3 08-18-2012 06:11 AM
sending remote command to background process + out log c0m4r Linux - Newbie 6 02-02-2010 05:34 AM
Process Substitution not working with echo command muazfarooqaslam Linux - Newbie 1 02-04-2008 10:10 AM
poepn or system function call executed by a background process kshkid Programming 1 08-24-2006 04:04 AM
alias or function in .bashrc, which sends command to background ngomong Linux - General 1 04-23-2002 10:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration