質問

セットアップを「ワンクリック展開」に少し近づけるために、グルーヴィーなスクリプトを使用して、バットスクリプトによって制御された他のプロセスを開始/停止し、ファイルシステムのさまざまな部分で、さらにはさまざまなマシンで実行します。

これらのスクリプトを実行する方法とそれを行う方法 彼らの それぞれの作業ディレクトリ?

私はジャワのことを知っています

java.lang.Runtime's exec()

しかし、これには多くの問題がありますが、Groovyにもこれのためにある種の速記があるのだろうかと思いましたか?

ありがとう!

役に立ちましたか?

解決

groovyは、古い文字列にexecute()メソッドを追加したので、これを試してください。

println "ls -la".execute().text

他のヒント

The execute() method can be used to change directories if you prefix it with the "cmd /c" command, and then use ampersand (assuming Windows) to chain commands together.

Example, assuming you want to go to subdirectory subdir and run a couple of batch files from there:

println "cmd /c cd subdir & batch1.bat & batch2.bat".execute().text

Not sure if there isn't a better way, but this does work.

You can also use ProcessBuilder which is a surprisingly convienent Java class introduced in java 5.

ProcessBuilder lets you

  • determine the working directory
  • determine which environmental variables the process should have

See http://download.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html for a brief example and more documentation.

If you aren't afraid to create some reusable code you could create an object that wraps an .execute() process. I created something like this and use it regularly.

Create a new process with:

def proc="cmd".execute()

After that you can use "consumeProcessOutput()" to manage the input and output of "proc". Anything you send to it will be acted on as though you typed it into a shell, and all the output of that shell will be available to you.

I wrapped all this up in a closure so that you could do this:

cmd("cd \\ \n dir ") {
    if(it.contains("AUTOEXEC.BAT")) 
        println it;
    return true;
}

To get a display that shows only the autoexec.bat line. Note that until you return true from the closure, the stdin of that process is available so you can send more lines of text and interact with it indefinitely.

I use it quite a bit because commands like "cd" and "Dir" don't work in windows with .execute(), so a simple:

def directoryListing=cmd("cd\\\ndir")

will get me a quick directory listing with ease.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top