tutorial on calling cygwin bash scripts from powershell?

i've spent some time searching but haven't found much about this.
quite a few comparisons of pros and cons of, say, bash versus
powershell, but not much on calling back and forth between them.

I have used cygwin for years and have begung using powershell in the
past year or two; I find both extremely useful, but tend to use them
separately: each tends to be good for certain kinds of problems and
less good for others.

now, however, i want to tackle something I haven't managed yet:
calling a cygwin/bash script from inside a powershell script and using
the result to do something useful in powershell.

this appears on second glance to be much harder than I expected, so
I'd like to find (or create) a series of increasingly sophisticated
examples to help me get where I want to go.

I'll start with some very basic examples, based on a hint or two that
I did manage to find.

first of all it's quite easy to call very simple commands with hard-
wired syntax, e.g.:

  c:\cygwin\bin\bash.exe --login -c 'ls'
  c:\cygwin\bin\bash.exe --login -c 'df'
  c:\cygwin\bin\bash.exe --login -c 'pwd'

however, it gets non-trivial (for the likes of me) starting at the
next step. (i want to be able to build these things up from pieces in
a modular way, since the bash commands that interest me will be more
complex; i anticipate problems with quotes, dollar signs and other fun
characters.)

after defining:
 $cmdWithinCygwin =
"df"
 $cygBashPath = "c:\cygwin\bin
\bash.exe"
 $cygBashSyntax =  "--login -c '$
{cmdWithinCygwin}'"

powershell does not let me run this:
 $cygBashPath $cygBashSyntax

(it yields "Unexpected token '${cygBashSyntax}' in expression or
statement...."; i must be breaking a powershell rule here.)

however, after fiddling a bit (and using a hint from about a year ago
i found in this group) i found this works:

 cmd /c "${cygBashPath} ${cygBashSyntax}"

moreover, this gets me to the next step i need (capturing the output):

 $cygCmdResult = cmd /c "${cygBashPath} ${cygBashSyntax}"
 echo "cmd yielded: (${cygCmdResult})"

now i have a carriage return linefeed problem (which surely depends on
cygwin settings and other stuff), but should be able to deal with
that).

probably the best way to practice building up bash "pipes" is one step
at a time at the command line, e.g.:

 PS> cmd /c c:\cygwin\bin\bash.exe --login -c 'dateArith.pl | tr -d
"."'
 20100622
 PS> cmd /c c:\cygwin\bin\bash.exe --login -c 'ddate=$(dateArith.pl |
tr -d "."); echo "${ddate}"'
 20100622

i need to be able to call awk, so it's good to know we can pass in
single quotes by doubling them.

i haven't figured out the next puzzle, so i'll post this as a
question: anyone know why the second command here returns only the two
dashes, not the date?

 PS> cmd /c c:\cygwin\bin\bash.exe --login -c 'ddate=$(dateArith.pl |
tr -d "."); echo "${ddate}"'
 20100622
 PS> cmd /c c:\cygwin\bin\bash.exe --login -c 'ddate=$(dateArith.pl 0
| tr -d ''.''); echo "-- ${ddate}"'
 --

the corresponding command is no problem in bash:
 $ ddate=$(dateArith.pl 0 | tr -d '.'); echo "-- ${ddate}"
 -- 20100622

cheers,

tbh

0
Tim
6/22/2010 3:57:16 PM
windows.powershell 690 articles. 0 followers. Follow

3 Replies
2207 Views

Similar Articles

[PageSpeed] 16


"Tim" <t.hanson@faz.de> wrote in message 
news:a9796a55-b231-4b6e-96a6-760e07a37441@r27g2000yqb.googlegroups.com...
> i've spent some time searching but haven't found much about this.
> quite a few comparisons of pros and cons of, say, bash versus
> powershell, but not much on calling back and forth between them.

If you have been following this group for a few weeks, you may have noticed 
that this group will be shutting down soon.

So, if you don't have a blog, you should consider setting one up on one of 
the free services out there.  That's where you should be publishing this 
potential series on Bash and PowerShell.

