Possible to use process substitution a variable number of times?

Hello Archers,
I have a script like so:
#! /bin/bash
( readlink /proc/$$/fd/0 | grep -q "^pipe:" ) || ( file $(readlink /proc/$$/fd/0) | grep -q "character special" )
cat | tee >(awk 'NR % 2 == 0') >(awk '(NR+1) % 2 == 0')
Well, it's pretty trivial at the moment, but the underlying goal is to split the input, so it can be processed in parallel on multiple cores. If I understand correctly, the above code will split the input across exactly two cores. Does there exist some way to put the process substitution (i.e. ">(awk ' ... ')")  in a loop?
Before trying to increase the number of cores, I first tried to see whether I could loop just two times with,
cat | tee $(for i in 1 2; do >(awk -vn="$i" '(NR + $n) % 2 == 0'); done)
but that didn't work.
It also occurred to me that maybe I'm presenting an X-Y problem. I did also consider using the split command, but as far as I can tell, it will only dump its output to files; it's no good for piping or process substitution. Any other ideas that would avoid needing to tee across a variable number of process substitutions?
Naturally, I tried a little Googlemancy, but only came up with answers to newbie questions, or tutorials aimed at newbies.
Thank you for your consideration.
Last edited by /dev/zero (2012-03-26 07:16:25)

Or if you want to avoid named pipes, since during development they tend to create lots of files, and the script has to clean them up afterwards, take a look at the 'coproc' feature bash has since version 4
Though, remember that 'man bash' under BUGS says, there may only be one active coproc at a time.
According to this it's not really such a big deal though.
And it won't kill the started coprocesses or anything if you start another process...
Here's a sample session: (the spaces are important btw. )
wry:~/ $ bash [19:02:43]
wry@blubmb:~$ coproc c1 ( cat )
[1] 4129
wry@blubmb:~$ coproc c2 ( cat )
bash: warning: execute_coproc: coproc [4129:c1] still exists
[2] 4130
wry@blubmb:~$ echo $c1_PID $c2_PID -- ${c1[@]} ${c2[@]}
4129 4130 -- 63 60 62 58
wry@blubmb:~$ echo aaac1 >&${c1[1]}
wry@blubmb:~$ echo bbbc2 >&${c2[1]}
wry@blubmb:~$ read -u ${c1[0]} line1
wry@blubmb:~$ read -u ${c2[0]} line2
wry@blubmb:~$ echo $line1 and $line2
aaac1 and bbbc2
wry@blubmb:~$ kill $c1_PID $c2_PID
wry@blubmb:~$
[1]- Terminated coproc c1 ( cat )
[2]+ Terminated coproc c2 ( cat )
wry@blubmb:~$ exit
exit
Just keep in mind that the filedescriptors you get for a coproc won't be available in subshells you execute with &
Eg. you cannot do
coproc X
( use X ) &
Links: http://wiki.bash-hackers.org/syntax/keywords/coproc

