Get groovy for better shell scripts(英文快读)

Get groovy for better shell scripts(英文快读),第1张

概述I often use shell scripts to automate mundane, repeatable tasks on my computer. Since I’ve found Groovy, though, I have discovered a great way tomake writing those scripts easier and more enjoyable. T

I often use shell scripts to automate mundane,repeatable tasks on my computer. Since I’ve found Groovy,though,I have discovered a great way tomake writing those scripts easIEr and more enjoyable. This is especially true if I have anything complex to do,and it saves me a LOT of time. Allow me to elaborate.

Getting started with command-line Groovy

like many of the tools I advocate here,you’ll want to grab Cygwin for the best experIEnce.

There are thorough instructions for getting Groovy running within the Groovy documentation. Basically you just download a ZIP,extract it where you want,and add a couple environment variables.

Can be installed easily on linux or Mac:

brew install groovy          # Homebrewsudo apt-get install groovy  # Debian-basedsudo yum install groovy      # RHEL and frIEnds@H_419_21@ 

Now you can start writing shell scripts in Groovy. Let’s write a little script to test it out:

#!/usr/bin/env groovyprintln "Yay! I can finally be expressive Now!"@H_419_21@ 

Actually,there are a ton of ways to run Groovy,but I’m just going to focus on scripts for Now.

chmod +x hello.groovyhello.groovy@H_419_21@  Bash vs. Groovy example 

Let’s say I want to have a script that can check my frIEnds’ last X tweets so I don’t have to leave my command-line to check twitter.

#!/bin/bashusername=xxxxxxxxpassword=yyyyyyyyyynumTweets=10#output tweets XMLcurl --basic --user $username:$password http://twitter.com/statuses/frIEnds_timeline.xml?count=$numTweets#Some crazy AWK goes here... your assignment ;)@H_419_21@ 

I really don’t want to read my tweets in XML. Being a Java guy,I wonder if there is a way we can harness it’s power. Groovy is basically enhanced Java so I can

#!/usr/bin/env groovyusername = "xxxxxxxx"password = "yyyyyyyyyyy"numTweets = "10"//If we have an argument use itif (args && args[0].tofloat() > 0) numTweets = args[0]//Use twitter API with cURLoutput = "curl -u $username:$password http://twitter.com/statuses/frIEnds_timeline.xml?count=$numTweets".execute().text//Parsing XML is Amazingly easy in Groovytweets = new XmlSlurper().parseText(output)tweets.status.each { tweet->    println "${tweet.user.name}: ${tweet.text}"}@H_419_21@ 

And run it just like any shell script:

chmod +x checktweets.groovychecktweets.groovy 15@H_419_21@ 

Groovy can certainly do much more than deal with XML,it is a full-featured dynamic language with great expressiveness. It is simply satisfying to write,and you can do everything a shell script can do and more.

As an extra treat,here is a Groovy script to update your twitter status,tweet.groovy:

#!/usr/bin/env groovyusername = "xxxxxxxx"password = "yyyyyyyyyyy"if (args) {    status = args[0]    println "curl -u $username:$password -d status=\"${status}\" http://twitter.com/statuses/update.xml".execute().text}@H_419_21@ 

I personally have a /scripts directory in my home dir which I put on my path,so to run the prevIoUs script I just have to type:

tweet.groovy "Twitter is Now Groovy,baby!"@H_419_21@  Conclusion 

Ok,so if you’re fairly savvy with your old-school shell scripting,I don’t expect you to switch to Groovy for simple tasks. I see 2 major cases for using it:

You need to do complex operations on data You kNow Java better than your shell scripting

If you like Groovy and want to learn more,you might consIDer checking out the Groovy docs.

Posted on under groovy

总结

以上是内存溢出为你收集整理的Get groovy for better shell scripts(英文快读)全部内容,希望文章能够帮你解决Get groovy for better shell scripts(英文快读)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1254519.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存