加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 16:42:26 所属栏目:大数据 来源:网络整理
导读: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 to make writing those scripts easier and more enjoyable . This is especially true if I have anythi

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          # Homebrew
sudo apt-get install groovy  # Debian-based
sudo yum install groovy      # RHEL and friends

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

#!/usr/bin/env groovy
println "Yay! I can finally be expressive now!"

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

chmod +x hello.groovy
hello.groovy

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/bash
username=xxxxxxxx
password=yyyyyyyyyy
numTweets=10

#output tweets XML
curl --basic --user $username:$password http://twitter.com/statuses/friends_timeline.xml?count=$numTweets
#Some crazy AWK goes here... your assignment ;)

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 groovy

username = "xxxxxxxx"
password = "yyyyyyyyyyy"
numTweets = "10"

//If we have an argument use it
if (args && args[0].toFloat() > 0) numTweets = args[0]

//Use twitter API with cURL
output = "curl -u $username:$password http://twitter.com/statuses/friends_timeline.xml?count=$numTweets".execute().text

//Parsing XML is Amazingly easy in Groovy
tweets = new XmlSlurper().parseText(output)
tweets.status.each { tweet->
    println "${tweet.user.name}: ${tweet.text}"
}

And run it just like any shell script:

chmod +x checktweets.groovy
checktweets.groovy 15

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 groovy

username = "xxxxxxxx"
password = "yyyyyyyyyyy"

if (args) {
    status = args[0]
    println "curl -u $username:$password -d status="${status}" http://twitter.com/statuses/update.xml".execute().text
}

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!"

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读