Similar Messages

  • Is it possible to use NUMCHARS to display the number of characters in a block of text instead of the whole document? How?

    Is it possible to use NUMCHARS to display the number of characters in a block of text instead of the whole document? How?

    Getting the character count for a paragraph is trivial:
    Sub NumChars1()
    MsgBox Selection.Paragraphs(1).Range.Characters.Count
    End Sub
    Even outputting it to the document is trivial:
    Sub NumChars2()
    With Selection.Paragraphs(1)
      .Range.InsertAfter .Range.Characters.Count
    End With
    End Sub
    The real issue comes when you want to update the document. If the destination paragraph is only ever empty or contains the character count of the previous paragraph, you could use:
    Sub NumChars3()
    With Selection.Paragraphs(1)
      .Next.Range.Text = .Range.Characters.Count & vbCr
    End With
    End Sub
    Cheers
    Paul Edstein
    [MS MVP - Word]

  • Is it possible to use SCCP and SIP at the same time?

    Our CCM is using v9.1 and all of our phones are SCCP. We are looking at a conference room phone that runs SIP. Would it be possible to use this in our current setup?

    Hi @bobdchambers,
    That's totally fine. They both co-exist in the same environment but make sure to not configure the same extension in different models because if you dial an extension that is configured in a SCCP phone and a SIP phone, just the SCCP phone rings.
    HTH.
    Rgrds,
    Martin, IT Specialist

  • Is it possible to use attachMovie with a variable ID reference?

    Hello:
    I am trying to write an attachMovie statement with a variable
    feeding the ID parameter. Is that possible?
    For Example:
    fadeInVar = '"LS_body_home_ID"';
    _level0.LS_hitArea_body.attachMovie([fadeInVar],
    "attached_LS_body", 1);
    I can't quite figure out how to write this. I tried an eval
    function as well...
    If you have any ideas, please let me know.
    Thank you!
    - Garrett

    I'm not so sure that this will even work but off the top of my head I can say remove the [] from around [fadeInVar].
    AS2... not sure if this will work. I know in AS3 it wouldn't. It would for sure work if you externalized your images/swfs from the library and used a moviecliploader to load in a swf/image from a URL.
    function loadFile(fileURL:String):Void
         var mcl:MovieClipLoader = new MovieClipLoader();
         mcl.load(fileURL, container); //Where container would be the display object (movieclip or Bitmap) that you wanted to load the item into.
         //To dynamically create something you can add the image into, do this
         var mcContainer:MovieClip = stage.createEmptyMovieClip("containerClip" + stage.getNextHighestDepth()");
         mcl.load(fileURL, mcContainer); //Where container would be the  display object (movieclip or Bitmap) that you wanted to load the item  into.
    Use of the above function would be:
    loadFile("images/image1.jpg");
    Let me know if that helps.

  • HT5463 Is it possible to use DND  on a single number and will this apply to messages as well?

    Can you use DND on a single phone number?

    What do you mean by "use DND"? Do you mean you want to block a single user? Or let only a single user through. The latter would be fairly simple. Just make that person the only "Favorite" and set DND to allow only favorites through.  Blocking only one person can be simply accomplished by assigning them customer ring and text tones of silence.

  • Possible to use rollover caption & image at the same time?

    If a user points to an area on the screen I want a caption to display (with voiceover) AND an image to display elsewhere on the screen at the same time. Is this possible? Is seems as though, because of the layering of objects on the screen, this cannot be accomplished?

    Hi Karin
    This is normally where one would consider using a Rollover Slidelet. Basically you roll over one area and a second area is revealed. The second area would then contain the image as well as the caption.
    Cheers... Rick
    Helpful and Handy Links
    Captivate Wish Form/Bug Reporting Form
    Adobe Certified Captivate Training
    SorcerStone Blog
    Captivate eBooks

  • Is it possible to use the HDMI output at the same time as the usb camera adaptor on an iPhone 4s

    I want to use the
    Apple Digital AV Adapter and
    Apple iPad Camera Connection Kit
    with my iphone 4s.
    I want to have the screen output to a larger screen (HDMI tv) and use a keyboard and mouse (usb) with the iphone apps.
    Anyone tried this or know if it will work?

    I have a raspberry pi and have bought the Atrix lapdock to use with it. I'm also hoping to use the lapdock with my iPhone 4s through the digital AV plug (HDMI and 30-pin) and the usb connector from the camera kit (into the 30-pin connector on the digital AV plug). I am making the assumption that they will work together, is that true? Anybody have both connectors?

  • Is it possible to use 2.4GB of data roaming in 1 hour on a iPhone

    Can anyone help me please. I have a iPhone 4 and turned off data roaming, emails 3G etc when I went to turkey and only used the phone to make a few texts and calls but received a £742 bill on my return to the uk for 2.4GB of data roaming in 1 hour 3 minutes. I have done a bit of research on how much data this actually is a I do not believe it is even possible to use this amount in such a short time.  Can anyone advise me as vodafone are just not listening and say I must have used it. The data roaming is claimed to be at 0400-0500 hours when o was asleep and my phone was turned off!

    Data use is not reported in real time in many cases. If the system you were on was like many, it reports data useage in batches, at a specific time each day. If the useage appears on your bill as occuring between 0400 and 0500 that is the time it was reported, not the time it was actually used. It could have actually been spread out across dozens or hundreds of individual transactions across the course of a day, or in rare cases, more.

  • Running a java program a set number of times

    This is a general question. Is it possible to make a java program run only 5 times for the sake of arguement.
    Basically I want to write a program that will give the user some flexibility when it will actually run another Java program, but I only want them to be able to say "not now' for a set number of times. When the last time comes the other program will launch. I was initially thinking of the Do Whilw loop, but this needs to work when the program is restarted.
    Program starts, it has 5 times it will run before it does something else(doesn't really matter now I think). User takes option "Not Now" and the program ends, but warns the user this will run 4 more times before you will need to do something.
    This process will repeat until the user takes the option "Ok install now" or the time limit expires and the install occurs anyway. Can someone point me in the right direction.

    ok I see so it's like one those programs that you download for free on the internet and they give you a set amount times to use it before you have to pay for it. but in this case when the number of times you use it equals 5 (or when the user clicks ok) a different java app will open automatically.
    My first thought would be to Write a Serialized object to disk using objectOutputStream that stores the number of times the application has been opened. and each time the program runs it checks for the serialized object and then you can do something like what I posted before. of course if were worried about security the user could always look for the object and erase it, if so then I guess we would have to come up with another plan of attack
    Hope this helps

  • Document Directory 'Number of Times Accessed' count

    Not really an api question, but i couldnt find anything else that matched better...
    Is the process that updates the Number of Times Accessed count a batch process and if so, how often does it run?
    The reason i ask is that i have some docs in my directory whose count is remaining zero, even after I have accessed them, whilst others whose count is increasing over time quite happily.
    Does anyone have any ideas why this might be the case?
    thans

    No, I think what's going on is that rather than update the counter every time a user clicks through, we use a randomized algorithm where there's a 1 in N chance that the portal will add N to the count. For example if N is 5 and you open a document 5 times you would expect to see the counter behave something like: 0 0 0 5 5
    This means there's a margin of error (think opinion polls) but the margin decreases as the count increases, so you might know a given document has been accessed 12,450 times with a 5% margin of error. The benefit is that we reduce the load on the database for this particular SQL update by a factor of N. I believe there's a setting somewhere to modify N if you need more accuracte counts, in fact you can set it to 1 if you need a precise count.

  • IPod stolen can I get it to be flagged with its serial number next time it's synced?, iPod stolen can I get it to be flagged with its serial number next time it's synced?

    iPod was stolen, is it possible to get it flagged via Serial number next time it's synced to load songs by the thief?
    Many thanks, it's my son's, was my son's, and I dunno what to do

    - If you previously turned on FIndMyiPod on the iPod in Settings>iCloud and wifi is on and connected go to iCloud: Find My iPhone, sign in and go to FIndMyiPhone. If the iPod has been restored it will never show up.
    - You can also wipe/erase the iPod and have to iPod play a sound via iCloud.
    - Change the passwords for all accounts used on the iPod and report to police
    - There is no way to prevent someone from restoring the iPod (it erases it) using it.
    - Apple will do nothing
    Reporting a lost or stolen Apple product
    - iOS: How to find the serial number, IMEI, MEID, CDN, and ICCID number
    No you need to have turned on FIndMyiP{od
    The SN will only help identify the iPod as your if it is recovered.

  • Playing a song through Music Match from iCloud on my iPhone does not update number of times played

    I noticed that when I play a song on my iPhone using Music Match / iCloud, the number of times played counter on in iTunes my Mac is not increased, nor is date last played etc. As this is still one user, using one music collection I had expected this to happen.
    Now my "good but forgotten" clever playlist (>=4 stars, date last played over a year ago) is not kept current anymore.
    Is this a bug or a "feature". Like most things iCloud it seems to work "sort of, but not quite there yet..."

    I am experiencing the same problem. I have uninstalled and re-installed ALL Apple software. Restored phone. Didn't work. Got a new phone and set up as a new phone. Same problem. Cannot play music. I select the song and the screen switches back to the previous screen and never plays. Artwork is all jacked up. Please help.

  • Listing number of times an archivelog is backed up.

    Is there anyway to use RMAN to determine the number of times an archive log has been backed up?

    Thank you. Also know that there is a column in v$archived_logs that gives me this information.

  • Possible to do variable number of REPLACE in SQL?

    Hello. Using Oracle 10G, R2
    Wondering if it is possible to do a variable number of REPLACE() entirely in SQL in a view.
    input_table.text_column = This (b)is(/b) some (i)text(/i) with (u)formatting(/u) codes
    Note: Using ( and ) to represent < and >
    rows in format_codes_table:
    (b)
    (/b)
    (i)
    (/i)
    (u)
    (/u)
    (p)
    (/p)
    etc. (The number of format_codes is not fixed)
    Desired output: This is some text with formatting codes
    This could be done with a user-defined function and then use that function in a SQL:
    create or replace function remove_format_codes(input_p IN varchar2)
    return varchar2
       v_output   varchar2(2000);
    is
    begin
       v_output := input_p;
       for r1 in (select format_code from format_codes_table)
       loop
          v_output := replace(v_output, r1.format_code);
       end loop;
       return v_output;
    end;
    create or replace view unformatted_output_vw
    as
    select remove_format_codes(input_table.text_column) as unformatted_output
    from input_table
    /I tried this SQL:
    select replace(input_table.text_column, format_codes_table.format_code) as unformatted_output
    from input_table
        ,format_codes_table
    /But it only replaces one format code at a time, and it is not recursive so the output is like this:
    This is(/b) some (i)text(/i) with (u)formatting(/u) codes
    This (b)is some (i)text(/i) with (u)formatting(/u) codes
    This (b)is(/b) some text(/i) with (u)formatting(/u) codes
    This (b)is(/b) some (i)text with (u)formatting(/u) codes
    etc.
    I've google'd Oracle recursive sql, looked at CONNECT BY, LEAD, LAG, MODEL, and I've also looked at a
    Tom Kyte example for varying in lists (http://tkyte.blogspot.com/2006/06/varying-in-lists.html),
    but I can't seem to find a way to replicate the loop in my user-defined function in SQL.
    Anyone think this is possible in SQL? If yes, any hints?
    Thanks

    Hi,
    Regular expressions (introduced in Oracle 10) are great for this:
    SELECT     REGEXP_REPLACE ( text_column
                     , '&lt;'          || -- left angle-bracket
                       '/?'          || -- optional slash
                    '[bipu]'     || -- any one of these characters
                    '>'             -- right angle-bracket
                     )      AS no_tag_text
    FROM    input_table
    ;You had some good ideas: recursive subqueries (new in Oracle 11.2), CONNECT BY and MODEL could also do this job, but not nearly as easily.
    In case you're interested, the following thread uses MODEL to do nested REPLACEs:
    SQL Query
    Edited by: Frank Kulash on May 13, 2010 4:08 PM
    Edited by: Frank Kulash on May 17, 2010 1:02 PM
    Fixed link

  • How to use a substitution variable in a load rule?

    I need to use a substitution variable in a load rule in a column, as I will receive a parameter to fix the Month and Year values within the data loading, could somebody tell me if this is possible. I put an expresion "&Yearproc" in the column value but it is not working.

    If you're a member of ODTUG (or even if not, you can sign up for an associate membership for free) you can download Glenn's presentation from 2009 Kaliedoscope "Little Used Features of Essbase (Like Data Mining and Triggers)" -- there is a section in that presentation on substitution variables -- he does a really good job in showing how this works.
    Go to: www.odtug.com, then Tech Resources, then Essbase/Hyperion, and search for Schwartzberg. Currently it's the ninth presentation on the list -- I think this changes based on popularity of downloads.
    Regards,
    Cameron Lackpour

Maybe you are looking for

  • Help with CS 6 on a second computer

    My daughter has Photoshop CS 6 installed on her 64 bit Windows 7 Pro computer.  She now has a new computer (also 64 bit Windows 7 Pro)  and would like to get CS 6 on it too.  I have been led to believe that she can have the program on two computers a

  • Hierarchial data through content conversion

    All, we have an input file in the following format. (This is a fixed length file structure, where the first character is the type of the record and the rest is the data) 0XXXX 1YYYY 2MasterRow-1 3Childrow 1 3Childrow 2 4Childrow 1 4Childrow 2 2Master

  • Creating a Client Bean for an SAP-Webservice

    Hi, I have created a web service for an SAP-Bapi with the web service creation wizard of the SAP-System (EC5). Now I would like to implement a client bean in the NetWeaver Developement Studio for it. How can I give input parameters for a testmethod?

  • Can I adjust pixel width and height in iPhoto?

    I'm trying to find out if it's possible to adjust pixel width and height of a photo without cropping it. iPhoto Help was no help at all. Thanks. iBook   Mac OS X (10.4.4)  

  • Partially complete PC to MAC migration using Migration Assistant

    I recently attempted to migrate data from my windows laptop to a new MacBook using Migration Assistant.  When the process started, Migration Assistant estimated approx 30GB for the new data.  I aborted the transfer after 24+ hours, but the hard drive