Bash: Can I run in background with pipes | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Bash: Can I run in background with pipes

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-03-2008
jjinno jjinno is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 15 January 2014, 2:49 PM EST
Location: Cloud 9
Posts: 92
Thanks: 2
Thanked 2 Times in 2 Posts
Bash: Can I run in background with pipes

If I have this (assume passwordless):


Code:
COMMAND="/sbin/ifconfig eth0"
CMD=$(ssh $IP "${COMMAND}" | grep "inet addr")

The problem is that if I run 100 of these in a loop, they all execute serially... which is really slow... BUT if I want to put the '&' in (before the first pipe), I have to get rid of my 'grep' statement for the thing to work... which makes sense, but doesnt change the fact that it would be WAY cooler & faster to do all these IP checks in parallel (like 5 seconds versus 5 minutes)

Any ideas?
Sponsored Links
    #2  
Old 01-03-2008
porter porter is offline Forum Advisor  
Registered User
 
Join Date: Jan 2007
Last Activity: 8 January 2008, 6:50 PM EST
Posts: 2,965
Thanks: 0
Thanked 5 Times in 5 Posts
I find it simple to put things in a function then execute those in parallel..


Code:
myfunc()
{
do-lots $1 | of-things
}

for arg in a b c d
do
    myfunc $arg &
done

wait

Sponsored Links
    #3  
Old 01-03-2008
stateful stateful is offline
Registered User
 
Join Date: Jan 2008
Last Activity: 17 December 2008, 10:59 AM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
BASH command grouping

You can group piped commands together with BASH grouping.

from Bash Reference Manual

Code:
Bash provides two ways to group a list of commands to be executed as a unit.
When commands are grouped, redirections may be applied to the entire command list. 
For example, the output of all the commands in the list may be redirected to a single stream.

()

              ( list )

    Placing a list of commands between parentheses causes a subshell environment to be created 
(see Command Execution Environment), and each of the commands in list to be executed in that subshell. 
Since the list is executed in a subshell, variable assignments do not remain in effect after the subshell completes.

{}

              { list; }

    Placing a list of commands between curly braces causes the list to be executed in the current shell context. 
No subshell is created. The semicolon (or newline) following list is required.

For example:
(cat massivefile | sed 's/10/20/g' >> output) &

You can even logout and it will continue to run.
    #4  
Old 01-03-2008
jjinno jjinno is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 15 January 2014, 2:49 PM EST
Location: Cloud 9
Posts: 92
Thanks: 2
Thanked 2 Times in 2 Posts
Wow, cool... never thought of doing it like that.

And man oh man is that "wait" important... the first time I tried your suggestion I dismissed it, and got the 100 responses back in the middle of my next 'vi' session... HAHAHA

Thanks
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
bash tee and background threads heatlamp Shell Programming and Scripting 1 08-25-2010 12:12 PM
background processing in BASH jville Shell Programming and Scripting 12 05-04-2009 01:52 PM
background processing in BASH jville Linux 5 04-29-2009 10:03 AM
How to get BASH to interpret pipes in a string correctly? neked Shell Programming and Scripting 2 04-02-2008 03:12 PM
PIPEs and Named PIPEs (FIFO) Buffer size Jus Filesystems, Disks and Memory 1 08-20-2004 10:14 AM



All times are GMT -4. The time now is 01:15 AM.