As for your final question, I may give it a try.  Not because I share your 
enthusiasm in calling Bash from PowerShell, but just because I'd like to 
help...

Marco 

0
Marco
6/24/2010 11:04:59 AM
> > i've spent some time searching but haven't found much about this.
> > quite a few comparisons of pros and cons of, say, bash versus
> > powershell, but not much on calling back and forth between them.
>
> If you have been following this group for a few weeks, you may have notic=
ed
> that this group will be shutting down soon.

thanks, Marco. i saw that and commented: i find it a shame: blogs are
useful, but newsgroups are pretty useful too: potentially more focused
and more uniformly searchable. but i guess this is progress. :)

> So, if you don't have a blog, you should consider setting one up on one o=
f
> the free services out there. =A0That's where you should be publishing thi=
s
> potential series on Bash and PowerShell.

good thoughts. true, it would be great if someone had a blog that
covered this. but i didn't find one and, while starting a blog might
be a good idea for other reasons, it seems like overkill for what
could end up being more or less a single tutorial or FAQ.

> As for your final question, I may give it a try. =A0Not because I share y=
our
> enthusiasm in calling Bash from PowerShell, but just because I'd like to
> help...

that'd be great, whatever your motivation!

i guess i have mixed feelings too: mixing bash and powershell, from
one angle, sounds pretty ugly. on the other hand, bash and friends
still run circles around powershell for basic text processing tasks.
powershell runs circles around bash and friends for windowy,
especially object-oriented tasks. so, while "kind of
objectionable" (random phrase I love from a Leo Kottke song), this
seems like one of those classic areas where homely compromises are
sometimes appropriate.

Jeffrey Snover seems to agree (e.g. here:
http://stackoverflow.com/questions/573623/powershell-vs-unix-shells -
or perhaps it would be fairer to say *I* agree with *him* :)

thanks again!

cheers,

Tim

p.s. ah, you replied here as well:
  http://social.technet.microsoft.com/Forums/en/winserverpowershell/thread/=
7227168d-ee39-44fa-9d9c-0378b192905e

thanks! if this place is going away, i'll move over to the other.
0
Tim
6/28/2010 8:14:46 AM
On 6/28/2010 3:14 AM, Tim wrote:
> i guess i have mixed feelings too: mixing bash and powershell, from
> one angle, sounds pretty ugly. on the other hand, bash and friends
> still run circles around powershell for basic text processing tasks...
 >

I often overlook solutions that involve the combination of Regular 
Expressions and PowerShell.  Perhaps for those basic text processing 
tasks using RE's to advantage would be the key.

  - Larry
0
Larry__Weiss
6/28/2010 12:24:53 PM
Reply:

Similar Artilces:

Tutorial
Hi, I am looking for a good and working tutorial which allows to actually do something with MFC. Most tutorials I found on the net are either broken or not self-contained. Any hints? Cheers Siap wrote: > Hi, > > I am looking for a good and working tutorial which allows > to actually do something with MFC. > > Most tutorials I found on the net are either broken or > not self-contained. > > Any hints? > > Cheers How about the SCRIBBLE tutorial, which comes with VC? Use the help system to find it. An even better way to learn would be to work thro...

Covered calls etc.
Have they taught Money to handle downloads from your broker of items like covered calls and bull-put spreads etc. etc.? In microsoft.public.money, az-willie wrote: >Have they taught Money to handle downloads from your broker of items >like covered calls and bull-put spreads etc. etc.? No. Cal Learner-- MVP wrote: >In microsoft.public.money, az-willie wrote: > > > >>Have they taught Money to handle downloads from your broker of items >>like covered calls and bull-put spreads etc. etc.? >> >> > >No. > > ======= Quicken 2004 d...

