Groovy 作为系统脚本使用

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"


 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值