Hi Brent,<br><br>Thanks for the xargs command info. I did not know it before.<br><br>The other reason I want to play with my mapm version is eventually I want to make it concurrent.<br><br>Thanks again,<br><br>Hong<br><br>

<div class="gmail_quote">On Tue, Aug 28, 2012 at 10:08 AM, Brent Yorgey <span dir="ltr">&lt;<a href="mailto:byorgey@seas.upenn.edu" target="_blank">byorgey@seas.upenn.edu</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

I do not know the solution to your problem -- dealing with shells,<br>
environments, etc. can be tricky.<br>
<br>
However, do you know about the &#39;xargs&#39; command?  E.g. your example<br>
could be accomplished with<br>
<br>
  ls | xargs -L 1 -I{} cp -pr {} destination_dir/{}<br>
<br>
-Brent<br>
<div><div class="h5"><br>
On Tue, Aug 28, 2012 at 09:58:16AM -0500, Hong Yang wrote:<br>
&gt; Hi,<br>
&gt;<br>
&gt; I am trying to mimic mapM() at shell command line. I define the interface<br>
&gt; as &quot;mapm cmd2 cmd1,&quot; so cmd2 will be run for each of the cmd1 results. &quot;$_&quot;<br>
&gt; can be used inside cmd2 to represent the current cmd1 result.<br>
&gt;<br>
&gt; For example, the command<br>
&gt;         mapm    &#39;cp -pr $_ destination_dir/$_&#39;    ls<br>
&gt; copies everything under the current directory to the destination directory.<br>
&gt;<br>
&gt; The code is as follows:<br>
&gt;<br>
&gt; --<br>
&gt; module Main where<br>
&gt;<br>
&gt; import System.Environment ( getArgs )<br>
&gt; import System.Exit<br>
&gt; import System.IO<br>
&gt; import System.Process<br>
&gt; import Text.Regex<br>
&gt; import Text.Regex.Posix<br>
&gt;<br>
&gt; main = do<br>
&gt;     hs_argv &lt;- getArgs<br>
&gt;     if length hs_argv /= 2<br>
&gt;       then<br>
&gt;         putStrLn &quot;wrong arguments!&quot; &gt;&gt; exitFailure<br>
&gt;       else do<br>
&gt;         let [cmd2, cmd1] = hs_argv<br>
&gt;         (_, hOut, hErr, _) &lt;- runInteractiveCommand cmd1<br>
&gt;         err &lt;- hGetContents hErr<br>
&gt;         hClose hErr<br>
&gt;         if null err<br>
&gt;           then do<br>
&gt;             out &lt;- hGetContents hOut<br>
&gt;             mapM (f cmd2) (lines out)<br>
&gt;           else<br>
&gt;             putStr err &gt;&gt; exitFailure<br>
&gt;<br>
&gt; f :: String -&gt; String -&gt; IO ExitCode<br>
&gt; f cmd2 item = system cmd2&#39;<br>
&gt;   where cmd2&#39; = if cmd2 =~ &quot;\\$\\_&quot;::Bool<br>
&gt;                 then subRegex (mkRegex &quot;\\$\\_&quot;) cmd2 item<br>
&gt;                 else cmd2<br>
&gt; --<br>
&gt;<br>
&gt; It works, except one issue that is bothering me.<br>
&gt;<br>
&gt; If I issue<br>
&gt;         mapm    &#39;lt $_&#39;    ls,<br>
&gt; I get a bunch of<br>
&gt;         /bin/sh: lt: command not found,<br>
&gt; while I expect it act the same as<br>
&gt;         mapm    &#39;ls -Alrt --color=auto $_&#39;    ls,<br>
&gt; because &quot;lt&quot; is aliased to &quot;ls -Alrt --color=auto.&quot;<br>
&gt;<br>
&gt; Notice &quot;/bin/sh&quot; above. My shell is actually tcsh. All the aliases are in<br>
&gt; ~/.cshrc.<br>
&gt;<br>
&gt; I tried replacing &quot;system cmd2&#39;&quot; with<br>
&gt;         system (&quot;source ~/.cshrc; &quot; ++ cmd2&#39;)<br>
&gt;     and<br>
&gt;         system (&quot;tcsh -c &quot; ++ &quot;&#39;source ~/.cshrc; &quot; ++ cmd2&#39; ++ &quot;&#39;&quot;),<br>
&gt; but they did not solve the problem.<br>
&gt;<br>
&gt; Can someone please help me?<br>
&gt;<br>
&gt; Thanks,<br>
&gt;<br>
&gt; Hong<br>
<br>
</div></div>&gt; _______________________________________________<br>
&gt; Beginners mailing list<br>
&gt; <a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
&gt; <a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br>
<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</blockquote></div><br>