adding new contact in phone call
how do you add a contact when entering a phone call directly related to the client the phone call is coming from? new to CRM. Hi, I am not able to understand your question clearly. Can you pleae re phrase it. If you want to create new contact. Then from menu on top New -> contact There are multiple ways to create new contacts. If you want to create a contact from phone call window than in phone call window, Sender lookup window, there is a button "New" that can also be used to create contact quickly. You may contact me directly at my email address if you want help on this ...

CALL TO ACTION: Photoshop GURU's help solve "BULGEGATE"... please read
CALL TO ACTION: Photoshop GURU's help solve "BULGEGATE"... please read Here is the story, there are 12 pictures of a wirey shape up Bush's back in the debates. We need help proving they are real. Please email me if you try this, please. We are short of time and need credibility and help. We think its a wire, what do you think? http://bulgegate.blogspot.com/ Any Photoshop guru's wanna take a shot? The originals are linked in that page. You can download them in 1 min and they are reasonably good. Can anyone get a better view of that wire than me? Email me back,...

Remote script with alternate credentials?
Hi, Is there a way to run VBScript remotely with alternate credentials? Thank's for your help. "Jo" <j.tribaudini@gmail.com> wrote in message news:388913a5-40cd-4761-b722-0f4dfebf57da@21g2000yqj.googlegroups.com... > Hi, > > Is there a way to run VBScript remotely with alternate credentials? > > Thank's for your help. You can use the Win32_Process class of WMI to run an executable, including a script, remotely, as long as it runs silently with no user interaction. An example VBScript program is linked here: http://www.rlmueller....

call center
What call center's manufactures have integration with Microsft CRM 3? Name them please Vlad, I would recomend Vonexus from sight only. They have some very cool integration peices and as a software based VOIP in your application I think this would be very powerful. Love to talk to you more about this. Jeff Loucks 888-474-2237 MVP "vlad" <vlad@discussions.microsoft.com> wrote in message news:F3420679-B125-47A2-8124-A33E96B3DE71@microsoft.com... > What call center's manufactures have integration with Microsft CRM 3? Name > them please A lot depends on th...

Free Tutorial website And Ebooks programing tutorials
Free Tutorial website And Ebooks programing tutorials http://www.365x24live.com Java Script Text Book http://www.365x24live.com/JavaScript/INDEX.HTM Applied Cryptography http://www.365x24live.com/Applied%20Cryptography/ewtoc.html Vb Script Text Book http://www.365x24live.com/vbscript/index.html CGI Programming text book http://www.365x24live.com/CGI%20Programming%20Unleashed/index.htm Java Text Book http://www.365x24live.com/java/index.html Programmer's Guide Nokia WAP Server API 1 http://www.365x24live.com/nokia/1.html Aptitude Questions http://www.365x24live.com/aptitude/ind...

Where are tutorials for 2007 Publisher?
Where can I find tutorials on 2007 Publisher, more specifically on the creating a calendar? Publisher 2007 Help and How-to http://officebeta.iponet.net/en-us/publisher/FX100649111033.aspx -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com "juddr" <juddr@discussions.microsoft.com> wrote in message news:D80D5469-F2B9-43D8-AD67-CF9BE5CF6D6E@microsoft.com... > Where can I find tutorials on 2007 Publisher, more specifically on the > creating a calendar? ...

Glyn Simpson-add to b/u script
From: http://money.mvps.org/backup.aspx This is AWESOME! It's great to schedule backups temporally that copies the file with the date/time stamp within the filename. An enhancement that might help with unbounded "copies" of the backups files would be to check the directory for the number of files... if > x, then delete "oldest" file. It makes the script a little more complex, but this should be very doable for programmers. Unfortunately, I'm a network engineer--no programming abilities here! Thanks for the suggestion - I'll have to take a look at i...

Anybody familiar with an Add in call 411
Hello, I had on our system a Very nice tool call 411 Customer Search. But for some reason one day it just stop working. I check with our second store and it was also down. they whent down at the same time. it was not a tryout software. So I don't understand what could have happen to a very useful tool. If any body knows what happen please let me know. Thanks -- Reynold Cycle www.reynoldcycle.com ...

