Groovy 脚本运行
1)通过命令行运行
通过命令行运行脚本,需要现将 groovy/bin 加入到系统变量,此时就可以随处通过 groovy.bat/groovy.sh 运行 groovy 脚本
groovy foo/MyScript.groovy [arguments]
如运行以下脚本: hello.groovy
println "Hi ${args[0]}, welcome to Groovy!"
执行命令行
$ groovy Hello.groovy assad
> hi assad, welcome to Groovy!
2)使用 groovy 创建 Unix 脚本
如编写以下 groovy 脚本:hello.groovy
#!/usr/bin/env groovy
println("this is groovy script")
println("Hi,"+args[0]+" welcome to Groovy")
执行以下命令
$ chmod +x hello.groovy
$ ./hello.groovy assad
Groovy 运行系统指令
可以在 groovy 脚本中运行系统指令,如下:
Process proc = "rm -rf ./test.txt".execute();
proc.waitFor(); //线程挂起,等待proc执行完毕
Process proc = "ls /".execute()
proc.waitFor()
println(proc.text) //获取指令指定返回
清屏
printf "\033c"