Smart Tag Tutorial?
I'm looking for a piecewise Smart Tag tutorial. I.e., one that shows how the Shape Sheet sections are built up. Any suggestions? Thanks, SteveM ...

tutorials
were the best place for tutorials. Assuming you mean "Where are ...", I would suggest visiting your favorite bookstore and looking in their computer software section. If you prefer something online, try Google. -- JoAnn Paules MVP Microsoft [Publisher] ~~~~~ How to ask a question http://support.microsoft.com/KB/555375 "lightfoot@gmail.com" <jr.lightfoot@gmail.com> wrote in message news:1167578695.263827.145110@v33g2000cwv.googlegroups.com... > were the best place for tutorials. > Publisher 2003 Help and How-to http://office.microsoft.com/en-us/...

Searching good XSL-FO (formatting objects) tutorial for converting XML->PDF
I would like to learn some XSLT-FO handling procedures and need a good tutorial. Unfortunately most of the links in Google refer to books, lessons, XML syntax What I need is an easy step-by-step guide for installing and applying all necessary tools for converting a simple lets say "Hello world" XML doc into pdf with XSL-FO Does someone know such a quick start guide ? Harry You can also check iTextSharp - a C#.net library to output pdf. I took the time to learn XSL-FO, but to me iTextSharp is much easier to learn since you do everything in a .NET language. With XSL-FO I was al...

tutorials don't run
Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel the tutorials located in the online help menu within word do not run. i'm trying to find out how to link footer to previous without doing the same to header. I don't know anything about the tutorial. Do do what you want. Menu: View/Formatting Pallette Menu: View/Header and Footer Put cursor into the Header. Uncheck "Link to Previous" Put cursor into the Footer. Check "Link to Previous". --rms www.rmschneider.com On 11/02/10 22:11, alhill@officeformac.com...

XPath Call Quit Working
Below is some code that use to work. Now it is not and I have yet to figure out why. I want to select the nodes to a nodelist and remove them from the dom. The only thing I can figure is it broke when I added my schema. XML <?xml version="1.0" encoding="utf-8"?><IADSNOTES xmlns="notes.xsd" count="4"><Note href="D:\V4.0\DEMO\IADS_4_0.sgm#IADS V4.0 TOC@0" csm="0"><User>Ralph</User><Title>IADS 4.0 Demo - Table of Contents</Title><Text>Ralph's good note</Text></Note><N...

Blue Screen: A thread tried to release a resource it Did Not Own in function CreateFileMapping called
Hi I am my driver virtual filesystem. When I open a file from the virtual drive filesystem is ok for reading file. By cons when I have a blue screen when i call the CreateFileMapping on my handle file opened on my virtual drive filesystem. NTSTATUS DrvDispatch ( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp ) { PIO_STACK_LOCATION irps; NTSTATUS status=STATUS_NOT_IMPLEMENTED; KdPrintf(("[VFUM] control\n")); irps = IoGetCurrentIrpStackLocation(Irp); if (irps!=NULL) { KdPrintf(("[VFUM] vfums_control : Device:%x Majorfunction %d ir...

ESP not properly saved (DLL calling an exe's object function).
Hi everyone, I'm stress-testing an application of mine. This application is a service, so it has to be able to support quite a heavy loan of work. It is installed and working fine at several customers. But one of them has very heavy traffic, so there it crashes once a day. The service automatically restarts but its clients have to reconnect and stuff, so the customer is not very happy... So that's why I'm stress-testing it, to see if I catch the bug. The service is an exe file using a DLL, both are code of mine. After half an hour of stress-testing I get this debug error: ...

DoDataExchange never called?
Hello, I am creating a Dialog based application using MFC and I am having a difficult problem. For some reason, the DoDataExchange function that calls my DDX_Control functions is never being called and I am not sure why. I thought that it is always called by default? Thanks I apologize... I already found my solution.. Thanks Jon wrote: > Hello, I am creating a Dialog based application using MFC and I am > having a difficult problem. > > For some reason, the DoDataExchange function that calls my DDX_Control > functions is never being called and I am not sure why. I thou...

Free Tutorial website And Ebooks programing tutorials
Free Tutorial website And Ebooks programing tutorials http://www.365x24live.com Java Script Text Book http://www.365x24live.com/JavaScript/INDEX.HTM Applied Cryptography http://www.365x24live.com/Applied%20Cryptography/ewtoc.html Vb Script Text Book http://www.365x24live.com/vbscript/index.html CGI Programming text book http://www.365x24live.com/CGI%20Programming%20Unleashed/index.htm Java Text Book http://www.365x24live.com/java/index.html Programmer's Guide Nokia WAP Server API 1 http://www.365x24live.com/nokia/1.html Aptitude Questions http://www.365x24live.com/aptitude/index.htm...

Clustering question
Hi, I have a four node cluster built on windows server 2008 R2. The cluster itself is fine and I can manage it through the GUI no problem. Hi, However I recently discovered that I can manage it from powershell on he R2 box itself. all I have to do is import the library "FAILOVERCLUSTERS " first. so he first statement in my powershell scrip is always import-module powershell FAILOVERCLUSTERS however, it would be really cool if I could use this functionality from my client machine (win xp sp3) I have loaded powershell 2.0 on it, however, running the statement ...

Scenarios Tutorials
Hi everybody, I am new to exceltip forum. I need help on usage of Scenarios in excel. I have tried to find out Tutorials with example for usingh Scenarios but was unable to dig out anything like i found for Pivot tables (i.e. Pivot table 101 etc.) if any body knows nay link where i can have it for free, plz. tell me. I would be greatful to u. thanx in advance. Vikesh --- Message posted from http://www.ExcelForum.com/ There's a Scenario sample workbook on my web site: http://www.contextures.com/excelfiles.html Under the heading 'Scenarios', look for 'Model Pricing S...

R6025 pure virtual function call error on 2006 deluxe with XP
I have the trial 2006 deluxe edition of Money. I have been using it fine for about 2 months until just yesterday it started giving me this error after I do an update to retrieve info from my bank. Runtime Error! Program:..gram files\Microsoft Money 2006\MNYCoreFiles\msmoney.exe R6025 - pure virtual function call I found a KB article on older version of money and it suggests registering Cyptography DLLs which it doesnt work that way on XP. I also found some mention of reinstalling MDAC which I tried by reinstalling the MDAC.INF from my XP install files. None of this works. I even uninstalle...

Tutorial should be with program.
The tutorials should be with all the programs. Excel , Access , Power Point, etc. ---------------- This post is a suggestion for Microsoft, and Microsoft responds to the suggestions with the most votes. To vote for this suggestion, click the "I Agree" button in the message pane. If you do not see the button, follow this link to open the suggestion in the Microsoft Web-based Newsreader and then click "I Agree" in the message pane. http://www.microsoft.com/office/community/en-us/default.mspx?mid=155fefb5-4267-43a8-aaa9-d6cacc9c2824&dg=microsoft.public.excel.misc ...

Macro or scripting help
I have a spreadsheet that goes to column P. All cells have data except for Column M. Half of column M contains data and the rest are empty cells. I need to copy or move all data rows associated with the cells in column M that contains data. Normally I would be able to do this by just recording macros, but data is constantly being added. Hope I explained this correctly. Any advice is greatly appreciated. Thanks. The below suggestion uses autofilter to only show those rows where column(M) is nonblank, then copy all of the visible rows to your destination. You didn't ...

Free tutorial
How can I find a fine tutorials/ebboks for batch and shell programming for dos and windows . I have a unix shell programming background . Thanks happytoday wrote: > How can I find a fine tutorials/ebboks for batch and shell programming > for dos and windows . I have a unix shell programming background . Where can I find online tutorials to learn unix shell programming? -- Todd Vargo (Post questions to group only. Remove "z" to email personal messages) --- news://freenews.netfront.net/ - complaints: news@netfront.net --- happytoday (ehabaziz2001@gmail.com) ...